Beispiel #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
    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