Exemple #1
0
    def get_chat_events(cls, cursor, chat_id, include_meta=False):
        """Read all events in the chat specified by chat_id.

        Returns:
            [Event]

        """
        cursor.execute('SELECT * FROM event WHERE chat_id=? ORDER BY time ASC',
                       (chat_id, ))
        logged_events = cursor.fetchall()

        chat_events = []
        agent_chat = {0: False, 1: False}
        for row in logged_events:
            # Compatible with older event structure
            agent, action, time, data = [
                row[k] for k in ('agent', 'action', 'time', 'data')
            ]
            try:
                start_time = row['start_time']
            except IndexError:
                start_time = time
            try:
                metadata = json.loads(row['metadata'])
            except IndexError:
                metadata = None

            if not include_meta:
                if action == 'join' or action == 'leave' or action == 'typing':
                    continue
            if action == 'message' and len(data.strip()) == 0:
                continue

            data = cls.process_event_data(action, data)
            agent_chat[agent] = True
            time = cls.convert_time_format(time)
            start_time = cls.convert_time_format(start_time)
            event = Event(agent,
                          time,
                          action,
                          data,
                          start_time,
                          metadata=metadata)
            chat_events.append(event)

        return chat_events
Exemple #2
0
    def build_event(self, result):
        if isinstance(result, list):
            if len(result) == 0:
                action = 'reject'
                data = {}
            else:
                action = 'select'
                data = {'book': result[0], 'hat': result[1], 'ball': result[2]}
        else:
            action = 'message'
            data = result

        return Event(
            agent=1 - self.agent_num,
            time=self.time,
            action=action,
            data=data,
        )
Exemple #3
0
 def message(self, text, state=None):
     event = Event(agent=self.agent, time=None, action='message', data=text)
     event.state = state
     return event