skip to Main Content

I’m more than a day look how to deploy telegram bot with webhook instead of polling..

even in the official doc it’s not work for me https://github.com/python-telegram-bot/python-telegram-bot/wiki/Webhooks

someone can explain me oh give me some link to work tutorial how to deploy the most basic bot like this one

#!/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()

2

Answers


  1. I run my Telegram BOTs on Heroku and normally start the webhook then is set it:

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

    When deploying on Heroku you get a URL for your application using SSL (https://myapp.herokuapp.com), which you then configure via the BotFather.

    Providing the BotFather with a HTTPS url is a must, I wonder if you don’t do that or maybe there is an issue with the SSL certificate you are using (self-signed?)

    Login or Signup to reply.
  2. The problem is that the default port in App Engine is 8080
    And the webhook in telegram does not support this port (just ports 443, 80, 88, or 8443)
    https://core.telegram.org/bots/webhooks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search