Open jsondune opened 7 years ago
ติดตั้ง python และกำหนด PATH environment http://python.org
ติดตั้ง pip
ติดตั้ง curl หรือ POSTMAN Chrome extension หรือ Advanced REST Client Chrome extension
ติดตั้ง virtualenv
ดาวน์โหลดซอร์สโค้ดจาก github.com https://github.com/conbus/fbmq https://github.com/enginebai/PyMessager
cd fbmq/example
Mac: virtualenv env source env/bin/activate Windows: cd fbmq/example env/scripts/bin/activate.bat
pip install -r requirements.txt python server.py
Page Access Token เช่น
EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD
เปลี่ยนได้ตลอด
https://developers.facebook.com/docs/messenger-platform/send-api-reference/text-message
Test API using ARC text message
{ "recipient":{ "id":"1630676673641502" }, "message":{ "text":"hello, world!" } }' "https://graph.facebook.com/v2.6/me/messages?access_token=EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD"
https://developers.facebook.com/docs/messenger-platform/send-api-reference/file-attachment
{
"recipient":{
"id":"1630676673641502"
},
"message":{
"attachment":{
"type":"file",
"payload":{
"url":"https://68.media.tumblr.com/tumblr_ly41xqlqEF1qdiwj3o1_400.jpg"
}
}
}
}
https://jsonformatter.curiousconcept.com/
sender.py
import requests import json
PAGE_ACCESS_TOKEN = "EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD"
fb_api = "https://graph.facebook.com/v2.6" fb_url = fb_api + "/me/messages?access_token={}".format(PAGE_ACCESS_TOKEN)
data = { "recipient":{ "id":"1630676673641502" }, "message":{ "attachment":{ "type":"template", "payload":{ "template_type":"button", "text":"What do you want to do next?", "buttons":[ { "type":"web_url", "url":"https://www.messenger.com", "title":"Visit Messenger" }, { ... }, {...} ] } } } } headers = { 'content-type': 'application/json' }
r = requests.post(fb_url, headers=headers, data = json.dumps(data)) print r.text
Button Templates
import requests import json
PAGE_ACCESS_TOKEN = "EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD"
fb_api = "https://graph.facebook.com/v2.6" fb_url = fb_api + "/me/messages?access_token={}".format(PAGE_ACCESS_TOKEN)
data = { "recipient":{ "id":"1630676673641502" }, "message":{ "attachment":{ "type":"template", "payload":{ "template_type":"button", "text":"What do you want to do next?", "buttons":[ { "type":"web_url", "url":"https://www.messenger.com", "title":"Visit Messenger" }, { "type":"web_url", "url":"https://www.messenger.com", "title":"Visit Messenger" }, { "type":"web_url", "url":"https://www.messenger.com", "title":"Visit Messenger" },] } } } } headers = { 'content-type': 'application/json' }
r = requests.post(fb_url, headers=headers, data = json.dumps(data)) print r.text
profile.py
import requests import json
import requests import json
PAGE_ACCESS_TOKEN = "EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD"
fb_uid = '1630676673641502' fb_api = "https://graph.facebook.com/v2.6" fb_fields = 'first_name, last_name, profile_pic, gender, locale' fb_url = fb_api + "/{uid}?fields={fields}&access_token={token}".format(uid=fb_uid,fields=fb_fields,token=PAGE_ACCESS_TOKEN)
r = requests.get(fb_url) print json.dumps(json.loads(r.text), indent=4)
{ "persistent_menu":[ { "locale":"default", "composer_input_disabled": true, "call_to_actions":[ { "title":"My Account", "type":"nested", "call_to_actions":[ { "title":"Pay Bill", "type":"postback", "payload":"PAYBILL_PAYLOAD" }, { "title":"History", "type":"postback", "payload":"HISTORY_PAYLOAD" }, { "title":"Contact Info", "type":"postback", "payload":"CONTACT_INFO_PAYLOAD" } ] }, { "type":"web_url", "title":"Latest News", "url":"http://petershats.parseapp.com/hat-news", "webview_height_ratio":"full" } ] }] }
{ "persistent_menu":[ { "locale":"default", "composer_input_disabled": true, "call_to_actions":[ { "title":"My Account", "type":"nested", "call_to_actions":[ { "title":"Pay Bill", "type":"postback", "payload":"PAYBILL_PAYLOAD" }, { "title":"History", "type":"postback", "payload":"HISTORY_PAYLOAD" }, { "title":"Contact Info", "type":"postback", "payload":"CONTACT_INFO_PAYLOAD" } ] }, { "type":"web_url", "title":"Latest News", "url":"http://petershats.parseapp.com/hat-news", "webview_height_ratio":"full" } ] }] }
import requests import json
PAGE_ACCESS_TOKEN = "EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD"
fb_api = "https://graph.facebook.com/v2.6" fb_url = fb_api + "/me/messenger_codes?access_token={}".format(PAGE_ACCESS_TOKEN)
data = {
"type":"standard",
"image_size": 1000
}
headers = {
'content-type': 'application/json'
}
r = requests.post(fb_url, headers=headers, data = json.dumps(data)) print r.text
how-to-fb-messenger.pdf