连接服务器上的所有证书。注册了一个机器人。我从关闭的代码。文档并上传到服务器
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
import logging
from viberbot.api.viber_requests import ViberFailedRequest
from viberbot.api.viber_requests import ViberMessageRequest
from viberbot.api.viber_requests import ViberSubscribedRequest
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
app = Flask(__name__)
viber = Api(BotConfiguration(
name='имя',
avatar='https://dl-media.viber.com/5/share/2/long/vibes/icon/image/0x0/130b/87c64fe73f29f2601d0f2243c4be524f688e45be0a64abbc11d479fefc1b130b.jpg',
auth_token='токен'
))
viber.set_webhook("https://сайт:4443/bots/viber/")
@app.route('/', methods=['POST'])
def incoming():
logger.debug("received request. post data: {0}".format(request.get_data()))
# every viber message is signed, you can verify the signature using this method
if not viber.verify_signature(request.get_data(), request.headers.get('X-Viber-Content-Signature')):
return Response(status=403)
# this library supplies a simple way to receive a request object
viber_request = viber.parse_request(request.get_data())
if isinstance(viber_request, ViberMessageRequest):
message = viber_request.message
# lets echo back
viber.send_messages(viber_request.sender.id, [
message
])
elif isinstance(viber_request, ViberSubscribedRequest):
viber.send_messages(viber_request.get_user.id, [
TextMessage(text="thanks for subscribing!")
])
elif isinstance(viber_request, ViberFailedRequest):
logger.warn("client failed receiving message. failure: {0}".format(viber_request))
return Response(status=200)
if __name__ == "__main__":
context = ('server.crt', 'server.key')
app.run(host='0.0.0.0', port=443, debug=True, ssl_context=context)
我收到一个错误
2020-11-12 10:40:08,864 - requests.packages.urllib3.connectionpool - DEBUG - Starting new HTTPS connection (1): chatapi.viber.com
2020-11-12 10:40:14,244 - requests.packages.urllib3.connectionpool - DEBUG - https://chatapi.viber.com:443 "POST /pa/set_webhook HTTP/1.1" 200 224
Traceback (most recent call last):
File "webhook.py", line 24, in <module>
viber.set_webhook("https://мойсайт:4443/bots/viber/")
File "/usr/local/lib/python3.5/dist-packages/viberbot/api/api.py", line 29, in set_webhook
return self._request_sender.set_webhook(url, webhook_events, is_inline)
File "/usr/local/lib/python3.5/dist-packages/viberbot/api/api_request_sender.py", line 32, in set_webhook
raise Exception(u"failed with status: {0}, message: {1}".format(result['status'], result['status_message']))
Exception: failed with status: 1, message: Result[HttpRequest[POST /bots/viber/ HTTP/1.1]@1b2c34c4 > HttpResponse[null 0 null]@6b5df0a3] java.util.concurrent.TimeoutException: Total timeout 5000 ms elapsed
决定添加 viber.json 文件
{
"url": "мойсайт/bots/viber/"
}
并通过 curl 发出请求
curl -# -i -g -H "X-Viber-Auth-Token:токен" -d @viber.json -X POST https://chatapi.viber.com/pa/set_webhook -v
已收到
Warning: Couldn't read data from file "viber.json", this makes an empty POST.
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 52.44.100.16...
* TCP_NODELAY set
* Connected to chatapi.viber.com (52.44.100.16) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: CN=*.viber.com
* start date: Feb 13 00:00:00 2020 GMT
* expire date: Mar 13 12:00:00 2021 GMT
* subjectAltName: host "chatapi.viber.com" matched cert's "*.viber.com"
* issuer: C=US; O=Amazon; OU=Server CA 1B; CN=Amazon
* SSL certificate verify ok.
> POST /pa/set_webhook HTTP/1.1
> Host: chatapi.viber.com
> User-Agent: curl/7.52.1
> Accept: */*
> X-Viber-Auth-Token:token
> Content-Length: 0
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< cache-control: no-cache no-store
cache-control: no-cache no-store
< content-type: application/json
content-type: application/json
< date: Thu Nov 12 05:53:32 2020
date: Thu Nov 12 05:53:32 2020
< server-time: 78wWvHUBAAA=
server-time: 78wWvHUBAAA=
< Content-Length: 73
Content-Length: 73
< Connection: keep-alive
Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host chatapi.viber.com left intact
{"status":4,"status_message":"missingData","chat_hostname":"SN-CHAT-03_"}
我不明白问题是什么,为什么它不启动?
您的路由有错误。您正在尝试在站点上放置一个 webhook:4443/bots/viber,并且路由仅在站点上配置:4443 也许 viber 没有收到来自服务器的响应并引发错误
用。。。来代替