如何使用SSl为Telegram Bot创建Webhook?

时间:2019-07-29 11:56:58

标签: python telegram-bot telegram-webhook

我想为Telegram bot制作Webhook,我有一个SSL证书和密钥。当我启动程序并将文本发送到我的机器人时,我什么也没有。也许我没有正确的主持人或做我不应该做的事。我的主要问题是如何获取主机,如何设置webhook以及如何在Windows中查看iptable(Linux),最后一个问题是lsof -i | grep python3.6如何在cmd(windows)中编写

我试图用电报setWebhook做到这一点,但无济于事。我也尝试了程序ngrok。但是我无法进入程序

import telebot
import logging
import cherrypy




WEBHOOK_HOST = '127.0.0.1'# I don't know where take it
WEBHOOK_PORT = 443  # 443, 80, 88, 8443
WEBHOOK_LISTEN = '0.0.0.0'

WEBHOOK_SSL_CERT = 'server.crt'  # certificate path
WEBHOOK_SSL_PRIV = 'server.key'

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

logger = telebot.logger
telebot.logger.setLevel(logging.INFO)


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(commands=['start'])
def send_welcome(message):
    bot.send_message(message.chat.id,"Привет")

bot.remove_webhook()
access_log = cherrypy.log.access_log


bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH, certificate=open(WEBHOOK_SSL_CERT, 'r'))
for handler in tuple(access_log.handlers):
    access_log.removeHandler(handler)
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
})
cherrypy.quickstart(WebhookServer(), WEBHOOK_URL_PATH, {'/': {}})

0 个答案:

没有答案