【问题标题】:Stop listening to old message Python Telegram Bot停止收听旧消息 Python Telegram Bot
【发布时间】:2020-11-23 21:11:11
【问题描述】:

我做了一个简单的python代码来打开我的自动门,代码如下:

#!/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()

问题是,如果由于某种原因我的 Raspberry Pi 关机了,那么在我重新启动它的那一刻,所有在关机时收到的消息都会在队列中执行,从而导致出现不需要的行为。有没有办法可以检查用户发送消息的日期时间并仅在有限的时间内执行它?例如。: 用户 A 发送 OPEN 命令 如果我的脚本在发送时间后 1 分钟内收到它: 执行命令 别的 丢弃消息

希望我已经说清楚了。

最好的问候

里卡多

【问题讨论】:

    标签: python raspberry-pi bots telegram gpio


    【解决方案1】:

    我确实设法获取消息时间,以便我可以与实际时间进行比较并丢弃旧消息。

    谢谢大家

    里卡多

    【讨论】:

    • 请您解释一下它是如何完成的?
    猜你喜欢
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    相关资源
    最近更新 更多