从 Viber 机器人向订阅用户发送消息
Posted
技术标签:
【中文标题】从 Viber 机器人向订阅用户发送消息【英文标题】:Send message from Viber bot to subscribed user 【发布时间】:2019-08-06 04:57:45 【问题描述】:我正在尝试将消息从 Viber 机器人发送给订阅用户。我可以获得订阅的用户 ID,但是当我发送消息时,我收到 500 错误。
from flask import Flask, request, Response
from viberbot import Api
from viberbot.api.bot_configuration import BotConfiguration
from viberbot.api.messages.text_message import TextMessage
app = Flask(__name__)
viber = Api(BotConfiguration(
name='PythonSampleBot',
avatar='http://www.clker.com/cliparts/3/m/v/Y/E/V/small-red-apple-hi.png',
auth_token='xxx-xxx-xxx'
))
@app.route('/', methods=['POST'])
def incoming():
user_id = viber.get_account_info()['members'][0]['id']
print(user_id)
viber.send_messages(user_id, [
TextMessage(text="thanks for subscribing!!!!!")
])
return Response(status=200)
if __name__ == "__main__":
context = ('E:\\Docs\\learn_py\\viberbot\\cert.pem',
'E:\\Docs\\learn_py\\viberbot\\key.pem')
app.run(host='0.0.0.0', port=4443, debug=True, ssl_context=context)
请求消息发送代码:
import json
import requests
webhook_url = 'https://xxx.xxx.xxx.xxx:4443'
requests.post(
webhook_url, data=json.dumps("text": "Hello World"),
headers='Content-Type': 'application/json',
verify='E:\\Docs\\learn_py\\viberbot\\cert.pem'
)
错误信息:
Cfklv9HOJ6bXZcHMaTl9Gw==
192.168.1.1 - - [15/Mar/2019 08:44:02] "POST / HTTP/1.1" 500 - Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response) File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e) File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb) File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request() File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e) File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb) File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request() File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args) File "E:\Docs\learn_py\viberbot\app.py", line 21, in incoming
TextMessage(text="thanks for subscribing!!!!!") File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\viberbot\api\api.py", line 72, in send_messages
to, self._bot_configuration.name, self._bot_configuration.avatar, message, chat_id) File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\viberbot\api\message_sender.py", line 27, in send_message
return self._post_request(BOT_API_ENDPOINT.SEND_MESSAGE, payload) File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\viberbot\api\message_sender.py", line 53, in _post_request
raise Exception(u"failed with status: 0, message: 1".format(result['status'], result['status_message'])) Exception: failed with status: 10, message: webhookNotSet * Detected change in 'E:\\Docs\\learn_py\\viberbot\\app.py', reloading
更新:根据 mingaleg 的建议,我已将 viber.set_webhook('https://xxx.xxx.xxx.xxx:yyyy')
添加到 incoming()
函数中,现在又出现了另一个错误:
Exception: failed with status: 1, message: Result[HttpRequest[POST / HTTP/1.1]@2c67393 > HttpResponse[null 0 null]@72391ae] javax.net.ssl.SSLHandshakeException: General SSLEngine problem
证书是按照this answer中的建议生成的:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
【问题讨论】:
您不应该使用自签名证书。我可以推荐 LetsEncrypt 服务,因为它可以方便且免费地为您的域获取 SSL 证书。 或者你可以使用ngrok进行本地开发或部署到Heroku(它免费提供HTTPS域)。 【参考方案1】:由于您有一个webhookNotSet
错误消息,您应该将您的机器人配置为有一个:
...
viber = Api(BotConfiguration(
name='PythonSampleBot',
avatar='http://www.clker.com/cliparts/3/m/v/Y/E/V/small-red-apple-hi.png',
auth_token='xxx-xxx-xxx'
))
viber.set_webhook(webhook_url)
...
webhook_url
应该是您的烧瓶服务器可以访问的那个。
【讨论】:
异常:失败,状态:1,消息:结果[HttpRequest[POST HTTP/1.1]@6992828d > HttpResponse[null 0 null]@554955e3] java.util.concurrent.TimeoutException:总超时已过 您应该提供可从互联网访问的webhook_url
,尽管它看起来不像
如果我在您提供的位置设置了 webhook,那么它会尝试在服务器启动之前设置钩子。我已经将它作为第一个语句移到了 incominng() 函数(什么是正确的位置?无论如何,在我移到那里后,我遇到了证书错误:异常:状态失败:1,消息:结果 [HttpRequest [POST /HTTP/1.1]@7ba1c887 > HttpResponse[null 0 null]@5c94ba77] javax.net.ssl.SSLHandshakeException: 一般 SSLEngine 问题
@DmitrijKultasev 您不应该使用自签名证书。我可以推荐LetsEncrypt
服务,因为它可以方便且免费地为您的域获取 SSL 证书。【参考方案2】:
您不应该使用自签名证书。 我可以推荐Let's Encrypt 服务,因为它可以方便且免费地为您的域获取 SSL 证书。
或者您可以使用ngrok进行本地开发或部署到Heroku(它免费提供HTTPS域)。
【讨论】:
以上是关于从 Viber 机器人向订阅用户发送消息的主要内容,如果未能解决你的问题,请参考以下文章
从 Viber bot 向 botbuilder-viber 发送消息时出现错误 ERR_INVALID_ARG_TYPE