コード例 #1
0
ファイル: app.py プロジェクト: MikenIkey/ucla-swipe-bot
def webhook():

    data = request.get_json()
    # for testing
    log(data)

    if data["object"] == "page":
        for entry in data["entry"]:
            for messaging_event in entry["messaging"]:

                # the facebook ID of the person sending you the message
                sender_id = messaging_event["sender"]["id"]
                # the recipient's ID, which should be your page's facebook ID
                recipient_id = messaging_event["recipient"]["id"]

                # someone sent us a message
                if messaging_event.get("message"):
                    # skip message if its an emoji
                    if "text" not in messaging_event["message"]:
                        continue

                    message_text = messaging_event["message"][
                        "text"]  # the message's text

                    # TODO: temporarily starts data request save on begin msg send
                    #       change to on "get started" button press instead later
                    if message_text.lower() == "begin":
                        fb.send_message(sender_id, fb.init_user())
                    else:
                        fb.send_message(sender_id,
                                        fb.setup_str("Got a message!"))

                # user clicked/tapped "postback" button in earlier message
                if messaging_event.get("postback"):
                    payload = messaging_event["postback"]["payload"]
                    handle_payload(sender_id, payload)

    return "ok", 200
コード例 #2
0
 def test_send_time_question(self):
     res_code = fb.send_message(self.my_uid, fb.setup_time())
     self.assertEqual(res_code, 200)
コード例 #3
0
 def test_send_location_question(self):
     res_code = fb.send_message(self.my_uid, fb.init_location())
     self.assertEqual(res_code, 200)
コード例 #4
0
 def test_send_buyer_question(self):
     res_code = fb.send_message(self.my_uid, fb.init_user())
     self.assertEqual(res_code, 200)
コード例 #5
0
 def test_send_message(self):
     res_code = fb.send_message(self.my_uid, fb.setup_str("Test 1"))
     self.assertEqual(res_code, 200)
コード例 #6
0
ファイル: app.py プロジェクト: MikenIkey/ucla-swipe-bot
def handle_payload(uid, payload):
    log("Received postback payload from {id}: {load}".format(id=uid,
                                                             load=payload))

    action, value = payload.split(":")

    if action == "HALL":
        add_hall(uid, value)

    elif action == "BUYER":
        set_buyer(uid, value)

        # if we have no dining hall data
        if "where" not in incomplete_data[uid]:
            fb.send_message(uid, fb.init_location())

    # TODO: Convert to NLP to prompt user for time data
    elif action == "TIME":
        add_time(uid, int(value))

    # use this postback action to resend prompts
    elif action == "GOTO":
        if value == "TIME":
            fb.send_message(uid, fb.setup_time())
        elif value == "HALL":
            fb.send_message(uid, fb.init_location())
        elif value == "DONE":
            usr = incomplete_data[uid]
            log("USR {uid} State when done: {usr}".format(uid=uid, usr=usr))
            # user gave complete data
            if "when" in usr and "where" in usr and "buyer" in usr:
                # add object to complete dict
                complete_data[uid] = incomplete_data[uid]
                del incomplete_data[uid]

                fb.send_message(
                    uid,
                    fb.setup_str(
                        "Great! I will try my best to match you and let you know if I find someone!"
                    ))
                log("Added USR {uid} to complete data db".format(uid=uid))
                log(complete_data)

            else:
                fb.send_message(
                    uid,
                    fb.setup_str(
                        "I don't have the complete information necessary to match you, please fill out the following forms"
                    ))

                if "where" not in usr:
                    fb.send_message(uid, fb.init_location())
                if "when" not in usr:
                    fb.send_message(uid, fb.setup_time())
                if "buyer" not in usr:
                    fb.send_message(uid, fb.init_user())

    else:
        log("Received unhandled payload: {load}".format(load=payload))