我用python,flask,pymessenger,witai创建了一个聊天机器人。我添加了一些按钮,但是一旦用户按下/从菜单中选择选项,我就不知道如何回复。下面是我的代码。
@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)
用户选择一个选项后,我不知道该如何处理请求。例如,如果用户选择披萨,我想以文本或按钮格式提供披萨菜单。
答案 0 :(得分:0)
您可以从数据
中检索有效载荷if 'postback' in data['entry'][0]['messaging'][0] :
payload = data['entry'][0]['messaging'][0]['postback']['payload']
sender = data['entry'][0]['messaging'][0]['sender']['id']