Heroku上的Telegram机器人:“没有与主机名关联的地址”

时间:2019-01-24 22:58:18

标签: python heroku telegram-bot cherrypy

我正在尝试使用Cherrypy Webhooks在Heroku上部署简单的echo-bot。它返回HTTP 400错误请求:

  

telebot.apihelper.ApiException:对Telegram API的请求是   不成功。服务器返回HTTP 400错误请求。响应正文:

     

[b'{“ ok”:false,“ error_code”:400,“ description”:“ Bad Request:bad   webhook:无法解析主机:没有与之关联的地址   主机名“}']

代码如下:

import telebot
import cherrypy
import os

token = '<TOKEN>'
WEBHOOK_HOST = 'https://<APPNAME>.herokuapp.com/' + token
WEBHOOK_PORT = os.environ.get('PORT', 8443)
WEBHOOK_LISTEN = '0.0.0.0'
WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % token
bot = telebot.TeleBot(token)

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)

@bot.message_handler(func=lambda message: True)
def echo_message(message):
    bot.reply_to(message, message.text)

bot.remove_webhook()
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH)

cherrypy.config.update({
    'server.socket_host': WEBHOOK_LISTEN,
    'server.socket_port': WEBHOOK_PORT
})

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

0 个答案:

没有答案