how to delay between telebot
-
How to delay the performance of the teams on the telegram of the bote. For example, if the user wrote a team, and then again, he was texted "Wait five seconds before this crew" and so for five seconds. Please help me, I'm very happy with your help.
-
Example of response /questions/tagged/python-telegram-bot (sighs) UPD added the option /questions/tagged/pytelegrambotapi but implementation can be translated into other libraries.
Action:
- Establish the structure, e.g. vocabulary or database for id chata storage and communication time
- By receiving the message, check the difference between the current time and the fact that it was retained.
- You can keep the current time in seconds,
timestamp
- In the example, the time of storage in the facility
datetime
, in deducting onedatetime
fromdatetime
Subject receivedtimedelta
which has a methodtotal_seconds
- You can keep the current time in seconds,
- If the difference is less or equal to the given seconds, we'll send a message waiting.
https://github.com/gil9red/SimplePyScripts/blob/8e4d03f65bff32b4ac0f35840cee7c58f7420893/telegram_bot_examples/restriction_on_frequent_sending_of_messages__using_dict.py#L30
import datetime as DT
...
CHAT_BY_DATETIME = dict()
def on_request(update: Update, context: CallbackContext):
message = update.effective_messagetext = 'Получено!' need_seconds = 50 current_time = DT.datetime.now() last_datetime = CHAT_BY_DATETIME.get(message.chat_id) # Если первое сообщение (время не задано) if not last_datetime: CHAT_BY_DATETIME[message.chat_id] = current_time else: # Разница в секундах между текущим временем и временем последнего сообщения delta_seconds = (current_time - last_datetime).total_seconds() # Осталось ждать секунд перед отправкой seconds_left = int(need_seconds - delta_seconds) # Если время ожидания не закончилось if seconds_left > 0: text = f'Подождите {seconds_left} секунд перед выполнение этой команды' else: CHAT_BY_DATETIME[message.chat_id] = current_time message.reply_text(text, quote=True)
...
UPD. https://github.com/gil9red/SimplePyScripts/blob/dd43a5ba251ead34159475f7a8581857d8144444/telegram_bot__telethon__examples/restriction_on_frequent_sending_of_messages__using_dict.py#L26
import datetime as DT
SOURCE: pip install pyTelegramBotAPI
import telebot
from config import TOKEN
bot = telebot.TeleBot(TOKEN)
CHAT_BY_DATETIME = dict()
@bot.message_handler(commands=['help', 'start'])
def on_start(message: telebot.types.Message):
bot.send_message(message.chat.id, 'Write something')@bot.message_handler(func=lambda message: True)
def on_request(message: telebot.types.Message):
text = 'Получено!'
need_seconds = 50
current_time = DT.datetime.now()
last_datetime = CHAT_BY_DATETIME.get(message.chat.id)# Если первое сообщение (время не задано) if not last_datetime: CHAT_BY_DATETIME[message.chat.id] = current_time else: # Разница в секундах между текущим временем и временем последнего сообщения delta_seconds = (current_time - last_datetime).total_seconds() # Осталось ждать секунд перед отправкой seconds_left = int(need_seconds - delta_seconds) # Если время ожидания не закончилось if seconds_left > 0: text = f'Подождите {seconds_left} секунд перед выполнение этой команды' else: CHAT_BY_DATETIME[message.chat.id] = current_time bot.reply_to(message, text)
bot.infinity_polling()
Result: