0

As said in the title, I'm using telebot locally as an app in Flask with a free webhook taken from ngrok.

With the script just started, everything works fine, but with around 30-60 minutes of inactivity (no new messages from users) the script still receive POST messages correctly but the bot don't answer anymore and I have to restart the flask app.

I think this could be something that I don't know about with the Telegram API, because I see no problem in Flask or ngrok incoming POST messages...

Here is the main code:

from flask import Flask, request
import telebot
import threading
from time import sleep
import os
import art
from telebot import types
from telebot.types import InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, KeyboardButton
from datetime import datetime
from colorama import Fore, init, Style
from pyngrok import ngrok
import logging

https_tunnel = str(ngrok.connect("80", bind_tls=True)).split('"')[1]

bot = telebot.TeleBot(MYTOKENHERE)
bot.set_webhook(url=https_tunnel)
app=Flask(__name__)

@app.route('/', methods=["POST"])
def webhook():
    bot.process_new_updates(
        [telebot.types.Update.de_json(request.stream.read().decode("utf-8"))]
    )
    return "ok"

@bot.message_handler(commands=['start'])
def command_start(message):
   #MY STARTING ROUTINE HERE

@bot.callback_query_handler(func=lambda call: True)
def call_routine(call):
   #MY CALLBACK ROUTINE HERE


if __name__ == "__main__":
    app.run(port=80)
Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
Alemane85
  • 11
  • 3
  • Do you answer inline queries in your call_routine? bot.answer_callback_query – Marat Mkhitaryan Jul 24 '21 at 10:46
  • I use this bot methods to send or edit the bot answers: bot.edit_message_text and bot.send_message – Alemane85 Jul 24 '21 at 13:17
  • You must do bot.answer_callback_query. That mught be the reason why your bot is stucked. https://core.telegram.org/bots/api#callbackquery "It is, therefore, necessary to react by calling answerCallbackQuery even if no notification to the user is needed". – Marat Mkhitaryan Jul 24 '21 at 15:12
  • Thank you Marat now it's Better but still after some idle time when i use /start or a call back the bot don't answer if not after a 10 seconds or so ... Is It normal or there Is something like a wakeup time to adjust? Thank you in advance – Alemane85 Jul 25 '21 at 14:59

0 Answers0