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.