gpt4 book ai didi

python - 电报机器人。 heroku 网络服务器上的 "Webhook can be set up only on ports 80, 88, 443 or 8443"错误

转载 作者:行者123 更新时间:2023-12-05 06:38:49 29 4
gpt4 key购买 nike

我正在尝试使用 webhook 和 cherrypy 服务器在 heroku 上部署 telegram simple echo bot。我的例子:

#!/usr/bin/python3.4
# -*- coding: utf-8 -*-
import telebot
import cherrypy
import os

TOKEN = 'mytoken'
WEBHOOK_HOST = 'quiet-springs-24190'
WEBHOOK_PORT = int(os.environ.get('PORT', 443)) # 443, 80, 88 или 8443 (port must be open!)
WEBHOOK_LISTEN = '0.0.0.0' # On some servers, you need write ip similar to webhook_host

WEBHOOK_SSL_CERT = './webhook_cert.pem' # path to certificate
WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # path to private key

WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (TOKEN)

bot = telebot.TeleBot(TOKEN)


# our webhook-server
class WebhookServer(object):
@cherrypy.expose
def index(self):
if 'content-length' in cherrypy.request.headers and \
'content-type' in cherrypy.request.headers and \
cherrypy.request.headers['content-type'] == 'application/json':
length = int(cherrypy.request.headers['content-length'])
json_string = cherrypy.request.body.read(length).decode("utf-8")
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return ''
else:
raise cherrypy.HTTPError(403)


# handl all text messages
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
bot.reply_to(message, message.text)

print(bot.get_webhook_info())

# remove webhook before adding
bot.remove_webhook()

bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH)

# CherryPy setting
cherrypy.config.update({
'server.socket_host': WEBHOOK_LISTEN,
'server.socket_port': WEBHOOK_PORT
# 'server.ssl_module': 'builtin',
# 'server.ssl_certificate': WEBHOOK_SSL_CERT,
# 'server.ssl_private_key': WEBHOOK_SSL_PRIV
})

# running
cherrypy.quickstart(WebhookServer(), WEBHOOK_URL_PATH, {'/': {}})

证书有注释,因为据我了解,heroku 有自己的证书,,,运行后,我在日志中有错误:

2017-08-06T14:55:46.022286+00:00 app[web.1]: telebot.apihelper.ApiException: A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
2017-08-06T14:55:46.022290+00:00 app[web.1]: [b'{"ok":false,"error_code":400,"description":"Bad Request: bad webhook: Webhook can be set up only on ports 80, 88, 443 or 8443"}']

但是我在 443 端口上运行我的服务器。
是否可以使用 webhook 在 Heroku 上部署 Telegram Bot?我该怎么做?

最佳答案

我可能来不及了。但我遇到了同样的问题,并且在测试其他端口后它起作用了。对我来说是 8443

关于python - 电报机器人。 heroku 网络服务器上的 "Webhook can be set up only on ports 80, 88, 443 or 8443"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45533321/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com