Last active
November 4, 2018 09:23
-
-
Save RiceeeChang/11a48b238c043fa40ded1508511ff467 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def call_send_api(sender_psid, response): | |
data = { | |
"messaging_type": "RESPONSE", | |
"recipient": { | |
"id": sender_psid | |
}, | |
"message": response | |
} | |
try: | |
resp = requests.post(API_URL + API_VERSION + | |
"/me/messages?access_token=" + PAGE_ACCESS_TOKEN, json=data) | |
except Exception as e: | |
logging.error('Unable to send message:' + e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"title":"Facebook Messenger 聊天機器人實作班", | |
"image_url":"https://i.imgur.com/Ig9vfRn.jpg", | |
"subtitle":"課程將手把手教你使用 Python 建立自己的第一個聊天機器人", | |
"default_action": { | |
"type": "web_url", | |
"url": "https://tenlong.kktix.cc/events/facebook-messenger-chatbot-101", | |
}, | |
"buttons": [ # 最多放三個按鈕 | |
{ | |
"type": "web_url", | |
"url": "https://www.pycone.com/", | |
"title": "松果城市網站", | |
}, | |
{ | |
"type": "postback", | |
"title": "主選單", | |
"payload": "menu" | |
} | |
] | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"payload": { | |
"template_type":"generic", | |
"elements":[ | |
{ | |
"title":"<TITLE_TEXT>", | |
"image_url":"<IMAGE_URL_TO_DISPLAY>", | |
"subtitle":"<SUBTITLE_TEXT>", | |
"default_action": { | |
"type": "web_url", | |
"url": "<DEFAULT_URL_TO_OPEN>", | |
"messenger_extensions": <TRUE | FALSE>, | |
"webview_height_ratio": "<COMPACT | TALL | FULL>" | |
}, | |
"buttons":[<BUTTON_OBJECT>, ...] | |
}, | |
... | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"payload": { | |
"template_type": "generic", | |
"elements": [ # 陣列裡面最多放十個項目 | |
{ | |
"title":"Facebook Messenger 聊天機器人實作班", | |
"image_url":"https://i.imgur.com/Ig9vfRn.jpg", | |
"subtitle":"課程將手把手教你使用 Python 建立自己的第一個聊天機器人", | |
"default_action": { | |
"type": "web_url", | |
"url": "https://tenlong.kktix.cc/events/facebook-messenger-chatbot-101", | |
}, | |
"buttons": [ # 最多放三個按鈕 | |
{ | |
"type": "web_url", | |
"url": "https://www.pycone.com/", | |
"title": "松果城市網站", | |
}, | |
{ | |
"type": "postback", | |
"title": "主選單", | |
"payload": "menu" | |
} | |
] | |
}, | |
{ | |
"title":"天瓏書局", | |
"image_url":"https://blog.gcp.expert/material/2018/08/%E5%A4%A9%E7%93%8F.png", | |
"subtitle":"最齊全的電腦書專賣店,天瓏提供專業電腦中文書、英文書、簡體書、電子開發板", | |
"default_action": { | |
"type": "web_url", | |
"url": "https://www.tenlong.com.tw/", | |
}, | |
"buttons": [ | |
{ | |
"type": "web_url", | |
"url": "https://www.tenlong.com.tw/", | |
"title": "天瓏書局", | |
}, | |
{ | |
"type": "web_url", | |
"url": "https://www.pycone.com/", | |
"title": "松果城市網站", | |
}, | |
{ | |
"type": "postback", | |
"title": "主選單", | |
"payload": "menu" | |
} | |
] | |
}, | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"attachment": { | |
"type": "image", | |
"payload": { | |
"url":"https://i.imgur.com/AcipQzu.png", | |
"is_reusable":true | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"payload": { | |
"template_type": "list", | |
"top_element_style": "<LARGE | COMPACT>", | |
"elements": [ | |
{ | |
"title": "<TITLE_TEXT>", | |
"subtitle": "<SUBTITLE_TEXT>", | |
"image_url": "<IMAGE_URL_FOR_THUMBNAIL>", | |
"buttons": [<BUTTON_OBJECT>], | |
"default_action": { | |
"type": "web_url", | |
"url": "<URL_TO_OPEN_WHEN_ITEM_IS_TAPPED>", | |
"messenger_extensions": <TRUE | FALSE>, | |
"webview_height_ratio": "<COMPACT | TALL | FULL>" | |
} | |
}, | |
... | |
], | |
"buttons": [<BUTTON_OBJECT>] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"message": { | |
"text": <TEXT>, | |
"attachment": { | |
"type": <TYPE>, | |
"payload": <PAYLOAD> | |
}, | |
"quick_reply": <QUICK_REPLY> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"sender":{ | |
"id":"<PSID>" | |
}, | |
"recipient":{ | |
"id":"<PAGE_ID>" | |
}, | |
"timestamp":1458692752478, | |
"postback":{ | |
"title": "<TITLE_FOR_THE_CTA>", | |
"payload": "<USER_DEFINED_PAYLOAD>", | |
"referral": { | |
"ref": "<USER_DEFINED_REFERRAL_PARAM>", | |
"source": "<SHORTLINK>", | |
"type": "OPEN_THREAD", | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"messaging_type": <MESSAGING_TYPE>, | |
"recipient": { | |
"id": <SENDER_PSID> | |
}, | |
"message": <MESSAGE>, | |
"sender_action": <SENDER_ACTION>, | |
"notification_type": <REGULAR> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = { | |
"get_started": {"payload": "menu"}, | |
"greeting": [ | |
{ | |
"locale": "default", | |
"text": "多元訊息範例" | |
} | |
], | |
"persistent_menu": [ | |
{ | |
'locale': 'default', | |
'call_to_actions': [ | |
{ | |
'title': '範例列表', | |
'type': 'postback', | |
'payload': 'menu' | |
} | |
] | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "postback", | |
"title": "主選單", | |
"payload": "menu" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"quick_reply": [ | |
{ | |
"content_type": "text", | |
"title": "圖片(image)", | |
"image_url": "https://i.imgur.com/Ig9vfRn.jpg", | |
"payload": "image", | |
}, | |
{ | |
"content_type":"location" | |
}, | |
{ | |
"content_type":"user_phone_number" | |
}, | |
{ | |
"content_type":"user_email" | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resp = requests.post(API_URL + API_VERSION + | |
"/me/messenger_profile?access_token=" + PAGE_ACCESS_TOKEN, json=data) | |
data = { | |
"get_started": {"payload": "greeting"}, | |
"greeting": [ | |
{ | |
"locale": "default", | |
"text": "Welcome to the Pugbus" | |
}, | |
{ | |
"locale": "zh_TW", | |
"text": "歡迎搭乘Pugbus" | |
} | |
], | |
"persistent_menu": [ | |
{ | |
'locale': 'default', | |
'call_to_actions': [ | |
{ | |
'title': 'Pugbus Menu', | |
'type': 'postback', | |
'payload': 'menu' | |
}, | |
{ | |
"title": "Meet Taipei Page", | |
"type": "web_url", | |
"url": "https://meettaipei.tw/" | |
} | |
] | |
}, | |
{ | |
'locale': 'zh_TW', | |
'call_to_actions': [ | |
{ | |
"title": "Pugbus 主選單", | |
'type': 'postback', | |
'payload': 'menu' | |
}, | |
{ | |
"type": "web_url", | |
"title": "Meet Taipei 主頁", | |
"url": "https://meettaipei.tw/" | |
} | |
] | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"attachment": { | |
"type": "template", | |
"payload": { | |
"template_type": <TEMPLATE_TYPE>, | |
... | |
... | |
... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if 'message' in webhook_event: | |
if 'quick_reply' in webhook_event['message']: | |
if 'payload' in webhook_event['message']['quick_reply']: | |
postback = webhook_event['message']['quick_reply'] | |
handle_postback(sender_psid, postback) | |
else: | |
message = webhook_event['message'] | |
handle_message(sender_psid, message) | |
elif 'postback' in webhook_event: | |
postback = webhook_event['postback'] | |
handle_postback(sender_psid, postback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment