TelegramBot。 “在Heroku网络服务器上只能在端口80,88,443或8443上设置Webhook”错误

时间:2017-08-06 15:05:35

标签: python heroku telegram-bot

我正在尝试使用webhook和cherrypy server在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吗?我怎么能这样做?

0 个答案:

没有答案