heroku电报bot,BadRequest:错误的webhook:保留ip地址0.0.0.0



我创建了一个简单的电报机器人:

import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import os
PORT = int(os.environ.get('PORT', 8443))
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
TOKEN = 'YOURTELEGRAMBOTTOKEN'
# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi!')
def help(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 error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
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))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN)
updater.bot.setWebhook('https://yourherokuappname.herokuapp.com/' + TOKEN)
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()

当我在本地机器上运行它时,它工作得很好。当我根据这个链接编辑它以应用于heroku网络服务器时。然后给我一个错误,比如:telegram.error.BadRequest: Bad webhook: ip address 0.0.0.0 is reserved或者,如果我更改了端口,它会说,我只能使用443/8443/80端口。在之后

Stopping process with SIGKILL
Process exited with status 137

机器人不工作。我已经设置了Procfile,requirements.txt。我已经阅读了有关此错误的信息,但一无所获。当然,我设置了我的TOKEN和我的应用程序名称。有时,如果我修改我的端口,比如port=443,它会写:

telegram.error.BadRequest: Bad webhook: webhook can be set up only on ports 80, 88, 443 or 8443

并退出137错误状态。

端口的类似问题:无应答

尝试更改

updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN)
updater.bot.setWebhook('https://yourherokuappname.herokuapp.com/' + TOKEN)

updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN,
webhook_url = 'https://yourherokuappname.herokuapp.com/' + TOKEN)

start_webhook()函数已使用参数webhook_url调用set_webhook,这将导致您的错误。

请参阅:https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.updater.html

根据您的Python版本,您还需要添加:force_event_loop=True

您应该将第52行中的updater.start_webhook部分更改为:

updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN,
webhook_url='https://yourherokuappname.herokuapp.com/' + TOKEN)

最新更新