Пример #1
0
    def unify_update(self, event: Event, payload: str = None) -> Update:
        """
        Create the internal `Update` type from facebook's `Event`
        """
        ud = Update()
        ud.original_update = event
        ud.client_name = self.client_name
        ud.message_id = event.message_mid
        ud.datetime = datetime.datetime.fromtimestamp(event.timestamp / 1000.0)

        ud.payload = payload

        ud.user, created = User.get_or_create(facebook_id=event.sender_id)
        if created:
            ud.user.save()

        if event.message_attachments:
            try:
                voice = next(x for x in event.message_attachments if x.get('type') == 'audio')
                ud.voice_id = voice['payload']['url']
            except StopIteration:
                pass

        if hasattr(event, 'message_text'):
            ud.message_text = event.message_text
        return ud
Пример #2
0
    def voice_received(self, bot: BotAPIClient, update: Update):
        bot.show_typing(update.user)

        path = os.path.join(ROOT_DIR, 'tmp')
        filepath = bot.download_voice(update.voice_id, path)

        converted = self.voice.convert_audio_ffmpeg(
            filepath, os.path.join(path, 'voice.flac'))

        text = self.voice.recognize(converted)
        if text is None:
            # TODO: Inform the user about failed SR
            log.warning("Could not recognize user input.")
            return

        update.message_text = text
        log.debug(f"Voice message received: {text}")

        self.text_update_received(bot, update)
    def say(self, text):
        update = Update()
        update.message_text = text
        update.message_id = self.msg_count
        update.datetime = datetime.datetime.now()
        update.user = self.user

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.nlp.insert_understanding(update)
        context = self.context_manager.add_incoming_update(update)

        next_response = self.planning_agent.build_next_actions(context)

        actions = next_response.collect_actions()

        context.add_actions(actions)

        self.msg_count += 1
        return actions