forked from AmI-2018/python-lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTo_Do_Bot.py
More file actions
106 lines (82 loc) · 2.64 KB
/
To_Do_Bot.py
File metadata and controls
106 lines (82 loc) · 2.64 KB
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
from telegram.ext import Updater, CommandHandler, \
MessageHandler, Filters
from telegram import ChatAction
task = []
def insert(st):
task.append(st)
def remove(name):
if name not in task:
print("Not found")
else:
task.remove(name)
def save():
inpt=open("task_list.txt", "w")
for x in task:
inpt.write(x+"\n")
inpt.close()
def start(bot,update):
update.message.reply_text("Hello!")
print("Hello!")
def nomsg(bot, update):
bot.sendChatAction(update.message.chat_id, ChatAction.TYPING)
update.message.reply_text("I'm sorry, I can't do that.")
def show(bot, update):
bot.sendChatAction(update.message.chat_id, ChatAction.TYPING)
if task == "":
update.message.reply_text("Nothing to show.")
else:
for x in task:
update.message.reply_text(x)
def rmv(bot, update, args):
bot.sendChatAction(update.message.chat_id, ChatAction.TYPING)
string = " ".join(str(x) for x in args)
if string not in task:
update.message.reply_text(string + "not found!")
else:
task.remove(string)
save()
update.message.reply_text(string + "removed successfully!")
def rmvall(bot, update, args):
name = str(args[0])
removed = []
for x in task:
if name in x:
removed.append(x)
task.remove(x)
if not removed :
update.message.reply_text("No matches.")
else:
save()
name = " and ".join(str("\""+x+"\"") for x in removed)
update.message.reply_text("The elements " + name + " successfully removed!")
def new(bot, update, args):
bot.sendChatAction(update.message.chat_id, ChatAction.TYPING)
string = " ".join(str(x) for x in args)
insert(string)
save()
update.message.reply_text("Successfully added!")
def help(boy,update):
update.message.reply_text("To-do Manager:\n1. Insert new task\
\n2. Remove task\n3. Show all tasks")
inpt = open("task_list.txt")
for line in inpt:
insert(line.strip())
inpt.close()
def main():
"""
My First Bot
"""
inpt=open("TOKEN.txt","r")
updater=Updater(inpt.readline())
dp = updater.dispatcher
dp.add_handler(CommandHandler("start",start))
dp.add_handler(MessageHandler(Filters.text,nomsg))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("showTasks",show))
dp.add_handler(CommandHandler("newTask",new, pass_args=True))
dp.add_handler(CommandHandler("removeTask",rmv,pass_args=True))
dp.add_handler(CommandHandler("removeAllTasks",rmvall, pass_args=True))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()