1

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,
        )
Tibebes. M
  • 6,940
  • 5
  • 15
  • 36

1 Answers1

0

I'm a newbie to telegram api and telebot, but i saw this question has no answers so im gonna 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, done everything it wanted but 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. Hope it helps you or anyone else that is reading this.

PS. I'm gonna 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. just gonna stick to Heroku to see if it works)