1

I am trying to add webhook into my code, following this template PyTelegramBotApi webhook_aiohttp. My problem is that I cannot receive any updates from telegram using ngrok. What should be done in order to use webhook?

  1. I use in terminal

python -m SimpleHTTPServer 8443

  1. cd to the project's directory
  2. Launch ngrok

ngrok http 8443

Then output says that :

======== Running on http://localhost:8443 ======== (Press CTRL+C to quit)

My Project structure:

database

  • database.py

handlers

  • account.py
  • buy.py
  • join_group.py
  • shipping_handlers.py
  • start.py

config.py

main.py

from telebot import TeleBot
from membership_bot import config
from membership_bot.handlers.buy import buyHandlerTest, buyHandler
from membership_bot.handlers.join_group import joinGroupHandler
from membership_bot.handlers.start import startHandler, backHomeHandler
from membership_bot.handlers.shipping_handlers import checkout, got_payment
from membership_bot.handlers.account import accountHandler

bot = TeleBot(config.BOT_TOKEN)

bot.register_message_handler(startHandler, commands=['start'], chat_types=['private'], pass_bot=True)
bot.register_callback_query_handler(joinGroupHandler, func=lambda message: message.data == "join_group", pass_bot=True)
bot.register_callback_query_handler(accountHandler, func=lambda message: message.data == "account", pass_bot=True)
bot.register_callback_query_handler(backHomeHandler, func=lambda message: message.data == "back_home", pass_bot=True)
bot.register_callback_query_handler(buyHandler, func=lambda message: message.data.startswith('Buy'), pass_bot=True)
bot.register_pre_checkout_query_handler(checkout, func=lambda query: True, pass_bot=True)
bot.register_message_handler(got_payment, content_types=['successful_payment'], pass_bot=True)


bot.infinity_polling()

My edited template with aiohttps:

import logging


from aiohttp import web

import telebot

API_TOKEN = 'MyToken'

WEBHOOK_HOST = '123.eu.ngrok.io'
WEBHOOK_PORT = 8443  # 443, 80, 88 or 8443 (port need to be 'open')
WEBHOOK_LISTEN = 'localhost'  

WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)

logger = telebot.logger
telebot.logger.setLevel(logging.INFO)

bot = telebot.TeleBot(API_TOKEN)

app = web.Application()


# Process webhook calls
async def handle(request):
    logging.info('Called handle requests........')
    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)


app.router.add_post('/{token}/', handle)


# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
    logging.info('Called start command........')
    bot.reply_to(message,
                 ("Hi there, I am EchoBot.\n"
                  "I am here to echo your kind words back to you."))


# Handle all other messages
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
    bot.reply_to(message, message.text)


# Remove webhook, it fails sometimes the set if there is a previous webhook
logging.info('Removing webhook........')
bot.remove_webhook()
logging.info('Webhook removed........')
# Set webhook
logging.info('Setting webhook........')
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH)
logging.info('Webhook set........')


# Start aiohttp server
web.run_app(
    app,
    host=WEBHOOK_LISTEN,
    port=WEBHOOK_PORT
    
)
Ian
  • 21
  • 3

0 Answers0