ข้ันตอนการใช้งานมีดังนี้
ทำการติดตั้ง flask และ pymessenger โดยใช้คำสั่งนี้
โค้ด: เลือกทั้งหมด
pip install Flask
pip install pymessenger
โค้ด: เลือกทั้งหมด
from flask import Flask, request
from pymessenger.bot import Bot
โค้ด: เลือกทั้งหมด
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def hello():
if request.method == 'GET':
if request.args.get("hub.verify_token") == VERIFY_TOKEN:
return request.args.get("hub.challenge")
else:
return 'Invalid verification token'
if __name__ == "__main__":
app.run(port=5002, debug=True)
จากนั้นให้ไปที่ เมนู My Apps ขั้นตอนต่อมา ให้ทำการ Set up ตัว messenger กับ webhooks มา ให้ไป ตั้งค่าต่างๆ ใน messenger กับ wedhooks
ไปที่ Access Tokens ทำการ add pages ถ้าไม่มีสามารถสร้างได้จากมุมขวาบน จากนั้นให้ทำการ add pages ที่สร้างมา จากนั้นไปที่ Webhooks ทำการนำ url มาใส่ จะต้องเป็น HTTPS เท่านั้น และตัว verify token เพื่อว่าตรงกันมั้ย
เมื่อทำทุกอย่างเสร็จแล้วให้เพิ่ม code ใน method hello ตามนี้
โค้ด: เลือกทั้งหมด
ACCESS_TOKEN = "<TOKEN pages>"
VERIFY_TOKEN = "<VERIFY_TOKEN>"
bot = Bot(ACCESS_TOKEN)
@app.route("/", methods=['GET', 'POST'])
def hello():
if request.method == 'POST':
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for x in messaging:
if x.get('message'):
recipient_id = x['sender']['id']
if x['message'].get('text'):
message = x['message']['text']
bot.send_text_message(recipient_id, message)
if x['message'].get('attachments'):
for att in x['message'].get('attachments'):
bot.send_attachment_url(recipient_id, att['type'], att['payload']['url'])
else:
pass
return "Success"
full code
โค้ด: เลือกทั้งหมด
from flask import Flask, request
from pymessenger.bot import Bot
app = Flask(__name__)
ACCESS_TOKEN = "EAATkd2BdopsBAHoEkqIB8Mk4qnA4YagiwKyvuRbkogItmd1PLMIT4MZAB5hlzGfeY5yLhZB13gZBydSWZA5gdvCS9hlBztbw1TZCco5sLZCyJAZAZCfUAblMlhiED04ye6xtZAQaF9cJiXBoHAjjN5KpZB1P9LCObFFDbYa2noC88PlldcbW9xmz3txZBby6bCyiIMZD"
VERIFY_TOKEN = "hello"
bot = Bot(ACCESS_TOKEN)
@app.route("/", methods=['GET', 'POST'])
def hello():
if request.method == 'GET':
if request.args.get("hub.verify_token") == VERIFY_TOKEN:
return request.args.get("hub.challenge")
else:
return 'Invalid verification token'
if request.method == 'POST':
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for x in messaging:
if x.get('message'):
recipient_id = x['sender']['id']
if x['message'].get('text'):
message = x['message']['text']
bot.send_text_message(recipient_id, message)
if x['message'].get('attachments'):
for att in x['message'].get('attachments'):
bot.send_attachment_url(recipient_id, att['type'], att['payload']['url'])
else:
pass
return "Success"
if __name__ == "__main__":
app.run(port=5002, debug=True)
https://developers.facebook.com/docs/messenger-platform/getting-started/app-setup
https://developers.facebook.com/docs/messenger-platform/getting-started/quick-start
https://stackoverflow.com/questions/53928278/facebook-messenger-chatbot-with-flask-and-pymessenger
https://developers.facebook.com/docs/graph-api/