-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrtbot.py
68 lines (61 loc) · 1.83 KB
/
frtbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import telegram as tg
import time
import os
#from subprocess import call,Popen
import subprocess
token = "<TOKEN>"
update_id_fname = "update_id.frt"
update_id = -1
if os.path.isfile(update_id_fname) and os.stat(update_id_fname).st_size !=0:
try:
update_id_fd = open(update_id_fname,"rb+")
update_id = int(update_id_fd.readline())
except IOError:
print "IOError accured!"
exit()
else:
try:
print "There is no "+update_id_fname+" file exit or it's empty try to create newone!"
update_id_fd = open(update_id_fname,"wb")
update_id = 0
except IOError:
print "IOError accured!"
exit()
#if update_id < 0 :
# update_id = int(update_id_fd.readline())
# print "The last update_id is :"+str(update_id)
#else:
# update_id = 0
print "Strat the fortune Bot"
bot = tg.Bot(token=token)
print "Try to run getMe"
print "I am "+bot.getMe().first_name
print "Try to run fortune"
#print call("fortune")
if subprocess.Popen("fortune", shell=True, stdout=subprocess.PIPE).stdout.read():
print "fortune is OK"
else:
print "Please install fortune and run again!"
exit()
#update_id = 0
custom_keyboard = [[ tg.Emoji.THUMBS_UP_SIGN, tg.Emoji.THUMBS_DOWN_SIGN ]]
reply_markup = tg.ReplyKeyboardMarkup(custom_keyboard)
while(True):
updates = bot.getUpdates(offset=update_id,timeout=120)
for update in updates:
if (update.update_id != update_id):
update_id = update.update_id
update_id_fd.seek(0,0)
update_id_fd.write(str(update_id)+"\n")
#print update
if update.message.chat == None:
continue
bot.sendChatAction(update.message.chat_id,tg.ChatAction.TYPING)
fortune = subprocess.Popen("fortune", shell=True, stdout=subprocess.PIPE).stdout.read()
print update_id
print fortune
bot.sendMessage(chat_id=update.message.chat_id,text=fortune,reply_markup=reply_markup)
#time.sleep(1)
time.sleep(1)
if update_id_fd:
update_id_fd.close()