skip to Main Content

I create a simple 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()

When I run it in local machine it works fine. When I edit it to apply on heroku web server according this link. Then it’s give me error like:
telegram.error.BadRequest: Bad webhook: ip address 0.0.0.0 is reserved
Or, if I changed port then it says, I can use only 443 / 8443 / 80 port.
After that

Stopping process with SIGKILL
Process exited with status 137

And bot not working. I have already setup Procfile, requirements.txt. I have read about this error and find nothing.
Of course, I set up my TOKEN, and my app name.
Sometimes, if I modify my port like PORT = 443 It writes:

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

And exit 137 error status.

Similar problem with port: without answer

2

Answers


  1. Try changing

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

    to

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

    The start_webhook() function already calls set_webhook with the argument webhook_url which results in your error.

    Refer to: https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.updater.html

    Depending on your Python version you will also need to add: force_event_loop=True

    Login or Signup to reply.
  2. You should change updater.start_webhook section in line 52 to:

    updater.start_webhook(listen="0.0.0.0",
                          port=int(PORT),
                          url_path=TOKEN,
                          webhook_url='https://yourherokuappname.herokuapp.com/' + TOKEN)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search