0

I'm trying to track the users previous actions via a web hook (flask). I've done this by creating a dictionary that reads in the users ID as the key and then keeps replacing the value for every new action (I only need the very last action made). However, when I look at the logs on the Heroku server it seems to be finding this very difficult...It either places a completely different payload in the previous_payload variable (which actually wasn't the last action) or it decides that it no longer registers the sender ID and therefore makes the value 0. I'm not sure if this is because of how I've set up the dictionary (although I can't see how) or because the server is being slow, which is causing a delay and confusing the variable holding the prior event.

This code should be executing synchronously and as such track able - so why does prior_payload not seem to be linear?

Is there an alternative that anyone can suggest or see a mistake I've made in my code that may be causing this?

The important part is the below code but just for context the send_qresponse function just send a post request back to the server based on what the user did (postback/ quick reply etc).

The main reason I need to track the prior event is because I ask the user several questions, and in order to register that they just responded to an open ended question I check the last event they made.

Thanks in advance!

import os
import sys
import json
import time

import requests
from flask import Flask, request

prior_payload = {}

app = Flask(__name__)

@app.route('/', methods=['POST'])
def webhook():
    log(prior_payload.keys())
    log(prior_payload.values())
    # endpoint for processing incoming messaging events
    data = request.get_json()  

if data["object"] == "page":

    for entry in data["entry"]:
        time = entry['time']
        for messaging_event in entry["messaging"]:

            sender_id = str(messaging_event["sender"]["id"])

            if sender_id not in prior_payload.keys():
                prior_payload[sender_id] = 0

            if messaging_event.get("message"):  # someone sent us a message

                if "attachments" in messaging_event['message']:
                    sender_id = str(messaging_event["sender"]["id"])        
                    recipient_id = messaging_event["recipient"]["id"]  
                    payload = "uploaded random file"

                    mistake_message(sender_id, payload ,prior_payload[sender_id])
                    user_input(sender_id, payload, time, prior_payload[sender_id])

                elif messaging_event["message"]["text"] == 'done' or messaging_event["message"]["text"] == 'Done':
                    sender_id = str(messaging_event["sender"]["id"])
                    payload = messaging_event["message"]["text"]
                    send_qresponse(sender_id, payload)

                    user_input(sender_id, payload, time, prior_payload[sender_id])

                elif "quick_reply" in messaging_event["message"]:
                    sender_id = str(messaging_event["sender"]["id"])
                    payload = messaging_event["message"]["quick_reply"]['payload']
                    prior_payload[sender_id] = None
                    prior_payload[sender_id] = str(payload)

                    send_qresponse(sender_id, payload)
                    user_input(sender_id, payload, time, prior_payload[sender_id])


                elif messaging_event['message']['text'] == "ready" or messaging_event['message']['text'] == "Ready":
                    sender_id = str(messaging_event["sender"]["id"])
                    payload = messaging_event["message"]["text"]

                    send_qresponse(sender_id, payload)
                    user_input(sender_id, payload, time, prior_payload[sender_id])


                elif prior_payload[sender_id] == "ready_vacancies":
                    sender_id = str(messaging_event["sender"]["id"])
                    prior_payload[sender_id] = "second_question"                                
                    recipient_id = messaging_event["recipient"]["id"]
                    payload = "second_question"
                    send_qresponse(sender_id, payload)

                    user_input(sender_id, payload, time, prior_payload[sender_id])


                elif prior_payload[sender_id] == "second_question":
                    sender_id = str(messaging_event["sender"]["id"])        
                    prior_payload[sender_id] = "third_question"
                    recipient_id = messaging_event["recipient"]["id"]
                    payload = "third_question"
                    send_qresponse(sender_id, payload)

                    user_input(sender_id, payload, time, prior_payload[sender_id])

                elif prior_payload[sender_id] == "third_question":
                    sender_id = str(messaging_event["sender"]["id"])        
                    prior_payload[sender_id] = "fourth_question"
                    recipient_id = messaging_event["recipient"]["id"]
                    payload = "fourth_question"

                    send_qresponse(sender_id, payload)

                    user_input(sender_id, payload, time, prior_payload[sender_id])

                elif prior_payload[sender_id] == "fourth_question":
                    sender_id = str(messaging_event["sender"]["id"])        
                    prior_payload[sender_id] = "fifth_question"
                    recipient_id = messaging_event["recipient"]["id"]
                    payload = "fifth_question"
                    send_qresponse(sender_id, payload)

                    user_input(sender_id, payload, time, prior_payload[sender_id])

                elif prior_payload[sender_id] == "fifth_question":
                    sender_id = str(messaging_event["sender"]["id"])        
                    prior_payload[sender_id] = "linkedin_url"
                    recipient_id = messaging_event["recipient"]["id"]
                    payload = "linkedin_url"
                    send_qresponse(sender_id, payload)

                    user_input(sender_id, payload, time, prior_payload[sender_id])

                else:
                    sender_id = str(messaging_event["sender"]["id"])        
                    recipient_id = messaging_event["recipient"]["id"]  
                    payload = str(messaging_event["message"]["text"])

                    mistake_message(sender_id, payload)

                    user_input(sender_id, payload, time, prior_payload[sender_id])

            if messaging_event.get("delivery"):  # delivery confirmation
                pass

            if messaging_event.get("optin"):  # optin confirmation
                pass

            if messaging_event.get("postback"):  # user clicked/tapped "postback" button in earlier message

                sender_id = str(messaging_event["sender"]["id"])
                recipient_id = messaging_event["recipient"]["id"]
                payload = str(messaging_event['postback']['payload'])
                prior_payload[sender_id] = str(payload)

                send_qresponse(sender_id, payload)
                user_input(sender_id, payload, time, prior_payload[sender_id])



return "ok", 200
Hazzamataza
  • 983
  • 2
  • 12
  • 25
  • I don't believe this is the same? You've mentioned in other thread that it's not synchronous but my code should be as its a single thread? – Hazzamataza Mar 28 '17 at 21:52
  • Could you shed some light on why you believe it's not working? I've read the other thread you have said its the same as but I still don't understand why it's not working... Thanks – Hazzamataza Mar 28 '17 at 21:53

0 Answers0