Alemane85
Alemane85

Reputation: 11

Telegram Bot in python with Flask and Ngrok as webhook: after a while the bot stops answering but the script still receive post messages

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)

Upvotes: 0

Views: 1239

Answers (0)

Related Questions