跳转到主要内容

这是vxmessenger项目。

项目描述

Vumi Messenger传输

https://img.shields.io/travis/praekeltfoundation/vumi-messenger.svg https://img.shields.io/pypi/v/vxmessenger.svg Code Coverage vxmessenger Docs

使用Messenger传输,Vumi的所有应用程序都可以在Messenger上运行。它为大规模的交互式移动对话提供了良好的体验。

兼容性说明

请注意,在版本2.0.0中引入了对传输接口的重大更改。如果您的设置使用的是旧接口,您应使用早期版本(1.2.6或更早版本),因为这些更改不是向后兼容的。

入门指南

安装Junebug,独立的Vumi传输启动器和Facebook Messenger传输

$ apt-get install redis-server rabbitmq-server
$ pip install junebug
$ pip install vxmessenger

使用配置了Vumi Messenger通道的Junebug服务

$ jb -p 8000 \
    --channels facebook:vxmessenger.transport.MessengerTransport \
    --logging-path .

使用以下模板,并更新您的FB_PAGE_ID、FB_ACCESS_TOKEN,并将其保存为名为config.json的文件

{
    "type": "facebook",
    "label": "Sample Facebook Channel",
    "mo_url": "http://127.0.0.1:8000/channel/message",
    "status_url": "http://127.0.0.1:8000/channel/status",
    "public_http": {
        "enabled": true,
        "web_path": "/api",
        "web_port": 8051
    },
    "config": {
        "web_path": "/api",
        "web_port": 8051,
        "noisy": true,
        "page_id": "YOUR_FB_PAGE_ID",
        "retrieve_profile": false,
        "outbound_url": "https://graph.facebook.com/v2.8/me/messages",
        "access_token": "YOUR_FB_PAGE_ID",
        "redis_manager": {
    },
        "request_batch_size": 20,
        "request_batch_wait_time": 0.1
    }
}

消息传输使用了Facebook的批量API,通过一个HTTP请求发送多个操作指令。每个请求中要发送的操作数在request_batch_size字段中设置,而请求之间的等待时间在request_batch_wait_time字段中设置。批量大小最大为50(也是默认值),等待时间默认为0.1秒。

传输在发送请求之前使用Redis进行排队。如果您想自定义Redis设置(例如端口号),请在redis_manager字段中传递所需的配置作为JSON对象。传递一个空对象将导致传输使用默认配置。

将配置发布到Junebug以启动频道

$ curl -X POST -d@config.json http://localhost:8000/channels/

现在您可以使用Facebook的Messenger API并与您的Vumi应用程序提供机器人访问权限。

Facebook将希望验证您的应用程序,为此请确保它通过SSL提供服务。API URL为

http://localhost:8051/api

如果您使用了不同的web_portweb_path参数,则需要相应地更新URL。

将应用程序连接到您的消息传递集成

所有Vumi应用程序都可以作为机器人在Facebook Messenger上显示,为什么不尝试连接一个简单的猜谜游戏呢?

$ twistd -n vumi_worker \
    --worker-class=vumi.demos.hangman.HangmanWorker \
    --set-option=random_word_url:http://randomword.setgetgo.com/get.php \
    --set-option=transport_name:messenger_transport \
    --set-option=worker_name:hangman

JavaScript沙盒应用程序也是可用的。查看以下示例

示例FAQ浏览器

https://github.com/smn/faqbrowser-docker

示例服务评级应用程序

https://github.com/smn/servicerating-docker

丰富的模板

Vumi消息传输允许使用丰富的模板,包括文本、图像、超链接和按钮。

要使用这些功能,请将相关的helper_metadata添加到您的出站Vumi消息中

按钮回复

请注意Facebook对这些消息施加的限制。行动号召可能只能有最多3个按钮,并且字符计数限制适用。

self.publish_message(
    helper_metadata={
        'messenger': {
            'attachment': {
                'type': 'template',
                'payload': {
                    'template_type': 'button'
                    'text': 'The accompanying text with the button',
                    'buttons': [{ # Up to 3 buttons
                        'type': 'postback', # defaults to postback if not specified
                        'title': 'Button 1',
                        'payload': json.dumps({
                            # In here you can put whatever you want to
                            # 'content' and 'in_reply_to' will go into the standard vumi message
                            'content': 'The content expected when a button is pressed', # This can be left blank
                            'in_reply_to': 'The ID of the previous message', # This can be left blank
                            # Anything else will end up in transport_metadata.messenger and helper_metadata.messenger
                            'anything_extra': 'Bonus!'
                        })
                    }, {
                        'type': 'web_url',
                        'title': 'Button 2',
                        'url': 'http://some.url'
                    }, {
                        'type': 'phone_number',
                        'title': 'Button 3',
                        'url': '+271234567'
                    }]
                }
            }
        }
    })

通用回复

请注意Facebook对这些消息施加的限制。行动号召可能只能有最多3个按钮,10个元素,并且字符计数限制适用。

self.publish_message(
    helper_metadata={
        'messenger': {
            'attachment': {
                'type': 'template',
                'payload': {
                    'template_type': 'generic'
                    'elements': [{ # Up to 10 elements
                        'title': 'The title',
                        'subtitle': 'The subtitle', # This can be left blank
                        'image_url': 'The image_url to use', # This can be left blank
                        'item_url': 'The url if you click on the element', # Optional, Cannot be used with default_action
                        'default_action': { # The action if you click on element, optional, Cannot be user with item_url
                            'url': 'http://test',
                            'webview_height_ratio': 'compact|tall|full',  # Leave out for regular link
                            'messenger_extensions': True,  # If you are using the js extensions api, optional
                            'fallback_url': 'http://moo' # Fallback URL if target doesn't support js extensions
                                                     # will use url if not found, optional
                        },
                        'buttons': [{ # Up to 3 buttons
                            'type': 'postback', # defaults to postback if not specified
                            'title': 'Button 1',
                            'payload': json.dumps({
                                # In here you can put whatever you want to
                                # 'content' and 'in_reply_to' will go into the standard vumi message
                                'content': 'The content expected when a button is pressed', # This can be left blank
                                'in_reply_to': 'The ID of the previous message', # This can be left blank
                                # Anything else will end up in transport_metadata.messenger and helper_metadata.messenger
                                'anything_extra': 'Bonus!'
                            })
                        }, {
                            'type': 'web_url',
                            'title': 'Button 2',
                            'url': 'http://some.url'
                        }, {
                            'type': 'phone_number',
                            'title': 'Button 3',
                            'url': '+271234567'
                        }, {
                            # Share this post with friends. Only available in Generic Reply
                            'type': 'element_share'
                        }]
                    }]
                }
            }
        }
    })

列表回复

请注意Facebook对这些消息施加的限制。每个元素最多只能有1个按钮,2-4个元素,1个额外的按钮,并且字符计数限制适用。

self.publish_message(
    helper_metadata={
        'messenger': {
            'attachment': {
                'type': 'template',
                'payload': {
                    'template_type': 'list'
                    'top_element_style': 'compact|large', # Defines top element style
                                                          # Defaults to compact if not specified
                    'elements': [{ # 2 - 4 elements
                        'title': 'The title',
                        'subtitle': 'The subtitle', # This can be left blank
                        'image_url': 'The image_url to use', # This can be left blank
                        'default_action': { # The action if you click on element, optional
                            'url': 'http://test',
                            'webview_height_ratio': 'compact|tall|full',  # Leave out for regular link, optional
                            'messenger_extensions': True,  # If you are using the js extensions api, optional
                            'fallback_url': 'http://moo' # Fallback URL if target doesn't support js extensions
                                                     # will use url if not found, optional
                        },
                        'buttons': [{ # Up to 1 button
                            'type': 'postback', # defaults to postback if not specified
                            'title': 'Button 1',
                            'payload': json.dumps({
                                # In here you can put whatever you want to
                                # 'content' and 'in_reply_to' will go into the standard vumi message
                                'content': 'The content expected when a button is pressed', # This can be left blank
                                'in_reply_to': 'The ID of the previous message', # This can be left blank
                                # Anything else will end up in transport_metadata.messenger and helper_metadata.messenger
                                'anything_extra': 'Bonus!'
                            })
                        }, {
                            'type': 'web_url',
                            'title': 'Button 2',
                            'url': 'http://some.url'
                        }, {
                            'type': 'phone_number',
                            'title': 'Button 3',
                            'url': '+271234567'
                        }]
                    }],
                        'buttons': [{ # Up to 1 button at bottom of list
                        ...
                    }]
                }
            }
        }
    })

收据回复

请注意Facebook对这些消息施加的限制

self.publish_message(
    helper_metadata={
        'messenger': {
            'attachment': {
                'type': 'template',
                'payload': {
                    'template_type': 'receipt',
                    'order_number': '123', # Must be unique
                    'currency' 'ZAR'
                    'payment_method': 'EFT',
                    'recipient_name': 'John Doe',
                    'summary': {
                        'total_cost': 100.0,
                        # Optional fields
                        'subtotal': 40.0,
                        'total_tax': 10.0,
                        'shipping_cost': 10.00,
                    },
                    # Optional fields
                    'merchant_name': 'XYZ Inc.'
                    'order_url': 'http://example.com',
                    'timestamp': '12345678',
                    'elements': [
                        # Up to 100 elements
                        {
                            'title': 'element title',
                            'price': 10
                            # Optional fields
                            'subtitle': 'element subtitle',
                            'quantity': 1,
                            'currency': 'ZAR',
                            'image_url': 'http://example.com',
                        },
                    ],
                    'adjustments': [
                        {
                            'name': 'discount',
                            'amount': 1,
                        },
                    ],
                    'address': {
                        'street_1': '1 Main Street',
                        'street_2': 'Suburb' # This field is optional
                        'city': 'Cape Town',
                        'state': 'WC',
                        'country': 'RSA',
                        'postal_code': '1234',
                    },
                }
            }
        }
    })

快速回复

快速回复类似于按钮回复,但Messenger会暂时显示按钮,按下后它们会消失。您还可以有最多10个按钮。在此模式下仅允许postback按钮。

快速回复可以附加到任何消息格式。

self.publish_message(
    helper_metadata={
        'messenger': {
            ...
            'quick_replies': [{ # Up to 10 quick replies
                'type': 'text', # defaults to text if not specified
                'title': 'Button 1',
                'image_url': 'The image_url to use', # This can be left blank
                'payload': json.dumps({
                    # In here you can put whatever you want to
                    # 'content' and 'in_reply_to' will go into the standard vumi message
                    'content': 'The content expected when a button is pressed', # This can be left blank
                    'in_reply_to': 'The ID of the previous message', # This can be left blank
                    # Anything else will end up in transport_metadata.messenger and helper_metadata.messenger
                    'anything_extra': 'Bonus!'
                })
            }, {
                'type': 'location',
            }]
        }
    })

消息格式

由于即时通讯平台的某些额外功能,可能会有一些额外的数据需要您注意

transport_metadata

包含一个键为 messenger 的字典,具有以下键

mid:

即时通讯消息ID。

attachments:

包含此类字典的列表

{
    "type":"image",
    "payload": {
        "url":"IMAGE_URL"
    }
}
optin:

包含一个 ref 键的字典,该键是定义为

https://developers.facebook.com/docs/messenger-platform/plugin-reference#send_to_messenger

payload 中定义的其他项

例如 "anything_extra": "Bonus"

helper_metadata

包含一个键为 messenger 的字典,其数据与 transport_metadata 相同,并且包含用户资料

注意:只有当 retrieve_profile 配置为 true

{
    "first_name": "Firstname",
    "last_name": "Lastname",
    "profile_pic": "IMAGE_URL"
}

支持的网络钩子

messages:

标准对话消息和附件。

messaging_postbacks:

回传按钮。

messaging_optins:

发送到即时通讯/身份验证回调。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

vxmessenger-2.0.1.tar.gz (18.6 kB 查看散列)

上传时间

构建分布

vxmessenger-2.0.1-py2-none-any.whl (17.9 kB 查看散列)

上传时间 Python 2

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面