def execute_actions(self, session, actions): # pylint: disable=no-self-use, unused-argument if actions is not None: for action in actions: processed = False if self.type == 'twilio': from twilio_support.models import execute_action as twilio_execute # pylint: disable=import-outside-toplevel processed = twilio_execute(self, session, action) elif self.type == 'http': from http_support.models import execute_action as http_execute # pylint: disable=import-outside-toplevel processed = http_execute(self, session, action) elif self.type == 'command_line': from cli_support.models import execute_action as cli_execute # pylint: disable=import-outside-toplevel processed = cli_execute(self, session, action) if processed is False: processed = execute_action(self, session, action) if processed is False: settings.FETCH_LOGGER().warn('TODO: Process', action) log(self.log_id(), 'Executed action.', tags=['integration', 'action'], metadata=action, player=session.player, session=session, game_version=session.game_version)
def complete(self): dialog_key = 'session-' + str(self.pk) last_dialog = None for dialog in Dialog.objects.filter(key=dialog_key, finished=None): dialog.finish() last_dialog = dialog self.completed = timezone.now() self.save() metadata = {} if last_dialog is not None: metadata['dialog'] = 'dialog:%d' % last_dialog.pk log(self.log_id(), 'Completed dialog.', tags=['session', 'dialog'], metadata=metadata, player=self.player, session=self, game_version=self.game_version)
def process_player_incoming(self, player_lookup_key, player_lookup_value, payload, extras=None): player_match = None for player in Player.objects.all(): if player_lookup_key in player.player_state: if player.player_state[ player_lookup_key] == player_lookup_value: player_match = player if player_match is None and self.create_new_players: player_match = Player(identifier=(player_lookup_key + ':' + player_lookup_value)) player_match.player_state[player_lookup_key] = player_lookup_value # pylint: disable=unsupported-assignment-operation player_match.save() if player_match is not None: session = self.game.current_active_session(player=player_match) if session is None: session = Session(game_version=self.game.versions.order_by( '-created').first(), player=player_match, started=timezone.now()) session.save() if extras is not None and 'last_message' in extras: del extras['last_message'] if extras is not None and ( 'message_type' in extras) and extras['message_type'] == 'call': pass else: pass # session.process_incoming(self, None, extras) log(self.log_id(), 'Processing incoming payload.', tags=['integration'], metadata=payload, player=player_match, session=session, game_version=session.game_version) if isinstance(payload, list): actions = payload payload = None self.execute_actions(session, actions) session.process_incoming(self, payload, extras)
def set_variable(self, variable, value): old_value = self.session_state.get(variable, None) self.session_state[variable] = value # pylint: disable=unsupported-assignment-operation self.save() metadata = { 'variable_name': variable, 'old_value': old_value, 'new_value': value } log(self.log_id(), 'Set session variable (%s = %s).' % (variable, value), tags=['session', 'variable'], metadata=metadata, player=self.player, session=self, game_version=self.game_version)
def set_variable(self, variable, value): old_value = self.game_state.get(variable, None) self.game_state[variable] = value # pylint: disable=unsupported-assignment-operation self.save() metadata = { 'variable_name': variable, 'old_value': old_value, 'new_value': value } version = self.versions.order_by('-created').first() log(self.log_id(), 'Set game variable (%s = %s).' % (variable, value), tags=['game', 'variable'], metadata=metadata, player=None, session=None, game_version=version)
def incoming_twilio_call(request): # pylint: disable=too-many-branches, too-many-statements response = VoiceResponse() if request.method == 'POST': # pylint: disable=too-many-nested-blocks now = timezone.now() integration_match = None post_dict = request.POST.dict() source = post_dict['From'] destination = post_dict['To'] for integration in Integration.objects.filter(type='twilio'): if 'phone_number' in integration.configuration and ( source == integration.configuration['phone_number'] or destination == integration.configuration['phone_number']): integration_match = integration if source == integration.configuration['phone_number']: destination = post_dict['From'] source = post_dict['To'] post_dict['From'] = source post_dict['To'] = destination if 'CallStatus' in post_dict: incoming = IncomingCallResponse(source=source) incoming.receive_date = now incoming.message = '' if 'Digits' in post_dict: incoming.message = post_dict['Digits'].strip() if 'SpeechResult' in post_dict: if incoming.message: incoming.message += ' | ' incoming.message += post_dict['SpeechResult'].strip() incoming.transmission_metadata = post_dict if integration_match is not None: incoming.integration = integration_match if incoming.message.strip() == '': incoming.message = None incoming.save() if integration_match is not None: integration_match.process_incoming(post_dict) for call in OutgoingCall.objects.filter( destination=source, sent_date=None, send_date__lte=timezone.now(), integration=integration_match).order_by('send_date'): if call.next_action != 'gather': if call.message is not None and call.message != '': if call.message.lower().startswith( 'http://') or call.message.lower().startswith( 'https://'): response.play( call.message.replace('\n', ' ').replace( '\r', ' ').split(' ')[0]) else: response.say(call.message) elif call.file is not None and call.file != '': pass call.sent_date = timezone.now() call.save() if call.next_action == 'pause': response.pause(length=call.pause_length) elif call.next_action == 'gather': args = { 'input': call.gather_input, 'barge_in': True, 'num_digits': 1 } if call.gather_timeout is not None: args['timeout'] = call.gather_timeout # if call.gather_finish_on_key is not None: # args['finish_on_key'] = call.gather_finish_on_key # if call.gather_num_digits is not None: # args['num_digits'] = call.gather_num_digits if call.gather_speech_timeout is not None: args['speech_timeout'] = call.gather_speech_timeout if call.gather_speech_model is not None: args['speech_model'] = call.gather_speech_model gather = Gather(**args) if call.message is not None and call.message != '': if call.message.lower().startswith( 'http://') or call.message.lower().startswith( 'https://'): gather.play(call.message.replace( '\n', ' ').replace('\r', ' ').split(' ')[0], loop=call.gather_loop) else: gather.say(call.message, loop=call.gather_loop) elif call.file is not None and call.file != '': pass response.append(gather) break elif call.next_action == 'hangup': response.hangup() break if len(response.verbs) == 0: # pylint: disable=len-as-condition log_metadata = {} log_metadata['phone_number'] = post_dict.get('To', None) log_metadata['direction'] = post_dict.get('Direction', None) log_metadata['call_status'] = post_dict.get('CallStatus', None) log('twilio:incoming_twilio_call', 'Sending empty voice response back to Twilio. (Tip: verify that you are not stuck on a process response or other card awaiting user input.)', tags=['twilio', 'voice', 'warning'], metadata=log_metadata) return HttpResponse(str(response), content_type='text/xml')
def translate_value(self, value, session, scope='session'): # pylint: disable=unused-argument, no-self-use, too-many-branches translated_value = value try: while '[ME]' in translated_value: translated_value = translated_value.replace( '[ME]', session.player.identifier) while '[LAST-MESSAGE]' in translated_value: translated_value = translated_value.replace( '[LAST-MESSAGE]', session.last_message()) while '[LAST-MESSAGE-TYPE]' in translated_value: translated_value = translated_value.replace( '[LAST-MESSAGE-TYPE]', session.last_message_type()) while '[SESSION:' in translated_value: start = translated_value.find('[SESSION:') end = translated_value.find(']', start) if end != -1: tag = translated_value[start:(end + 1)] variable = tag[9:-1] variable_value = session.fetch_variable(variable) if variable_value is None: variable_value = '???' translated_value = translated_value.replace( tag, str(variable_value)) while '[GAME:' in translated_value: start = translated_value.find('[GAME:') end = translated_value.find(']', start) if end != -1: tag = translated_value[start:(end + 1)] variable = tag[6:-1] variable_value = session.game_version.game.fetch_variable( variable) if variable_value is None: variable_value = '???' translated_value = translated_value.replace( tag, variable_value) while '[PLAYER:' in translated_value: start = translated_value.find('[PLAYER:') end = translated_value.find(']', start) if end != -1: tag = translated_value[start:(end + 1)] variable = tag[8:-1] variable_value = session.player.fetch_variable(variable) if variable_value is None: variable_value = '???' translated_value = translated_value.replace( tag, variable_value) if translated_value != value: metadata = { 'original_value': value, 'translated_value': translated_value, } log(self.log_id(), 'Translated value.', tags=['integration', 'translate'], metadata=metadata, player=session.player, session=session, game_version=session.game_version) except TypeError: pass # Attempting to translate non-string return translated_value