1

I'm trying to write a Python script, the main function of the code is a while loop always looking for a keyword, and when the keyword is found, it'll exit the script.

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


What I'm trying to achieve is, as long as boss has replied in the past five minutes, the bot replies nothing, after the five minutes ran out, then the bot will reply "Boss is not here". And every time boss replies, it'll reset the five minutes. But at any time it should be detecting for 'done'. How do I do this?

Anonymous Mouse
  • 233
  • 3
  • 9
  • I think your problem is related to this one: https://stackoverflow.com/questions/492519/timeout-on-a-function-call – teoML Dec 14 '19 at 14:55

0 Answers0