I have a telegram python API bot which is writing messages with some chat users information in cycle. However, I want it to allow user to ask bot to stop writing if cycle is too long, how to do this? It seems like bot is staying in cycle ignoring all others commands until it ends, but I want it to allow to check outside for example each second to react to others commands and also potentially stop words to stop the cicle. 3 seconds is to prevent spam mute.
I am using python-telegram-bot
Here is a code:
import logging
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler
import random
import datetime
import time
lines1 = open('user_stats.txt', encoding="utf-8").read().splitlines()
async def stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
a = 0
while a < len(lines1):
time.sleep(3)
await context.bot.send_message(chat_id=update.effective_chat.id, text=lines1[a])
a += 1
if __name__ == '__main__':
application = ApplicationBuilder().token("TOKEN").build()
stats_handler = CommandHandler('stats', stats)
application.add_handler(stats_handler)
application.run_polling()