-
Notifications
You must be signed in to change notification settings - Fork 0
/
todoistbot.py
383 lines (295 loc) · 14.1 KB
/
todoistbot.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
#! python3
# -*- coding: utf-8 -*-
import os
try:
from commands import Path
except ImportError:
import os
os.system("pip install git+https://github.com/egigoka/commands")
from commands import Path
try:
import telebot
except ImportError:
from commands.pip9 import Pip
Pip.install("pytelegrambotapi")
import telebot
from todoiste import *
import telegrame
__version__ = "1.8.1"
my_chat_id = 5328715
ola_chat_id = 550959211
tgx_chat_id = 619037205
class Arguments:
pass
class State:
def __init__(self):
f = Path.safe__file__(os.path.split(__file__)[0])
json_path = Path.combine(f, "configs", "telegram_bot_todoist.json")
self.config_json = Json(json_path)
self.getting_project_name = False
self.getting_item_name = False
class JsonList(list):
def __init__(self, list_input, category, property):
list.__init__(self, list_input)
self.category = category
self.property = property
def append(self, obj):
out = list.append(self, obj)
self.save()
return out
def remove(self, obj):
out = list.remove(self, obj)
self.save()
return out
def save(self):
State.config_json[self.category][self.property] = self
State.config_json.save()
def purge(self):
while self:
self.pop()
self.save()
try:
self.excluded_projects = JsonList(self.config_json["excluded"]["projects"], "excluded", "projects")
except KeyError:
self.excluded_projects = JsonList([], "excluded", "projects")
try:
self.excluded_items = JsonList(self.config_json["excluded"]["items"], "excluded", "items")
except KeyError:
self.excluded_items = JsonList([], "excluded", "items")
self.counter_for_left_items = True
self.counter_for_left_items_int = 0
self.counter_all_items = 0
self.all_todo_str = ""
self.last_todo_str = ""
self.sent_messages = 1
self.last_radnom_todo_str = "not inited"
self.last_updated = 0
State = State()
encrypted_telegram_token = [-15, -21, -49, -16, -63, -52, -46, 6, -20, -13, -40, -6, -39, -33, 22, 0, 1, 51, 9, -26,
-41, -24, 13, 4, 49, 44, -25, 18, 9, -18, -19, 72, -12, -26, -3, 3, -62, 3, 17, 4, 7, -3,
-33, -3, -12]
encrypted_todoist_token = [-20, -20, -50, -14, -61, -54, 2, 0, 32, 27, -51, -21, -54, -53, 4, 3, 29, -14, -51, 29, -10,
-6, 1, 4, 28, 29, -55, -17, -59, -9, 2, 50, -13, -14, -52, -15, -56, -59, -44, 5]
def reset_password():
password = Str.input_pass()
GIV["api_password"] = password
return password
try:
password = GIV["api_password"]
if "reset" in sys.argv:
password = reset_password()
except (NameError, KeyError):
password = reset_password()
telegram_token = Str.decrypt(encrypted_telegram_token, password)
todoist_api_key = Str.decrypt(encrypted_todoist_token, password)
def get_random_todo(todo_api, telegram_api, chat_id):
Print.rewrite("Getting random todo")
bench = Bench(prefix="Get random item in", quiet=True)
bench.start()
incomplete_items = todo_api.all_incomplete_items_in_account()
# Print.debug(Print.prettify(incomplete_items, quiet=True))
bench.end()
counter_for_left_items_int = 0
counter_all_items = 0
all_todo_str = ""
for project_name, project_items in Dict.iterable(incomplete_items.copy()): # removing excluded
counter_all_items += len(project_items)
if project_name.strip() in State.excluded_projects:
incomplete_items[project_name] = []
continue
if project_items:
# print(f'"{project_name}"')
all_todo_str += project_name + newline
for item in project_items.copy():
if item["content"].strip() in State.excluded_items:
incomplete_items[project_name].remove(item)
# print(f' "{item["content"]}" excluded')
else:
counter_for_left_items_int += 1
# print(f' "{item["content"]}"')
all_todo_str += " " + item["content"] + newline
for project_name, project_items in Dict.iterable(incomplete_items.copy()): # removing empty projects
if not project_items:
incomplete_items.pop(project_name)
# Print.debug("counter_for_left_items_int", counter_for_left_items_int,
# "counter_all_items", counter_all_items)
# "all_todo_str", all_todo_str)
State.counter_for_left_items_int = counter_for_left_items_int
State.counter_all_items = counter_all_items
State.all_todo_str = all_todo_str
try:
random_project_name, random_project_items = Random.item(incomplete_items)
except IndexError:
return "All done!"
random_item = Random.item(random_project_items)
try:
if not random_item["due_date_utc"].endswith("20:59:59 +0000"):
time_string = " " + random_item["date_string"]
else:
time_string = ""
except KeyError:
time_string = ""
if State.counter_for_left_items and telegram_api and chat_id:
counter_for_left_items_str = f"({State.counter_for_left_items_int}/{State.counter_all_items} left)"
telegrame.send_message(telegram_api, chat_id, f"{counter_for_left_items_str} v{__version__}")
Print.rewrite()
return f"{random_item['content']} <{random_project_name}>{time_string}".replace(
"> (", "> (")
def todo_updater(todo_api, telegram_api, chat_id):
if Time.delta(State.last_updated, Time.stamp()) < 3:
Print("skip updating, thread already running")
return
State.last_radnom_todo_str = get_random_todo(todo_api=todo_api, telegram_api=telegram_api, chat_id=chat_id)
State.last_updated = Time.stamp()
def start_todoist_bot():
todoist_api = Todoist(todoist_api_key)
telegram_api = telebot.TeleBot(telegram_token, threaded=False)
todo_updater(todoist_api, None, None) # initing for fist message, no chat_id
@telegram_api.message_handler(content_types=["text"])
def reply_all_messages(message):
def main_message():
State.sent_messages = 1
main_markup = telebot.types.ReplyKeyboardMarkup()
main_button = telebot.types.KeyboardButton('MOAR!')
settings_button = telebot.types.KeyboardButton('Settings')
list_button = telebot.types.KeyboardButton('List')
main_markup.row(main_button)
main_markup.row(settings_button, list_button)
if State.excluded_projects:
excluded_str = f"Excluded projects: {State.excluded_projects}."
else:
excluded_str = "No excluded projects."
if State.excluded_items:
excluded_str += f"{newline}Excluded items: {State.excluded_items}."
else:
excluded_str += f"{newline}No excluded items."
current_todo = State.last_radnom_todo_str
telegrame.send_message(telegram_api, chat_id=message.chat.id,
# text=f"{excluded_str}{newline}{current_todo}") # , reply_markup=main_markup)
text=current_todo, reply_markup=main_markup)
State.last_todo_str = Str.substring(current_todo, "", "<").strip()
todo_updater_thread = MyThread(todo_updater, args=(todoist_api, telegram_api, message.chat.id), daemon=True, quiet=False)
todo_updater_thread.start()
if message.chat.id != my_chat_id:
telegrame.send_message(telegram_api, message.chat.id, "ACCESS DENY!")
return
if State.getting_project_name:
if message.text == "Cancel":
pass
else:
message_text = message.text.strip()
if message_text in State.excluded_projects:
State.excluded_projects.remove(message_text)
else:
State.excluded_projects.append(message_text)
State.getting_project_name = False
main_message()
elif State.getting_item_name:
if message.text == "Cancel":
pass
else:
message_text = message.text.strip()
if message_text in State.excluded_items:
State.excluded_items.remove(message_text)
else:
State.excluded_items.append(message_text)
State.getting_item_name = False
State._message = True
main_message()
elif message.text == "MOAR!": # MAIN MESSAGE
main_message()
elif message.text == "List":
if not State.all_todo_str:
get_random_todo(todoist_api, None, None)
if State.all_todo_str:
telegrame.send_message(telegram_api, message.chat.id, State.all_todo_str)
else:
telegrame.send_message(telegram_api, message.chat.id, "Todo list for today is empty!")
elif message.text == "Settings":
markup = telebot.types.ReplyKeyboardMarkup()
project_exclude_button = telebot.types.KeyboardButton("Exclude project")
project_include_button = telebot.types.KeyboardButton("Include project")
items_exclude_button = telebot.types.KeyboardButton("Exclude items")
items_include_button = telebot.types.KeyboardButton("Include items")
clean_black_list_button = telebot.types.KeyboardButton("Clean black list")
counter_for_left_items_button = telebot.types.KeyboardButton("Toggle left items counter")
markup.row(project_exclude_button, project_include_button)
markup.row(items_exclude_button, items_include_button)
markup.row(clean_black_list_button)
markup.row(counter_for_left_items_button)
telegrame.send_message(telegram_api, message.chat.id, "Settings:", reply_markup=markup)
elif message.text == "Exclude project":
markup = telebot.types.ReplyKeyboardMarkup()
for project_name, project_id in Dict.iterable(todoist_api.projects_all_names()):
if project_name not in State.excluded_projects:
project_button = telebot.types.KeyboardButton(project_name)
markup.row(project_button)
cancel_button = telebot.types.KeyboardButton("Cancel")
markup.row(cancel_button)
telegrame.send_message(telegram_api, message.chat.id, "Send me project name to exclude:", reply_markup=markup)
State.getting_project_name = True
elif message.text == "Include project":
if State.excluded_projects:
markup = telebot.types.ReplyKeyboardMarkup()
for project_name in State.excluded_projects:
project_button = telebot.types.KeyboardButton(project_name)
markup.row(project_button)
cancel_button = telebot.types.KeyboardButton("Cancel")
markup.row(cancel_button)
telegrame.send_message(telegram_api, message.chat.id, "Send me project name to include:", reply_markup=markup)
State.getting_project_name = True
else:
telegrame.send_message(telegram_api, message.chat.id, "No excluded projects, skip...")
main_message()
elif message.text == "Exclude items":
# main_markup = telebot.types.ForceReply(selective=False) it doesn't show up default keyboard :(
markup = telebot.types.ReplyKeyboardMarkup()
default_items = False
default_items_list = [r"Vacuum/sweep", "Wash the floor"]
if State.last_todo_str:
default_items_list.append(State.last_todo_str)
for item_name in default_items_list:
if item_name not in State.excluded_items:
project_button = telebot.types.KeyboardButton(item_name)
markup.row(project_button)
default_items = True
if not default_items:
project_button = telebot.types.KeyboardButton("Enter item manually")
markup.row(project_button)
cancel_button = telebot.types.KeyboardButton("Cancel")
markup.row(cancel_button)
telegrame.send_message(telegram_api, message.chat.id, "Send me item name:", reply_markup=markup)
State.getting_item_name = True
elif message.text == "Include items":
if State.excluded_items:
markup = telebot.types.ReplyKeyboardMarkup()
for item_name in State.excluded_items:
project_button = telebot.types.KeyboardButton(item_name)
markup.row(project_button)
cancel_button = telebot.types.KeyboardButton("Cancel")
markup.row(cancel_button)
telegrame.send_message(telegram_api, message.chat.id, "Send me item name:", reply_markup=markup)
State.getting_item_name = True
else:
telegrame.send_message(telegram_api, message.chat.id, "No excluded items, skip...")
main_message()
elif message.text == "Clean black list":
State.excluded_items.purge()
State.excluded_projects.purge()
main_message()
elif message.text == "Toggle left items counter":
if State.counter_for_left_items:
State.counter_for_left_items = False
else:
State.counter_for_left_items = True
main_message()
else:
telegrame.send_message(telegram_api, message.chat.id, f"ERROR! <{message.text}>")
State.sent_messages += 1
main_message()
telegram_api.polling(none_stop=True)
# https://github.com/eternnoir/pyTelegramBotAPI/issues/273
def main():
telegrame.very_safe_start_bot(start_todoist_bot)
if __name__ == '__main__':
main()