def standby_handler(messages): for message in messages: # if it is a standby message with only postback, then it is that # nasty case where facebook echoes the bots postback messages even # when it has the control of the chat if message.get("postback", False) and not message.get("message", False): # just ignore it continue sender = message["sender"]["id"] recipient = message["recipient"]["id"] is_echo = message["message"].get("is_echo", False) # If echo, the sender is the page if is_echo: # Reset standby counter Chat.set_standby(recipient) # append to log if log is enabled Chat.append_log(recipient, message["message"].get("text", ""), True) continue # if chat is not standby(expired) if not Chat.is_standby(sender) and Chat.check_expired(sender): message_and_postback_and_handover_handler([message]) continue # If the chat is on standby if Chat.is_standby(sender): # reset standby Chat.set_standby(sender) # append to log if log is enabled Chat.append_log(sender, message["message"].get("text", ""), False) continue
def postback_handler(uid, postback): handlers = { "get_started": greetings, "base_query": base_query, "send_spotted": send_spotted, "ask_question": ask_question, "raw_solution_found": raw_solution_found, "raw_solution_not_found": raw_solution_not_found, "other": other, } if not Chat.is_standby(uid): handlers[postback["payload"]](uid)
def postback_handler(uid, postback): handlers = { 'get_started': greetings, 'base_query': base_query, 'send_spotted': send_spotted, 'ask_question': ask_question, 'raw_solution_found': raw_solution_found, 'raw_solution_not_found': raw_solution_not_found, 'other': other } if not Chat.is_standby(uid): handlers[postback['payload']](uid)
def message_handler(uid, message): if Chat.is_standby(uid): # Ignore if, the user is standby return if not Chat.is_accepting_raw(uid): # If not accepting raw, return base return base_query(uid) # Set last sent raw message Chat.set_last_raw_message(uid, message['text']) # Process raw message response = api_process_raw_bot_message(message['text']) result = response['result'] result_status = response['result_status'] if result_status: # Some response was found buttons = [{ 'type': 'postback', 'title': 'Era isso :)', 'payload': 'raw_solution_found' }, { 'type': 'postback', 'title': 'Não ajudou :(', 'payload': 'raw_solution_not_found' }] payload = { 'template_type': 'button', 'text': result, 'buttons': buttons } return template_message.delay(uid, payload) # If no response was found, handover to human Chat.set_raw_solution_not_found(uid) message = 'Boa pergunta :p\nVou consultar os universitários e já volto' chain(simple_message.si(uid, message), pass_thread_control.si(uid))()
def message_handler(uid, message): if Chat.is_standby(uid): # Ignore if, the user is standby return if not Chat.is_accepting_raw(uid): # If not accepting raw, return base return base_query(uid) # Set last sent raw message Chat.set_last_raw_message(uid, message["text"]) # Process raw message response = api_process_raw_bot_message(message["text"]) result = response["result"] result_status = response["result_status"] if result_status: # Some response was found buttons = [ { "type": "postback", "title": "Era isso :)", "payload": "raw_solution_found", }, { "type": "postback", "title": "Não ajudou :(", "payload": "raw_solution_not_found", }, ] payload = {"template_type": "button", "text": result, "buttons": buttons} return template_message.delay(uid, payload) # If no response was found, handover to human Chat.set_raw_solution_not_found(uid) message = "Boa pergunta :p\nVou consultar os universitários e já volto" chain(simple_message.si(uid, message), pass_thread_control.si(uid))()