【问题标题】:heroku telegram bot, BadRequest: Bad webhook: ip address 0.0.0.0 is reservedheroku 电报机器人,BadRequest:Bad webhook:保留 IP 地址 0.0.0.0
【发布时间】:2021-06-29 09:01:51
【问题描述】:

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

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()

当我在本地机器上运行它时,它工作正常。当我根据this link 编辑它以应用于heroku Web 服务器时。然后它给我这样的错误: 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错误状态。

与端口类似的问题:without answer

【问题讨论】:

    标签: python heroku telegram


    【解决方案1】:

    尝试改变

    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

    【讨论】:

    • 工作就像一个魅力。谢谢。
    【解决方案2】:

    您应该将第 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)
    

    【讨论】:

    • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2017-05-10
    • 2021-03-29
    • 2015-11-01
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多