def handle_mqtt_message(client, userdata, message): data = dict(topic=message.topic, payload=message.payload.decode()) # print(data) message = data["payload"] from_no = data["topic"].split("/")[1] print(message, from_no) if sparrow.is_sparrow_request(message): sparrow.handle_sparrow_request(from_no, message) return receiver = db.getReceiver(from_no) print(receiver) if receiver == db.IBM_RECEIVER: if sparrow.is_command(message): sparrow.handle_command(from_no, message) return reply = chatbot.handle_message(from_no, message) for message in reply: mqtt.publish(data["topic"].replace("receive", "response"), message) sleep(2) return else: messaging.send_message(receiver, message) return
def listen_input(): message = request.values.get('Body', None) from_no = request.values.get('From', None) print(message, from_no) #Handling Media content num_media = int(request.values.get("NumMedia")) if num_media > 0: media_url = request.values.get(f'MediaUrl0') mime_type = request.values.get(f'MediaContentType0') print(media_url, mime_type) if num_media > 1: messaging.send_message( from_no, "Multiple media cannot be sent. Sending only first media") #Handling @statefarm commands if statefarm.is_statefarm_request(message): t = Thread(target=statefarm.handle_statefarm_request, args=( from_no, message, )) t.start() # statefarm.handle_statefarm_request(from_no, message) return str(MessagingResponse()) receiver = db.getReceiver(from_no) if receiver == db.IBM_RECEIVER: if statefarm.is_command(message): t = Thread(target=statefarm.handle_command, args=(from_no, message)) t.start() return str(MessagingResponse()) elif num_media > 0: reply = "Sorry! Our Automated chatbot doesn't support Media at this point." elif message == "": reply = "Invalid format. Your message is empty!" else: replies = chatbot.handle_message(from_no, message) if len(replies) > 1: t = Thread(target=messaging.send_messages, args=(from_no, replies)) t.start() return (str(MessagingResponse())) else: reply = replies[0] resp = MessagingResponse() resp.message(reply) return str(resp) else: if num_media > 0: messaging.send_message_with_media(from_no, receiver, message, media_url, mime_type) elif message == "": messaging.send_message(from_no, "Invalid message. Can't be sent") else: messaging.send_message(receiver, message) return str(MessagingResponse())
def handle_command(from_no, message): receiver = db.getReceiver(from_no) if connect.is_connect_requested(message): if receiver == db.IBM_RECEIVER: connect.connect(from_no, message) else: messaging.send_messages(from_no, ["You are already connected to a sparrow agent.","To disconnect first message\n@sparrow disconnect"]) elif connect.is_stop_requested(message): if receiver != db.IBM_RECEIVER: connect.disconnect(from_no, receiver) else: messaging.send_messages(from_no, ["You are not connected to any sparrow agent.","To connect first message\nconnect doctor/community"]) elif message == "list commands": messaging.send_messages(from_no, ["Sparrow is here to help you", "Use @sparrow to send sparrow commands\n\"@sparrow connect to doctor\" \tConnects you to doctor\n\"@sparrow connect to community\" \tConnects you to community\n\"@sparrow disconnect\" \tDisconnects you\n"]) else: messaging.send_message(from_no, "We cannot understand your command.\nMessage @sparrow help to get the commands")
def handle_command(from_no, message): receiver = db.getReceiver(from_no) if connect.is_connect_requested(message): if receiver == db.IBM_RECEIVER: connect.connect(from_no, message) else: messaging.send_messages(from_no, [ "You are already connected to a statefarm agent.", "To disconnect first message\n@statefarm disconnect" ]) elif connect.is_stop_requested(message): if receiver != db.IBM_RECEIVER: connect.disconnect(from_no, receiver) else: messaging.send_messages(from_no, [ "You are not connected to any statefarm agent.", "To connect first message\nconnect agent/community" ]) elif is_register_command(message): if message == "register as agent": db.updateUserType(from_no, db.TYPE_AGENT) messaging.send_messages(from_no, [ "You are now registered as statefarm agent.", "We will connect you to the user when asked for" ]) elif message == "register as user": db.updateUserType(from_no, db.TYPE_USER) messaging.send_messages(from_no, [ "You are now a user.", "You can talk to our chatbot or conenct to statefarm agent" ]) else: messaging.send_message( from_no, "We cannot understand your command.\nMessage @statefarm list commands to get the commands" ) elif message == "list commands": messaging.send_messages(from_no, [ "Statefarm is here to help you", "Use @statefarm to send statefarm commands\n\"@statefarm connect to agent\" \tConnects you to agent\n\"@statefarm connect to community\" \tConnects you to community\n\"@statefarm disconnect\" \tDisconnects you\n\"@statefarm register as agent\" \Registers you as agent\n\"@statefarm register as user\" \Registers you as user\n" ]) else: messaging.send_message( from_no, "We cannot understand your command.\nMessage @statefarm list commands to get the commands" )