使用Webhook部署电报

问题描述

我不止一天地研究如何使用webhook而不是轮询来部署电报机器人。

即使在正式文档中,它对我也不起作用https://github.com/python-telegram-bot/python-telegram-bot/wiki/Webhooks

有人可以向我解释哦,给我一些工作教程的链接,介绍如何部署像这样的最基本的机器人

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from telegram.ext import Updater,CommandHandler,MessageHandler,Filters

def start(update,context):
    """Send a message when the command /start is issued."""
    update.message.reply_text('Hi!')


def help_command(update,context):
    """Send a message when the command /help is issued."""
    update.message.reply_text('Help!')


def echo(update,context):
    """Echo the user message."""
    update.message.reply_text(update.message.text)


def main():

    TOKEN = "telegramToken"

    updater = Updater(TOKEN,use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start",start))
    dp.add_handler(CommandHandler("help",help_command))

    dp.add_handler(MessageHandler(Filters.text & ~Filters.command,echo))
    PORT = int(os.environ.get('PORT','5000'))
    SERVER = 'myipserver/'
    CERT = 'cert.pem'
    updater.bot.setWebhook(SERVER + TOKEN,certificate=open(CERT,'rb'))
    # updater.bot.setWebhook(SERVER + TOKEN) also dont working
    updater.start_webhook(listen="0.0.0.0",port=PORT,url_path=TOKEN)

    # updater.start_polling()

    updater.idle()

if __name__ == '__main__':
    main()

解决方法

我在Heroku上运行我的Telegram BOT,并通常启动webhook,然后对其进行设置:

updater.start_webhook(listen="0.0.0.0",port=int(os.environ.get("PORT",5000)),url_path='token'
updater.bot.setWebhook("https://myapp.com/token")

updater.idle()

在Heroku上进行部署时,您会使用SSL(https://myapp.herokuapp.com)获得应用程序的URL,然后通过BotFather对其进行配置。

必须为BotFather提供HTTPS URL,我想知道您是否不这样做,或者您使用的SSL证书是否存在问题(自签名?)