我创建了一个简单的 Telegram Bot :
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
最佳答案
尝试改变
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()
函数已经调用 set_webhook
与参数 webhook_url
这会导致您的错误。引用:https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.updater.html
根据您的 Python 版本,您还需要添加:
force_event_loop=True
关于python - heroku Telegram Bot ,BadRequest : Bad webhook: ip address 0. 0.0.0 被保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66920177/