我正在尝试使用flask应用程序为Messenger开发一个聊天机器人应用程序。 与ngrok和pymessenger一起使用烧瓶。 我可以更改接收用户消息的默认URL吗? 例如: hello序列发生在“ /”默认URL中,此后,用户消息应发布到“ / login”等。
#We will receive messages that Facebook sends our bot at this endpoint
@app.route("/", methods=['GET', 'POST'])
def receive_message():
if request.method == 'GET':
"""Before allowing people to message your bot, Facebook has implemented a verify token
that confirms all requests that your bot receives came from Facebook."""
token_sent = request.args.get("hub.verify_token")
return verify_fb_token(token_sent)
#if the request was not get, it must be POST and we can just proceed with sending a message back to user
else:
# get whatever message a user sent the bot
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for message in messaging:
if message.get('message'):
#Facebook Messenger ID for user so we know where to send response back to
recipient_id = message['sender']['id']
if message['message'].get('text'):
response_sent_text = get_message(recipient_id, message.get('message')['text'])
send_message(recipient_id, response_sent_text)
return redirect(url_for('pas2'))
#if user sends us a GIF, photo,video, or any other non-text item
if message['message'].get('attachments'):
response_sent_nontext = get_message_nontext(recipient_id, message.get('message'))
send_message(recipient_id, response_sent_nontext)
return "Message Processed"
@app.route("/login", methods = ['GET', 'POST'])
def pas2():
if request.method == 'GET':
"""Before allowing people to message your bot, Facebook has implemented a verify token
that confirms all requests that your bot receives came from Facebook."""
token_sent = request.args.get("hub.verify_token")
return verify_fb_token(token_sent)
else:
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for message in messaging:
if message.get('message'):
#Facebook Messenger ID for user so we know where to send response back to
recipient_id = message['sender']['id']
if message['message'].get('text'):
response_sent_text = get_message(recipient_id, 'pas2')
send_message(recipient_id, response_sent_text)
return 'OK'
这就是我现在拥有的东西,我对服务器端部分是陌生的。 谢谢。