Python telegram bot cooldown function
Question:
I was stuck on my code due to the reason that it’s not responding
because i want a bot that /price command has a cooldown and in the next time someone use /price command it will wont respond for 30 mins or prompt people to wait until the cooldown is done so that it will be less spammy on telegram groups when many people doing /price in a same time
def price(update, context):
if chat_data.get('last_time'):
if datetime.now()-last_time <= my_threshold:
return
chat_data['last_time'] = datetime.now()
update.message.reply_text(
'Bitcoin PRICE TRACKER n'
' Price: $ ' + usd + 'n'
' Marketcap: $ ' + usdcap + 'n'
' 24 Hour Volume: $ ' + usdvol + 'n'
' 24 Hour Change: % ' + usdchange + 'n'
'⚙️ Last Updated at: ' + lastat +'n'
)
updater = Updater('mytoken', use_context=True)
updater.dispatcher.add_handler(CommandHandler('price', price))
Answers:
What about something django-rest style, like a decorator?
import datetime
throttle_data = {
'minutes': 30,
'last_time': None
}
def throttle(func):
def wrapper(*args, **kwargs):
now = datetime.datetime.now()
delta = now - datetime.timedelta(minutes=throttle_data.get('minutes', 30))
last_time = throttle_data.get('last_time')
if not last_time:
last_time = delta
if last_time <= delta:
throttle_data['last_time'] = now
func(*args, **kwargs)
else:
return not_allowed(*args)
return wrapper
def not_allowed(update, context):
update.message.reply_text(text="You are not allowed.")
@throttle
def price(update, context):
update.message.reply_text(
'Bitcoin PRICE TRACKER n'
' Price: $ ' + usd + 'n'
' Marketcap: $ ' + usdcap + 'n'
' 24 Hour Volume: $ ' + usdvol + 'n'
' 24 Hour Change: % ' + usdchange + 'n'
'⚙️ Last Updated at: ' + lastat +'n'
)
updater = Updater('mytoken', use_context=True)
updater.dispatcher.add_handler(CommandHandler('price', price))
Of course throttle is resetted everytime you restart the bot.
This, by the way, will spam “You are not allowed”, so I you have a lot of user throttling, you change not_allowed
function removing the reply.
I was stuck on my code due to the reason that it’s not responding
because i want a bot that /price command has a cooldown and in the next time someone use /price command it will wont respond for 30 mins or prompt people to wait until the cooldown is done so that it will be less spammy on telegram groups when many people doing /price in a same time
def price(update, context):
if chat_data.get('last_time'):
if datetime.now()-last_time <= my_threshold:
return
chat_data['last_time'] = datetime.now()
update.message.reply_text(
'Bitcoin PRICE TRACKER n'
' Price: $ ' + usd + 'n'
' Marketcap: $ ' + usdcap + 'n'
' 24 Hour Volume: $ ' + usdvol + 'n'
' 24 Hour Change: % ' + usdchange + 'n'
'⚙️ Last Updated at: ' + lastat +'n'
)
updater = Updater('mytoken', use_context=True)
updater.dispatcher.add_handler(CommandHandler('price', price))
What about something django-rest style, like a decorator?
import datetime
throttle_data = {
'minutes': 30,
'last_time': None
}
def throttle(func):
def wrapper(*args, **kwargs):
now = datetime.datetime.now()
delta = now - datetime.timedelta(minutes=throttle_data.get('minutes', 30))
last_time = throttle_data.get('last_time')
if not last_time:
last_time = delta
if last_time <= delta:
throttle_data['last_time'] = now
func(*args, **kwargs)
else:
return not_allowed(*args)
return wrapper
def not_allowed(update, context):
update.message.reply_text(text="You are not allowed.")
@throttle
def price(update, context):
update.message.reply_text(
'Bitcoin PRICE TRACKER n'
' Price: $ ' + usd + 'n'
' Marketcap: $ ' + usdcap + 'n'
' 24 Hour Volume: $ ' + usdvol + 'n'
' 24 Hour Change: % ' + usdchange + 'n'
'⚙️ Last Updated at: ' + lastat +'n'
)
updater = Updater('mytoken', use_context=True)
updater.dispatcher.add_handler(CommandHandler('price', price))
Of course throttle is resetted everytime you restart the bot.
This, by the way, will spam “You are not allowed”, so I you have a lot of user throttling, you change not_allowed
function removing the reply.