D
D
duduychvbk992021-08-02 13:16:53
Bots
duduychvbk99, 2021-08-02 13:16:53

How to make an interval on a telegram bot command?

The user executes some command. If 5 seconds have not passed since the last request, the bot writes: wait so many seconds before executing the next command. using telebot

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RINAMI, 2021-08-03
@RINAMI

Good afternoon, to restrict certain commands, you need the threading module, the easiest way is:

import threading
def test()
     #options
def thread2():  # Поток 2
    thr2 = Thread(target=test)
    thr2.start()

Example on vk_api:
mes = event.object.message['text']
if mes == 'ваша_команда':
  try:
   test()  #Пользователь один раз использует команду
   time.sleep(5) #Лимит на 5 секунд
  except:
        pass

PS at the end of the code add:
if __name__ == '__main__':
    thread2()

Bottom line: the method is simple and effective, the user writes the command once, then the restriction and can be used again.

I
Ilhomidin Bakhoraliev, 2021-08-23
@ilhomidin

The best solution is to record the time the command was last used. Here I answered how to make an interval of 1 hour. In your case, .hour needs to be changed to .second and 1 to 5.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question