Pymessager是Facebook Messenger的Python API,也是一個示例項目,以演示如何在Facebook Messenger上開發聊天機器人。
完整的教程正在使用Python和Chatbot開發一個Facebook機器人:從0到1,您可以在其中找到更多詳細的信息進行設置和開發。
要安裝Pymessager,只需運行:
$ pip install pymessager
或從存儲庫安裝:
$ git clone [email protected]:enginebai/PyMessager.git
$ cd PyMessager
$ pip install -r requirements.txt
from pymessager . message import Messager , ... # something else you need
您可以通過開發人員控制台通過Facebook訪問令牌初始化Messager客戶端:
from pymessager . message import Messager
client = Messager ( config . facebook_access_token )
以下代碼用於構建消息接收器,有三個主要步驟可以為您的機器人準備:
@ app . route ( API_ROOT + FB_WEBHOOK , methods = [ "GET" ])
def fb_webhook ():
verification_code = 'I_AM_VERIFICIATION_CODE'
verify_token = request . args . get ( 'hub.verify_token' )
if verification_code == verify_token :
return request . args . get ( 'hub.challenge' )
@ app . route ( API_ROOT + FB_WEBHOOK , methods = [ 'POST' ])
def fb_receive_message ():
message_entries = json . loads ( request . data . decode ( 'utf8' ))[ 'entry' ]
for entry in message_entries :
for message in entry [ 'messaging' ]:
if message . get ( 'message' ):
print ( "{sender[id]} says {message[text]}" . format ( ** message ))
return "Hi"
if __name__ == '__main__' :
context = ( 'ssl/fullchain.pem' , 'ssl/privkey.pem' )
app . run ( host = '0.0.0.0' , debug = True , ssl_context = context )
消息有幾種類型: text
, image
, quick replies
, button template
或generic template
。 API提供不同的類來生成消息模板。
將簡單的文本或圖像發送給收件人,只需確保圖像URL是有效的鏈接即可。
client . send_text ( user_id , "Hello, I'm enginebai." )
client . send_image ( user_id , "http://image-url.jpg" )
QuickReply(title, payload, image_url, content_type)
類針對用戶定義了當前按鈕,以響應消息。
範圍 | 描述 | 必需的 |
---|---|---|
title | 按鈕標題 | y |
payload | 點擊有效負載字符串 | y |
image_url | 圖像圖像URL | n |
content_type | TEXT 或LOCATION | y |
client . send_quick_replies ( user_id , "Help" , [
QuickReply ( "Projects" , Intent . PROJECT ),
QuickReply ( "Blog" , Intent . BLOG ),
QuickReply ( "Contact Me" , Intent . CONTACT_ME )
])
ActionButton(button_type, title, url, payload)
類定義按鈕模板,其中包含文本和按鈕附件以輸入用戶的請求輸入。
範圍 | 描述 | 必需的 |
---|---|---|
button_type | WEB_URL 或POSTBACK | y |
title | 按鈕標題 | y |
url | 鏈接 | 只有button_type 是url |
payload | 點擊有效負載字符串 | 只有button_type 是POSTBACK |
client . send_buttons ( user_id , "You can find me with below" , [
ActionButton ( ButtonType . WEB_URL , "Blog" , "http://blog.enginebai.com" ),
ActionButton ( ButtonType . POSTBACK , "Email" , Intent . EMAIL )
])
GenericElement(title, subtitle, image_url, buttons)
類定義了一個水平滾動的項目,每個旋轉旋轉木旋的旋轉木製由圖像附件,簡短描述和用戶請求輸入的按鈕組成。
範圍 | 描述 | 必需的 |
---|---|---|
title_text | 消息主要標題 | y |
subtitle_text | 消息字幕,如果您不需要 | n |
button_list | ActionButton 的清單 | y |
project_list = []
for project_id , project in projects . items ():
project_list . append ( GenericElement (
project [ "title" ],
project [ "description" ],
config . api_root + project [ "image_url" ], [
ActionButton ( ButtonType . POSTBACK ,
self . _get_string ( "button_more" ),
# Payload use Intent for the beginning
payload = Intent . PROJECTS . name + project_id )
]))
client . send_generic ( user_id , project_list )
在聊天機器人開始接收消息之前,您必須將應用程序訂閱到聊天機器人頁面。要訂閱頁面,請調用它:
client . subscribe_to_page ()
問候文本將首次顯示您僅在移動設備上打開此聊天機器人。當用戶單擊“啟動”按鈕時,有效負載是觸發器。
client . set_greeting_text ( "Hi, this is Engine Bai. Nice to meet you!" )
client . set_get_started_button_payload ( "HELP" ) # Specify a payload string.
請隨時提交錯誤報告或功能請求,並確保在打開任何問題之前閱讀貢獻指南。
feature
/ bug
)的功能請求或錯誤報告。閱讀更多有關貢獻的信息。
The MIT License (MIT)
Copyright © 2017 Engine Bai.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.