jsondune / dohmis

0 stars 0 forks source link

Facebook / Line ChatBot Course #14

Open jsondune opened 7 years ago

jsondune commented 7 years ago

how-to-fb-messenger.pdf

jsondune commented 7 years ago

git clone https://github.com/enginebai/PyMessager

git clone https://github.com/conbus/fbmq

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

jsondune commented 7 years ago

https://ngrok.com/download

jsondune commented 7 years ago

Page Access Token เช่น

EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD

เปลี่ยนได้ตลอด

jsondune commented 7 years ago

https://developers.facebook.com/docs/messenger-platform/send-api-reference/text-message

Test API using ARC text message

2017-08-26_14-04-01

{ "recipient":{ "id":"1630676673641502" }, "message":{ "text":"hello, world!" } }' "https://graph.facebook.com/v2.6/me/messages?access_token=EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD"

2017-08-26_14-02-25

jsondune commented 7 years ago

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" } } } }

2017-08-26_13-56-39

jsondune commented 7 years ago

https://jsonformatter.curiousconcept.com/

2017-08-26_14-19-52

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

jsondune commented 7 years ago

https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template

jsondune commented 7 years ago

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

jsondune commented 7 years ago

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)

jsondune commented 7 years ago

2017-08-26_14-57-45

jsondune commented 7 years ago

{ "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" } ] }] }

jsondune commented 7 years ago

https://graph.facebook.com/v2.6/me/messenger_profile?access_token=EAALTeTE8aOIBACIfIBjw3fnSZBPZA2P1Q6pZCGuWvEeBEIqyonJpNsv71DYXu66soCk6usAZBinZC1juttBunouZCFXgPhG1mbUcb4lUWUZBgmUHySnEQAJbzkQYVa5IWjaBCjIhhQOPlIcKOH1XwERWPRz9RhxiKeZCowfazBYBPQZDZD

{ "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" } ] }] }

2017-08-26_15-54-08

2017-08-26_15-54-37

jsondune commented 7 years ago

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

jsondune commented 7 years ago

CHATTERBOT

pip install chatterbot

http://chatterbot.readthedocs.io/en/stable/