'How to add payload request for FB chatbot in python?
I have created a chatbot in python, flask, pymessenger, witai. I have added some button but I don't know how to reply back once the user press/select the option from my menu. Below is my code.
@app.route('/', methods=['GET'])
def verify():
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == "hello":
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Hello World", 200
@app.route('/', methods=['POST'])
def webhook():
data = request.get_json()
log(data)
if data['object'] == 'page':
for entry in data['entry']:
for messaging_event in entry['messaging']:
sender_id = messaging_event['sender']['id']
recipient_id = messaging_event['recipient']['id']
if messaging_event.get('message'):
if 'text' in messaging_event['message']:
messaging_text = messaging_event['message']['text']
else:
messaging_text = 'no text'
response = None
entity, value = wit_response(messaging_text)
if entity == 'cust_id':
test = cust(str(value))
buttons = []
button = Button(title='1. Pizza', type='postback', payload='Pizza')
buttons.append(button)
button = Button(title='2. Burger', type='postback', payload='Burger')
buttons.append(button)
button = Button(title='3. Pasta', type='postback', payload='Pasta')
buttons.append(button)
text = 'Hey '+test[0] +'👋.'+"\n"+'Select Option:'
bot.send_button_message(sender_id, text, buttons)
I don't know after the user chooses one option how to handle the request. For example, if the user selects Pizza, I want to return the pizza menu in text or button format.
Solution 1:[1]
You can retrieve the payload from data
if 'postback' in data['entry'][0]['messaging'][0] :
payload = data['entry'][0]['messaging'][0]['postback']['payload']
sender = data['entry'][0]['messaging'][0]['sender']['id']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Simon PII |