Пример #1
0
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
Пример #2
0
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"
        )
Пример #4
0
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())
Пример #5
0
def connect_expert(userID, type):
    expert = db.findExpert(userID, type)
    if expert:
        db.setupConnection(userID, expert)
        messaging.send_message(userID, "We have connected you to a " + type)
        messaging.send_message(expert, "We have connected you to a user")
    else:
        messaging.send_message(userID, "No " + type + " is available")
    return expert
Пример #6
0
def sendMessage():
    userID = request.values.get('userID', None)
    message = request.values.get('message', None)
    expert = messaging.send_message(userID, message)
    return str("Success")
Пример #7
0
def disconnect(user1, user2):
    db.breakConnection(user1, user2)
    messaging.send_message(user1, "You have been disconnected")
    messaging.send_message(user2, "The other party disconnected")
Пример #8
0
def connect(userID, message):
    if is_connect_agent(message):
        agent = db.findAgent(userID)
        if agent:
            db.setupConnection(userID, agent)
            messaging.send_message(userID, "We have connected you to a agent")
            messaging.send_message(agent, "We have connected you to a user")
        else:
            messaging.send_message(userID, "No agents are available")
    elif is_connect_community(message):
        community = db.findCommunity(userID)
        if community:
            db.setupConnection(userID, community)
            messaging.send_message(userID, "We have connected you to a member")
            messaging.send_message(community,
                                   "We have connected you to a user")
        else:
            messaging.send_message(userID, "No members are available")
Пример #9
0
def connect(userID, message):
    if is_connect_doctor(message):
        doctor = db.findDoctor(userID)
        if doctor:
            db.setupConnection(userID, doctor)
            messaging.send_message(userID, "We have connected you to a doctor")
            messaging.send_message(doctor, "We have connected you to a user")
        else:
            messaging.send_message(userID, "No doctors are available")
    elif is_connect_community(message):
        community = db.findCommunity(userID)
        if community:
            db.setupConnection(userID, community)
            messaging.send_message(userID, "We have connected you to a member")
            messaging.send_message(community,
                                   "We have connected you to a user")
        else:
            messaging.send_message(userID, "No members are available")