Answer the question
In order to leave comments, you need to log in
How to solve the silence of the telegram bot with a timer?
I created a telegram bot with a game on a 60 second timer. With more than two timer requests, players cannot interact with the bot, here is the piece where I implemented the timer for the bot:
import telebot
import asyncio
@bot.message_handler(commands=['start'])
def start(message):
asyncio.run(ng_threadadd_2(pl_room_id, g_time, host_id, player_id))
async def ng_threadadd_2(pl_room_id, g_time, host_id, player_id):
task = asyncio.create_task(ng_everysectimer_2(pl_room_id, g_time, host_id, player_id))
await asyncio.gather(task)
async def ng_everysectimer_2(pl_room_id, g_time, host_id, player_id):
while g_time <= 60:
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
cursor.execute(f'SELECT game_ended FROM users WHERE user_id = {pl_room_id};')
boolgameend = cursor.fetchone()[0]
if str(pl_room_id) in games and host_id != 0 and player_id != 0 and boolgameend == 0:
if not g_time >= 60:
g_time = g_time + 1
await asyncio.sleep(1.0)
else:
bot.send_message(host_id, "Игра завершена")
bot.send_message(player_id, "Игра завершена")
break
elif boolgameend == 1:
break
Answer the question
In order to leave comments, you need to log in
Your code is not asynchronous, you use asyncio and the synchronous telebot framework, which have blocking functions in it.
To interact with the telegram, take the aiogram library.
To work with sqlite, check out https://github.com/omnilib/aiosqlite as sqlite3 is also a synchronous library.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question