X
X
Xpym4uk762020-10-10 19:39:48
Python
Xpym4uk76, 2020-10-10 19:39:48

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

What do I want to achieve? - So that you can create more than 2 timers that would not stop the program (So that users can interact with the bot) and the bot can send messages, and not be silent! Please, please help! I will be very grateful if I solve this problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-10-14
@Xpym4uk76

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 question

Ask a Question

731 491 924 answers to any question