PyMessager 是 Facebook Messenger 的 Python API,也是一個示範如何在 Facebook Messenger 上開發聊天機器人的範例專案。
完整的教學位於使用 Python 和聊天機器人開發 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 存取令牌初始化訊息用戶端:
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 | 按鈕標題 | 是 |
payload | 點擊負載字串 | 是 |
image_url | 圖標圖像 URL | 氮 |
content_type | TEXT 或LOCATION | 是 |
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 | 是 |
title | 按鈕標題 | 是 |
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 | 訊息主標題 | 是 |
subtitle_text | 訊息副標題,不需要則留空 | 氮 |
button_list | ActionButton 列表 | 是 |
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.