def send(self): # TODO: even if cross talk is enabled, we don't want the bot to talk in a row if self.num_utterances >= 1: return None if self.received is False and (self.prev_action == 'select' or \ self.last_message_timestamp + random.uniform(1, self.PATIENCE) > time.time()): return None if len(self.queued_event) == 0: self.queued_event.append(self.session.send()) event = self.queued_event[0] if event is None: return self.queued_event.popleft() if event.action == 'message': delay = float(len(event.data)) / self.CHAR_RATE + random.uniform( 0, self.EPSILON) elif event.action == 'select': delay = self.SELECTION_DELAY + random.uniform(0, self.EPSILON) if self.prev_action == 'select': delay += self.REPEATED_SELECTION_DELAY # TODO: refactor this elif event.action in ('offer', 'accept', 'reject', 'done', 'quit'): delay = self.SELECTION_DELAY + random.uniform(0, self.EPSILON) elif event.action == 'join': delay = 0.5 else: raise ValueError('Unknown event type: %s' % event.action) if self.last_message_timestamp + delay > time.time(): # Add reading time before start typing reading_time = 0 if self.prev_action == 'join' else random.uniform( 0.5, 1) if event.action == 'message' and self.start_typing is False and \ self.last_message_timestamp + reading_time < time.time(): self.start_typing = True return Event.TypingEvent(self.agent, 'started') else: return None else: if event.action == 'message' and self.start_typing is True: self.start_typing = False return Event.TypingEvent(self.agent, 'stopped') elif event.action == 'join': event = self.queued_event.popleft() return event else: event = self.queued_event.popleft() self.prev_action = event.action self.received = False self.num_utterances += 1 self.last_message_timestamp = time.time() event.time = str(self.last_message_timestamp) return event
def typing_event(): backend = get_backend() action = request.args.get('action') uid = userid() chat_info = backend.get_chat_info(uid) backend.send( uid, Event.TypingEvent(chat_info.agent_index, action, str(time.time()))) return jsonify(success=True)