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.