forked from AmI-2019/python-lab4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo_manager_bot_db.py
More file actions
108 lines (81 loc) · 3.32 KB
/
todo_manager_bot_db.py
File metadata and controls
108 lines (81 loc) · 3.32 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
107
108
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import pymysql
task_list = []
connection = pymysql.connect(user="root", password="toor", host="localhost", database="todo_list")
next_id = 1
# Not used
def save_list(bot, update):
fh = open('task_list.txt', "w")
for task in sorted(task_list):
fh.write(task + "\n")
bot.send_message(chat_id=update.message.chat_id, text="TODO list saved!")
def start(bot, update):
global next_id
load_query = "SELECT task FROM todo_list.todo"
cursor = connection.cursor()
cursor.execute(load_query)
tasks = cursor.fetchall()
task_list.clear()
next_id += len(tasks)
for task in tasks:
task_list.append(task[0])
bot.send_message(chat_id=update.message.chat_id, text="Welcome, TODO list loaded!")
def show_task(bot, update):
if len(task_list) == 0:
bot.send_message(chat_id=update.message.chat_id, text='Task list is empty!')
for task in sorted(task_list):
bot.send_message(chat_id=update.message.chat_id, text=task)
def new_task(bot, update, args):
global next_id
insert_query = "INSERT INTO todo_list.todo(id_task, task) VALUES (%s, %s)"
cursor = connection.cursor()
cursor.execute(insert_query, (next_id, args[0]))
connection.commit()
next_id += 1
task_list.append(args[0])
bot.send_message(chat_id=update.message.chat_id, text=args[0]+" added to the list!")
def remove_task(bot, update, args):
remove_query = "DELETE FROM todo_list.todo WHERE task=%s"
if args[0] in task_list:
task_list.remove(args[0])
cursor = connection.cursor()
cursor.execute(remove_query, (args[0],))
connection.commit()
bot.send_message(chat_id=update.message.chat_id, text="Task removed!")
# save_list(bot, update)
else:
bot.send_message(chat_id=update.message.chat_id, text='Task not found in the list!')
def remove_all_task(bot, update, args):
remove_query = "DELETE FROM todo_list.todo WHERE task=%s"
modified = False
for task in task_list:
if args[0] in task:
task_list.remove(task)
cursor = connection.cursor()
cursor.execute(remove_query, (task,))
cursor.close()
modified = True
bot.send_message(chat_id=update.message.chat_id, text=task+" Task removed!")
if modified:
connection.commit()
else:
bot.send_message(chat_id=update.message.chat_id, text="No task found to be removed!")
def help_on_message(bot, update):
bot.send_message(chat_id=update.message.chat_id, text='Im sorry i cant do that')
def main():
updater = Updater("786366201:AAGfDQFB1zmLb--YCz9YxvRt1mO7wqHh1HM")
dispatcher = updater.dispatcher
handlers = []
handlers.append(CommandHandler('start', start))
handlers.append(CommandHandler('showTask', show_task))
handlers.append(CommandHandler('newTask', new_task, pass_args=True))
handlers.append(CommandHandler('removeTask', remove_task, pass_args=True))
handlers.append(CommandHandler('removeAllTasks', remove_all_task, pass_args=True))
handlers.append(MessageHandler(Filters.all, help_on_message))
for handler in handlers:
dispatcher.add_handler(handler)
updater.start_polling()
updater.idle()
connection.close()
if __name__ == "__main__":
main()