Stop listening to old message Python Telegram Bot

Viewed 450

I did a simple python code to open my automatic gate, here's the code:

#!/usr/bin/env python

"""
open my automatic gate
"""
import gpiozero
import time
import logging
from functools import wraps
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

# log
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

#user id
id = [XXXXX, YYYYY, ZZZZZ, WWWWW] #riccardo, papà, letizia, mamma

#relay object
relay = gpiozero.OutputDevice(4, active_high=True, initial_value=False)

# access restricted                                                                                                                                                                            def restricted(func):
    @wraps(func)
    def wrapped(update, context, *args, **kwargs):
        user_id = update.effective_user.id
        if user_id not in id:
            print("accesso non autorizzato per {}.".format(user_id))
            update.message.reply_text('Utente non autorizzato')
            return
        return func(update, context, *args, **kwargs)
    return wrapped

# open the gate
@restricted
def apri(update, context):
    relay.on()
    time.sleep(1)
    relay.off()
    update.message.reply_text('Apro il cancello!')
    time.sleep(30)
    update.message.reply_text('Cancello aperto!')
    time.sleep(82)
    update.message.reply_text('Chiudo il cancello')
    time.sleep(30)
    relay.off()
    update.message.reply_text('Cancello chiuso!')

# help
def aiuto(update, context):
    update.message.reply_text('Digitare /apri per aprire il cancello di casa. Solo utenti autorizzati')

# echo all other messages
def echo(update, context):
    update.message.reply_text(update.message.text)

# main                                                                                                                                                                                                         def main():
    """start the bot"""
    updater = Updater("TOKEN_HERE", use_context=True)

    #register
    dp = updater.dispatcher

    #message handler
    dp.add_handler(CommandHandler("apri", apri))
    dp.add_handler(CommandHandler("aiuto", aiuto))
    dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

    #listening
    updater.start_polling()

    # exiting
    updater.idle()


if __name__ == '__main__':
    main()

The problem is, if for some reason my Raspberry Pi goes off, all the messages received while off are executed in a queue the moment I restart it with the result of unwanted behavior. Is there a way I can check the datetime the user send the message an execute it only within a limited time? E.g.: User A send the OPEN command If my script received it within 1 minute from the send time: EXECUTE THE COMMAND Else Discard the message

Hope I have make myself clear.

Best regards

Riccardo

1 Answers

I did manage to get message time so that I can compare with actual time and discard older message.

Thank you all

Riccardo

Related