forked from AmI-2018/python-lab4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskBot.py
150 lines (119 loc) · 4.33 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
141
142
143
144
145
146
147
148
149
150
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler, ConversationHandler)
from telegram import (ChatAction, ReplyKeyboardMarkup, ReplyKeyboardRemove)
from os import _exit
import sqlite3
import os
import configparser
import logging
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.ERROR)
pathToFile = "/home/ale-dell/Python project/python-lab2/task_list.txt"
pathToDb = 'task_list.db'
con = None
def start(bot, update):
global con
bot.sendChatAction(update.message.chat_id, ChatAction.TYPING)
try:
con = sqlite3.connect(pathToDb)
# print("db correttamente aperto")
# update.message.reply_text("your user id is: " + str(update.message.from_user.id))
cur = con.cursor()
cur.execute(
"create table if not exists '%d' (id_task integer primary key, todo varchar[255] not null)" % update.message.from_user.id)
con.commit()
cur.close()
except sqlite3.DataError as DataErr:
print("errore di creazione table " + DataErr.args[0])
except sqlite3.DatabaseError as DBerror:
print("errore nell'apertura del db " + DBerror.args[0])
sys.exit(1)
update.message.reply_text("""
List of commands:
/start - Start the bot
/showTasks - Show stored tasks
/newTask - Add new task
/removeTask - Remove a single task
/removeAllTasks - Remove all the existing tasks from the DB that contain a provided
/quit - Exit the bot and save
""")
def showTasks(bot, update):
global con
cur = con.cursor()
cur.execute("select todo from '%s' order by todo" % update.message.from_user.id)
rows = cur.fetchall()
cur.close()
if len(rows) == 0:
update.message.reply_text("no tasks memorized yet")
return
for line in rows:
print(line[0])
update.message.reply_text(line[0])
def newTask(bot, update, args):
msg = ' '.join(args)
if (msg != ""):
global con
cur = con.cursor()
cur.execute(
"insert into '%d' (todo) values ('%s')" % (update.message.from_user.id, msg))
con.commit()
cur.close()
# showTasks(bot, update)
update.message.reply_text("added " + msg + " to the tasks list")
else:
update.message.reply_text("empty task...")
def removeTask(bot, update, args):
msg = ' '.join(args)
try:
global con
cur = con.cursor()
cur.execute("delete from '%s' where todo = '%s'" % (update.message.from_user.id, args[0]))
con.commit()
cur.close()
update.message.reply_text(msg + " removed")
except ValueError:
update.message.reply_text("element not found!")
def substringStatement(string, userID):
statement = "delete * from " + userID + "where"
words = string.split(" ")
for word in words:
statement += " todo like " + word
if word != words[-1]:
statement += "and"
print("da eliminare\n" + statement)
def removeAllTasks(bot, update):
global con
cur = con.cursor()
# use the substingStatement function
# cur.execute("delete from '%s' " % update.message.from_user.id)
update.message.reply_text("Deleted ALL tasks")
def closeBot(bot, update):
global con
con.close()
update.message.reply_text("adieu!")
_exit(0)
def clean_db(bot, update):
global pathToDb
os.remove(pathToDb)
with open(pathToDb, 'w'):
os.utime(pathToDb, None)
def main():
config = configparser.ConfigParser()
config.read('config.ini')
token = config['telegram']['token']
updater = Updater(token)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("showTasks", showTasks))
dispatcher.add_handler(CommandHandler("newTask", newTask, pass_args="true"))
dispatcher.add_handler(CommandHandler("removeTask", removeTask, pass_args="true"))
dispatcher.add_handler(CommandHandler("removeAllTasks", removeAllTasks))
dispatcher.add_handler(CommandHandler("quit", closeBot))
dispatcher.add_handler(CommandHandler("clean_db", clean_db))
# piu brevi
dispatcher.add_handler(CommandHandler("add", newTask, pass_args="true"))
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()