Viber Bot 不启动
Viber Bot does not start
我 运行 我的 Viber 机器人通过 gunicorn
和 flask
在服务器上。但是当我做 curl
请求时我得到错误
* Curl_http_done: called premature == 0
* Connection #0 to host chatapi.viber.com left intact
{"status":1,"status_message":"Result[HttpRequest[POST / HTTP/1.1]@3f742f36 > HttpResponse[HTTP/1.1 500 INTERNAL SERVER ERROR]@16f0ed02] null","chat_hostname":"SN-CHAT-01_"}
当我打开 url 我的网站时,我有
Method Not Allowed
The method is not allowed for the requested URL.
app.py
from flask import Flask, request, Response
from viberbot import Api
from viberbot.api.bot_configuration import BotConfiguration
from viberbot.api.messages import KeyboardMessage
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
from keyboard import keyboard_menu
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='name',
avatar='https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg',
auth_token='token'
))
@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 = TextMessage(text="test", keyboard=keyboard_menu)
# 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)
wsgi.py
from app import app
if __name__ == "__main__":
app.run()
curl
curl -# -i -g -H "X-Viber-Auth-Token:token" -d @viber.json -X POST https://chatapi.viber.com/pa/set_webhook -v
viber.json
{
"url": "https://my.site.com"
}
我做错了什么?
尝试向该端点发送 POST 请求,就像 Viber 服务器那样,但使用 cURL
。所以您将能够看到错误。
或更新根记录器以查看 500 错误的原因。建议像 documentation (see here 中那样配置以获取更多详细信息):
from logging.config import dictConfig
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
app = Flask(__name__)
最后认为:
when I open url my site I have
Method Not Allowed
The method is not allowed for the requested URL.
那是因为当您在浏览器中打开 url 时,它会发送 GET 请求,但端点仅接受 POST
http 方法。
我 运行 我的 Viber 机器人通过 gunicorn
和 flask
在服务器上。但是当我做 curl
请求时我得到错误
* Curl_http_done: called premature == 0
* Connection #0 to host chatapi.viber.com left intact
{"status":1,"status_message":"Result[HttpRequest[POST / HTTP/1.1]@3f742f36 > HttpResponse[HTTP/1.1 500 INTERNAL SERVER ERROR]@16f0ed02] null","chat_hostname":"SN-CHAT-01_"}
当我打开 url 我的网站时,我有
Method Not Allowed
The method is not allowed for the requested URL.
app.py
from flask import Flask, request, Response
from viberbot import Api
from viberbot.api.bot_configuration import BotConfiguration
from viberbot.api.messages import KeyboardMessage
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
from keyboard import keyboard_menu
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='name',
avatar='https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg',
auth_token='token'
))
@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 = TextMessage(text="test", keyboard=keyboard_menu)
# 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)
wsgi.py
from app import app
if __name__ == "__main__":
app.run()
curl
curl -# -i -g -H "X-Viber-Auth-Token:token" -d @viber.json -X POST https://chatapi.viber.com/pa/set_webhook -v
viber.json
{
"url": "https://my.site.com"
}
我做错了什么?
尝试向该端点发送 POST 请求,就像 Viber 服务器那样,但使用 cURL
。所以您将能够看到错误。
或更新根记录器以查看 500 错误的原因。建议像 documentation (see here 中那样配置以获取更多详细信息):
from logging.config import dictConfig
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
app = Flask(__name__)
最后认为:
when I open url my site I have
Method Not Allowed The method is not allowed for the requested URL.
那是因为当您在浏览器中打开 url 时,它会发送 GET 请求,但端点仅接受 POST
http 方法。