在主循环保持运行的情况下,如何制作单独的“类似计时器”的函数来设置变量?

时间:2019-12-14 14:40:05

标签: python

我正在尝试编写Python脚本,代码的主要功能是while循环,始终在寻找关键字,找到关键字后,它将退出脚本。

import telepot
import datetime
import time

bot = telepot.Bot('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

print ('start listening to replies')

last_update = 0
update_offset = 0


f = open('last_update_id.txt', 'r')

if f == '':
    last_update = 0
elif type(f) == str:
    last_update = int(f)
elif type (f) == int:
    last_update = f

f.close



update_offset = last_update + 1

#START LOOP UNTIL SATISFIED WITH REPLY
while true:
    #get update
    response = bot.getUpdates(offset=update_offset)
    listOfSenders = []
    for i in response:

        #INTERPRETION OF CURRENT MESSAGE

        update_id = i['update_id']


        message = i['message']


        whoSent = message['from']


        senderUsername = whoSent['username']


        timestampEpoch = message['date']
        timestampHuman = datetime.datetime.fromtimestamp(timestampEpoch).strftime('%Y-%m-%d %H:%M:%S')


        text = message['text']


        print(text + ' from ' + sender + ' at ' + timestampHuman + ' server\'s time')

        listOfSenders.append(senderUsername)

    #TAKE ACTION TO MESSAGE
    #is boss here?
    if 'boss' in listOfSender:
        bossIsPresent = 'yes'
    else:
        bossIsPresent = 'no'

    if 'done' in text:
        #job is done, bye
        jobDone = 'yes'

    else:
        jobDone = 'no'



    if bossIsPresent = 'yes':
        bot.sendMessage(zzzzzzz, 'My boss is here. Goodbye')

    #boss not here, reply
    elif jobDone = 'yes':
        bot.sendMessage(zzzzzzz, 'OK. My Job is done. Goodbye')
        #write last update_id to file      
        f = open('last_update_id.txt', 'w+')
        f.write(str(update_id))
        f.close
        #exit
        exit
    elif jobDone = 'no':
        bot.sendMessage(zzzzzzzz, 'Huh?!')

    #PREPARE FOR NEXT UPDATE
    #the next update_offset
    update_offset = update_id + 1
    time.sleep(10)

#write last update_id to file      
f = open('last_update_id.txt', 'w+')
f.write(str(update_id))
f.close


我要实现的目标是,只要老板在过去五分钟内做出答复,该机器人就什么也不会回复,在五分钟内,机器人会回复“老板不在这里”。老板每次答复时,都会重置五分钟。但是在任何时候它都应该检测“完成”。我该怎么做?

0 个答案:

没有答案