def webhook(self, request): from botshot.core.chat_manager import ChatManager if request.method == "POST": manager = ChatManager() request_body = json.loads(request.body.decode('utf-8')) raw_message, entities = self.parse_raw_message(request_body) logging.debug("Received raw message: %s", raw_message) self.responses[raw_message.raw_user_id] = [] manager.accept_with_entities(raw_message, entities) return self._generate_response(raw_message.raw_user_id) return HttpResponse(status=405)
def _schedule_wrapper(self, conversations: ConversationFilter, action, task_id=None): logging.debug("Running schedule %s", task_id) conversations = conversations.get_ids() if isinstance(action, dict): # Botshot state + context schedule, different for each conversation manager = ChatManager() for id in conversations: manager.accept_scheduled( conversation_id=id, user_id=None, payload=action) elif isinstance(action, MessageElement): # Prepared MessageElement schedule, sent as a broadcast # TODO: send the message to the interfaces # TODO: send more messages at once raise NotImplementedError()
def webhook(self, request): from botshot.core.chat_manager import ChatManager if request.method == "POST": manager = ChatManager() request_body = json.loads(request.body.decode('utf-8')) try: raw_message = self.parse_raw_message(request_body) except: return HttpResponseBadRequest() logging.info("Received raw message: %s", raw_message) self.responses[raw_message.raw_user_id] = [] manager.accept(raw_message) return self._generate_response(raw_message.raw_user_id) return HttpResponseBadRequest()
def webhook(self, request): manager = ChatManager() text = request.POST.get('message') payload = None if request.POST.get('payload'): payload = json.loads(request.POST.get('payload')) if not text and not payload: logging.warning("[Webchat] Neither text nor payload was provided") return False webchat_id = request.session["webchat_id"] msg_type = ChatMessage.BUTTON if payload else ChatMessage.MESSAGE raw_message = RawMessage(interface=self, raw_user_id=webchat_id, raw_conversation_id=webchat_id, conversation_meta={}, type=msg_type, text=text, payload=payload, timestamp=time.time()) self.on_message_received(raw_message) logging.info("[Webchat] Received raw message: %s", raw_message) run_async(manager.accept, raw_message=raw_message) return True
def test_send_messages(self): message, context, logger = mock.Mock(), mock.Mock(), mock.Mock() chat_mgr = ChatManager() dialog = Dialog(message, context, chat_mgr, logger) chat_mgr.send = mock.Mock() dialog.send("Hello world") assert chat_mgr.send.called chat_mgr.send = mock.Mock() dialog.send(TextMessage("Hi")) assert chat_mgr.send.called with pytest.raises(ValueError): dialog.send(105) with pytest.raises(ValueError): dialog.send([None, None])
def __init__(self): self.message = ChatMessage() self.sender = ChatUser() self.conversation = ChatConversation() self.conversation.conversation_id = 1 self.conversation.save() self.chat_manager = ChatManager() self.context = Context.load({}) self.logging_service = Mock() self.sent = [] self.schedules = [] self.inactive_callbacks = []
def webhook(self, request): from botshot.core.chat_manager import ChatManager if request.method == "POST": manager = ChatManager() request_body = json.loads(request.body.decode('utf-8')) raw_messages = self.parse_raw_messages(request_body) for raw_message in raw_messages: diff_seconds = time.time() - raw_message.timestamp if diff_seconds > self.msg_limit_seconds: logging.warning( "Delay {} seconds too big, ignoring message!".format( diff_seconds)) continue self.on_message_received(raw_message) logging.info("Received raw message: %s", raw_message) run_async(manager.accept, raw_message=raw_message) return HttpResponse() elif request.method == "GET": return self.webhook_get(request) return HttpResponseBadRequest()
def test_counter_increment(self, flows, message): proc = MessageProcessor(ChatManager(), message) assert proc.context.counter == 0 proc.process() assert proc.context.counter == 1
def chat_mgr(): yield ChatManager()