This repository has been archived by the owner on Sep 11, 2019. It is now read-only.
forked from AmI-2018/python-lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskBot.py
140 lines (98 loc) · 3.27 KB
/
taskBot.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler
from telegram import ChatAction
import time
from sys import exit
from os import _exit
tasks_list = list()
pathToFile = "/home/ale-dell/Python project/python-lab2/task_list.txt"
"""
/showTasks
/newTask <task to add>
/removeTask <task to remove>
/removeAllTasks <substring to use to remove all the tasks that contain it>
"""
def load_list(path):
with open(path) as f:
lines = f.readlines()
for line in lines:
tasks_list.append(line.strip())
f.close()
def save_list(bot, update):
f = open(pathToFile, 'w')
for item in tasks_list:
f.write("%s\n" % item)
update.message.reply_text("list saved")
f.close()
"""
def readInput(bot, update):
readed = update.message.text
print("f readInput" + readed)
return update.message.text
"""
def showTasks(bot, update):
if len(tasks_list) == 0:
update.message.reply_text("no tasks memorized yet")
return
tasks_list.sort()
for element in tasks_list:
update.message.reply_text(element)
def newTask(bot, update, args):
msg = ' '.join(args)
#print(msg + " replied text!!")
if(msg != ""):
tasks_list.append(msg)
#showTasks(bot, update)
update.message.reply_text("added " + msg + " to the tasks list")
else:
update.message.reply_text("error")
def removeTask(bot, update, args):
msg = ' '.join(args)
try:
tasks_list.remove(msg)
update.message.reply_text(msg + " removed")
except ValueError:
update.message.reply_text("element not found!")
def removeAllTasks(bot, update):
tasks_list.clear()
update.message.reply_text("Deleted ALL tasks")
def start(bot, update):
# handle start on bot
update.message.reply_text("connected")
update.message.reply_text("""
List of commands:
/start - Start the bot
/show_tasks - Show stored tasks
/new_task - Add new task
/remove_task - Remove a single task
/remove_all_tasks - Remove ALL tasks contening the sent phrase
/save_tasks - Store the current list
/quit - Exit the bot and save
""")
def testPrint():
if len(tasks_list) == 0:
print("no tasks memorized yet")
return
tasks_list.sort()
print(*tasks_list, sep='\n')
def closeBot(bot, update):
update.message.reply_text("adieu!")
save_list(bot, update)
_exit(0)
def main():
updater = Updater("541907262:AAEOOVHWPnGZPJcHjHEkVtvsJ_asrRdwc14")
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("show_tasks", showTasks))
dispatcher.add_handler(CommandHandler("new_task", newTask, pass_args="true"))
dispatcher.add_handler(CommandHandler("remove_task", removeTask, pass_args="true"))
dispatcher.add_handler(CommandHandler("remove_all_tasks", removeAllTasks))
dispatcher.add_handler(CommandHandler("save_tasks", save_list))
dispatcher.add_handler(CommandHandler("quit", closeBot))
#dispatcher.add_handler(MessageHandler(Filters.text, readInput))
load_list(pathToFile)
# testPrint()
updater.start_polling()
updater.idle()
# save_list(pathToFile)
if __name__ == "__main__":
main()