-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToddy.py
458 lines (377 loc) · 17.4 KB
/
Toddy.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Simple To-DO bot created and designed by Enrico Cecchetti
ienricocecchetti@gmail.com
Enjoy and have fun with this code!
--------------------------------
please at least mention me on your project if you copy past my code :)
"""
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from functions import dateparser, timeparser, timeDifferenceInSec
import logging
import sqlite3
import re
from datetime import date, datetime, timedelta
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi Mate press /help for more info')
chat_id = update.message.chat_id
chat_usr = update.message.from_user.username
# check user existance (and update his info) or create a new one
checkuser(chat_id, chat_usr)
def help(bot, update):
"""Send a message when the command /help is issued."""
update.message.reply_text('🔴 Welcome my Master!! 🔴 \n'
'use the commands shown below to interact wih me:\n\n'
' 📅 TODO Supporter 📅\n'
'/schedule <?> - show daily due📓 \n'
'/remember <?> - to setup event 📌 \n'
'/info <?> - show info related to an event\n'
'/forget <?> - to delete some due ❌ \n'
'/free - to wipe all your due (⚠Attention⚠)\n\n')
def remember(bot, update, args, job_queue, chat_data):
"""Echo the user message."""
chat_id = update.message.chat_id
chat_usr = update.message.from_user.username
# check user existance (and update his info) or create a new one
checkuser(chat_id, chat_usr)
# get the user-id to execute the query
codu = getuser(chat_id)
name = ""
date = ""
place = ""
time = ""
desc = ""
if len(args) < 2:
update.message.reply_text("Master please don't forget that i don't know all the info\n"
"Please send me all the data, write: \n\n"
"/remember <due><date><place><time><desc>\n\n"
" ex: '/remember reunion 12/10/2019 Banca 13:00 reunion with China friends at POLITO'")
return
else:
i = 0
for par in args[0:]:
i += 1
matchObj = re.fullmatch(r'([^\s!/]+)', par, re.M | re.I)
if matchObj:
name += (matchObj.group()+" ")
continue
matchObj = re.match(r'[0-9]{2}/[0-9]{2}/[0-9]{4}', par, re.M | re.I)
if matchObj:
date = matchObj.group()
break
for par in args[i:]:
i += 1
matchObj = re.fullmatch(r'([^\s!:]+)', par, re.M | re.I)
if matchObj:
place += (matchObj.group()+" ")
continue
matchObj = re.match(r'[0-9]{2}:[0-9]{2}', par, re.M | re.I)
if matchObj:
time = matchObj.group()
break
for par in args[i:]:
desc += (re.match(r'([^\s]+)', par, re.M | re.I).group()+" ")
if time == "":
time = "08:00"
if dateparser(date) != -1 and timeparser(time) != -1:
conn = sqlite3.connect('todo.db')
c = conn.cursor()
# print("INSERT INTO todos (uid, name, date, place, time)
# VALUES("+str(codu)+","+str(name)+","+str(date)+","+str(place)+","+str(time)+","+str(desc)+")")
c.execute("INSERT INTO todos (uid, name, date, place, time, descr) "
"VALUES(?,?,?,?,?,?)", (codu, name, date, place, time, desc))
conn.commit()
conn.close()
due_datetime = datetime.combine(dateparser(date),
timeparser(time))
today_date = datetime.now()
# it will notify 1800 sec before the date and time selected so 30 min before
diff_in_sec = timeDifferenceInSec(due_datetime, today_date) - 5400
set_timer(update, diff_in_sec, job_queue, chat_id, chat_data)
update.message.reply_text("Got it!! no problem i'll remember it for ya 💪 \n")
else:
update.message.reply_text("❌ Hey sir, Time or Date format is wrong... are you drunk? \n"
"Remember that DATE must be like : dd/mm/yyyy \n"
"and TIME must be in the format: hh:mm \n\n ")
def alarm(bot, job):
"""Send the alarm message."""
bot.send_message(job.context, text='✔ Hey Master you have something to in half an hour... '
'Check your appointments for details!')
def set_timer(update, args, job_queue, chat_id, chat_data):
"""Add a job to the queue."""
try:
# args[0] should contain the time for the timer in seconds
#print("value in second = %s" % (int(args),))
due = int(args)
if due < 0:
update.message.reply_text('Sorry we can not go back to future!')
return
# Add job to queue
job = job_queue.run_once(alarm, due, context=chat_id)
chat_data['job'] = job
update.message.reply_text('Corresponding alarm successfully set!')
except (IndexError, ValueError):
update.message.reply_text('Sir, i got some problem setting the alarm! '
'May i ask to to delete/reinsert the appointment!?')
def unset(bot, update, chat_data):
"""Remove the job if the user changed their mind."""
if 'job' not in chat_data:
# update.message.reply_text('You have no active timer')
return
job = chat_data['job']
job.schedule_removal()
del chat_data['job']
# update.message.reply_text('Timer successfully unset!')
def info(bot, update, args):
"""Echo the user message."""
chat_id = update.message.chat_id
chat_usr = update.message.from_user.username
# check user existance (and update his info) or create a new one
checkuser(chat_id, chat_usr)
# get the user-id to execute the query
codu = getuser(chat_id)
if len(args) == 0:
update.message.reply_text("Master, please insert the note that you want me to show in details!\n"
"ex: /info <codEvent>")
else:
conn = sqlite3.connect('todo.db')
c = conn.cursor()
print("SELECT * FROM todos WHERE uid="+str(codu)+" and tid="+str(args[0]))
c.execute('SELECT * FROM todos WHERE uid=? and tid=?', (codu, args[0]))
data = c.fetchone()
if data[0] is not None:
update.message.reply_text("🔻 NOTE SUMMARY for '"+data[2]+"'\n\n" +
"APPOINTMENT named: "+data[2]+"\n" +
"📆 will be on: "+data[3]+"\n" +
"⏰ at: "+data[5] + "\n" +
"🌍 in: "+str(data[4]).upper()+ "\n" +
"📎 details: "+str(data[6]) + "\n\n")
conn.close()
def forget(bot, update, args, chat_data):
"""Echo the user message."""
chat_id = update.message.chat_id
chat_usr = update.message.from_user.username
# check user existance (and update his info) or create a new one
checkuser(chat_id, chat_usr)
# get the user-id to execute the query
codu = getuser(chat_id)
if isinstance(args, list):
if len(args) == 0:
update.message.reply_text("Master, please insert the note that you want me to delete!"
"/forget <codEvent>")
else:
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('DELETE FROM todos WHERE uid=? and tid=?', (codu, args[0]))
conn.commit()
conn.close()
update.message.reply_text("Ok Master, i feel more free now!👾")
return
else:
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('DELETE FROM todos WHERE uid=? and tid=?', (codu, args))
conn.commit()
conn.close()
unset(bot, update, chat_data)
def free(bot, update, chat_data):
"""Echo the user message."""
chat_id = update.message.chat_id
chat_usr = update.message.from_user.username
# check user existance (and update his info) or create a new one
checkuser(chat_id, chat_usr)
# get the user-id to execute the query
codu = getuser(chat_id)
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('SELECT * FROM todos where uid=?', (codu,))
data = c.fetchall()
if len(data) != 0:
for row in data:
forget(bot, update, row[0], chat_data)
update.message.reply_text("Master im going in Holiday! Im finally free to go... 😎")
def default(bot, update):
"""Echo the user message."""
update.message.reply_text("yeah... you are right man!")
def manage_command(bot, update):
update.message.reply_text("Unknown command. Press /help for more info")
def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)
def checkuser(chat_id, chat_usr):
"""Send a message when the command /start is issued."""
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('SELECT COUNT(*) FROM user WHERE username=? or chat_id=?', (chat_usr, chat_id))
data = c.fetchall()
if data.pop(0)[0] == 0:
print("new user found")
c.execute('INSERT INTO user(username, chat_id) VALUES (?,?)', (chat_usr, chat_id))
else:
# update the infos
c.execute('UPDATE user SET username=?, chat_id=? WHERE chat_id=?', (chat_usr, chat_id, chat_id))
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.commit()
conn.close()
def getuser(chat_id):
"""Send a message when the command /start is issued."""
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('SELECT * FROM user WHERE chat_id=? ', (chat_id,))
data = c.fetchall()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
if len(data) != 0:
cod = data.pop(0)[0]
else:
cod = -1
conn.close()
return cod
def print_appointment(row):
temp = ""
temp += ("Appointment: " + str(row[2]) + "[cod:" + str(row[0]) + "]\n")
if row[3] != 0:
temp += ("at: " + str(row[3]))
if row[5] != 0:
temp += (" " + str(row[5]))
if row[4] != 0:
temp += ("\nin: " + str(row[4]))
return temp
def delete_old_schedule(bot, update):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('SELECT * FROM todos')
data = c.fetchall()
if len(data) != 0:
actual_date = date.today()
for row in data:
# data error in the record -> DELETE
if dateparser(row[3]) == "" or dateparser(row[3]) == -1:
# print("DELETE FROM todos WHERE tid=%s" % (row[0],))
c.execute('DELETE FROM todos WHERE tid=?', (row[0],))
# date is just expired so the appointment is old -> DELETE
if dateparser(row[3]) < actual_date:
# print("DELETE FROM todos WHERE tid=%s" % (row[0],))
c.execute('DELETE FROM todos WHERE tid=?', (row[0],))
conn.commit()
conn.close()
update.message.reply_text("Db cleaned Successfully my lord! 🙌")
def schedule(bot, update, args):
"""Echo the user message."""
chat_id = update.message.chat_id
chat_usr = update.message.from_user.username
# check user existance (and update his info) or create a new one
checkuser(chat_id, chat_usr)
# get the user-id to execute the query
codu = getuser(chat_id)
if codu == -1:
update.message.reply_text("Master i think there is some kind of problem with me...\n"
"i can't do what you are asking me!\n")
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('SELECT * FROM todos WHERE uid=?', (codu,))
data = c.fetchall()
if len(data) != 0:
actual_date = date.today()
appointments = ""
if len(args) != 1:
if len(args) == 0:
# daily schedule
update.message.reply_text("😵 Master, your daily appointment are:\n\n")
for row in data:
if dateparser(row[3]) != -1 and dateparser(row[3]) == actual_date:
appointments += ("📌 " + print_appointment(row)+"\n")
update.message.reply_text(appointments+"\n")
else:
# daily schedule
update.message.reply_text("👮 Master i think you wrote something wrong...\n\n")
else:
if args[0].find("/") != -1 or args[0].find("-") != -1:
# args is a date -> schedule of that day
update.message.reply_text("😵 Master, your appointments for " + args[0] + "are:\n\n")
for row in data:
if dateparser(row[3]) != -1 and dateparser(row[3]) == dateparser(args[0]):
appointments += ("📌 " + print_appointment(row) + "\n")
else:
update.message.reply_text("😵 wrong data format boss! I cant help you \n\n")
update.message.reply_text(appointments + "\n")
elif args[0] == "all":
# args is all -> all my schedule
update.message.reply_text("😵 Master, your appointments scheduled till now are:\n\n")
for row in data:
appointments += ("📌 " + print_appointment(row) + "\n")
update.message.reply_text(appointments + "\n")
else:
# arg is a number -> period schedule
update.message.reply_text("😵 Master, your daily appointment are:\n\n")
for row in data:
if dateparser(row[3]) != -1 and dateparser(row[3]) < (actual_date + timedelta(days=int(args[0]))):
appointments += ("📌 " + print_appointment(row) + "\n")
update.message.reply_text(appointments + "\n")
conn.close()
update.message.reply_text("\nWanna know something else??\n"
"Try:\n\n"
" /schedule - for show all daily appointment\n"
" /schedule <dd/mm/yy> - for schedule of a certain day\n"
" /schedule <# of days> - for next #n days schedule\n"
" /schedule all - for all your appointment\n")
else:
update.message.reply_text("Master i don't remember anything for today! So take it easy and relax 🎉 \n\n")
def own(bot, update):
update.message.reply_text("My Lord is the real IEC")
def main():
"""Start the bot."""
# Create the EventHandler and pass it your bot's token.
updater = Updater("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on different commands - todo ones
dp.add_handler(CommandHandler("remember", remember,
pass_args=True,
pass_job_queue=True,
pass_chat_data=True))
dp.add_handler(CommandHandler("forget", forget,
pass_args=True,
pass_chat_data=True))
dp.add_handler(CommandHandler("schedule", schedule,
pass_args=True))
dp.add_handler(CommandHandler("info", info,
pass_args=True))
dp.add_handler(CommandHandler("free", free,
pass_chat_data=True))
dp.add_handler(CommandHandler("clean", delete_old_schedule))
dp.add_handler(CommandHandler("set", set_timer,
pass_args=True,
pass_job_queue=True,
pass_chat_data=True))
dp.add_handler(CommandHandler("unset", unset,
pass_chat_data=True))
# true owner egg
dp.add_handler(CommandHandler("own", own))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, default))
dp.add_handler(MessageHandler(Filters.command, manage_command))
# Log all errors
dp.add_error_handler(error)
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()