telegram-botaiohttptelegram-webhookpy-telegram-bot-api

I can't figure out, how to set up webhook on pyTelegramBotApi (telebot)


I've done exactly, like in this official example: https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/webhook_examples/webhook_aiohttp_echo_bot.py. Nothing seems to work, though. It doesn't crush either.

WEBHOOK_URL in format http://adress.io. WEBHOOK_PATH = '/'.

import telebot
    from aiohttp import web

    from config import *
    from messages import *

    bot = telebot.TeleBot(TOKEN)


    app = web.Application()
    bot.remove_webhook()
    bot.set_webhook(url=WEBHOOK_URL + WEBHOOK_PATH)

    async def handle(request):
        if request.match_info.get('token') == bot.token:
            request_body_dict = await request.json()
            update = telebot.types.Update.de_json(request_body_dict)
            bot.process_new_updates([update])
            return web.Response()
        else:
            return web.Response(status=403)

    # main loop
    if __name__ == '__main__':
        @bot.message_handler(content_types=['text'])
        def reply(message):
            if message.text == "/start":
                bot.send_message(message.from_user.id, MESSAGE_START)
            elif message.text == "/help":
                bot.send_message(message.from_user.id, MESSAGE_HELP)
            elif message.text == "/seeagreement":
                bot.send_message(message.from_user.id, MESSAGE_AGREEMENT)

        web.run_app(
            app,
            host=WEBHOOK_IP,
            port=WEBHOOK_PORT,
        )

Solution

  • I'm new to the Telegram API and telebot, but I saw this question doesn't have any answers, so I’m going to put what I think may help you find it out (or for whoever that faces this question).

    I decided to deploy my bot to Heroku and have done everything it wanted, but I got a weird error that 'TeleBot' doesn't have 'message_handler' attr, but no error for webhook found. So I’m just gonna explain what I did.

    I used CPython code instead of aiohttp that you used. Your code with some changes will become this (the changes aren't exactly the same with source code so take a look at it):

    import telebot
    from http.server import BaseHTTPRequestHandler, HTTPServer
    
    from config import *
    from messages import *
    
    bot = telebot.TeleBot(TOKEN)
    
    WEBHOOK_HOST = '<ip/host where the bot is running>'
    WEBHOOK_PORT = int(os.environ.get('PORT', 5000))
    WEBHOOK_LISTEN = '0.0.0.0'
    
    WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
    WEBHOOK_URL_PATH = "/%s/" % (TOKEN)
    
    async def handle(request):
        if request.match_info.get('token') == bot.token:
            request_body_dict = await request.json()
            update = telebot.types.Update.de_json(request_body_dict)
            bot.process_new_updates([update])
            return web.Response()
        else:
            return web.Response(status=403)
    
    # main loop
    if __name__ == '__main__':
        @bot.message_handler(content_types=['text'])
        def reply(message):
            if message.text == "/start":
                bot.send_message(message.from_user.id, MESSAGE_START)
            elif message.text == "/help":
                bot.send_message(message.from_user.id, MESSAGE_HELP)
            elif message.text == "/seeagreement":
                bot.send_message(message.from_user.id, MESSAGE_AGREEMENT)
    
        httpd = HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT), WebhookHandler)
        httpd.serve_forever()
    

    worked fine for me.

    PS: I'm going to switch to Telegram pkg (the main API) because of that error (and where I live, I can't use any foreign host and it's so expensive here. I am just going to stick to Heroku to see if it works)