-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbot.py
More file actions
429 lines (390 loc) · 14.4 KB
/
bot.py
File metadata and controls
429 lines (390 loc) · 14.4 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
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
# Import some libraries
import logging
from bs4 import BeautifulSoup
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup
from telegram.ext import *
import os, json, requests, re
from datetime import datetime as dt
import datetime
# USER defined variables
TOKEN = ''
REMNOT_API= ''
USER_ID = ''
HEROKU_NAME = ''
HOME_DIR = 'Saved Telegram'
# Some global variables
PORT = os.environ.get('PORT')
NOTE_ID = None
PARENT = None
NOTE = None
MEDIA_ID = None
FIRST, SECOND, THIRD, FOURTH, FIFTH = range(5)
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Get the todays date!
def suffix(d):
return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')
def custom_strftime(format, t):
return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))
today = custom_strftime('%B {S}, %Y', dt.now())
# Send notes
def send_note(text, parent=None, link=False, doc=None):
global REMNOT_API, USER_ID, DOCUMENT, MEDIA_ID, today, URL
url_post="https://api.remnote.io/api/v0/create"
parent_Id = parent
if parent_Id == None:
parent_Id = get_daily_rem()
if parent_Id == None:
parent_Id = search_parent()
text = text + "#[["+today+"]]"
par={
"apiKey": REMNOT_API,
"userId":USER_ID,
"text": text,
"parentId": parent_Id
}
if link == True:
par['isDocument'] = True
par['source'] = URL
reqs = requests.get(URL)
soup = BeautifulSoup(reqs.text, 'html.parser')
try:
par['text'] = str(soup.find_all('title')[0].get_text()) + '#[['+today+']]'
except:
par['text'] = str(URL)
elif DOCUMENT==True:
par['source'] = open(f"{MEDIA_ID}.pdf", 'rb')
par['isDocument'] = True
par['text'] = MEDIA_ID
created_rem = requests.post(url_post, data=par)
return created_rem.json()['remId']
def get_daily_rem():
global REMNOT_API, USER_ID, today
url_get_by_name = "https://api.remnote.io/api/v0/get_by_name"
search={
"apiKey": REMNOT_API,
"userId":USER_ID,
"name":datetime.date.today().strftime('%d/%m/%Y'),
}
res = requests.post(url_get_by_name, data=search)
if res.json()["found"] == True:
return res.json()["_id"]
elif res.json()["found"] == False:
search['name'] = today
res = requests.post(url_get_by_name, data=search)
if res.json()["found"] == True:
return res.json()["_id"]
else:
url_get_by_name = "https://api.remnote.io/api/v0/get_by_name"
search={ "apiKey": REMNOT_API,
"userId":USER_ID,
"name":'Daily Documents'}
res = requests.post(url_get_by_name, data=search)
url="https://api.remnote.io/api/v0/create"
par={ "apiKey":REMNOT_API,
"userId":USER_ID,
"text": "Temporary" + " #[[" + str(today) + "]]",
"positionAmongstSiblings" : 0,
"isDocument" : True,
"parentId":res.json()["_id"]
}
created_rem = requests.post(url, data=par)
delete = { "apiKey": REMNOT_API,
"userId":USER_ID,
"remId" : created_rem.json()['remId']
}
delete_url = "https://api.remnote.io/api/v0/delete"
requests.post(delete_url, data=delete)
search['name'] = datetime.date.today().strftime('%d/%m/%Y')
res = requests.post(url_get_by_name, data=search)
if res.json()["found"] == False:
search['name'] = 'Daily Documents'
res = requests.post(url_get_by_name, data=search)
par['parentId'] = res.json()["_id"]
created_rem = requests.post(url, data=par)
delete['remId'] = created_rem.json()['remId']
requests.post(delete_url, data=delete)
search['name'] = today
res = requests.post(url_get_by_name, data=search)
return res.json()["_id"]
else:
return res.json()["_id"]
# Start the conversation
def start_doc(update, context):
global DOCUMENT, LINK, MEDIA_ID
DOCUMENT = True
LINK = False
media = context.bot.get_file(update.message.document)
photo = context.bot.get_file(media.file_id)
MEDIA_ID = media.file_id
photo.download(f"{media.file_id}.pdf")
if PARENT != None:
send_note(text=update.message.text, parent=PARENT, doc=media.file_id)
else:
update.message.reply_text("Need some extra details...",
reply_markup=main_menu_keyboard())
return FIRST
def start(update, context):
"""Echo the user message."""
global LINK, DOCUMENT, NOTE, PARENT, URL
DOCUMENT = False
NOTE = update.message.text
try:
text_to_search = str(NOTE).rsplit()
URL=None
for i in text_to_search:
if URL != None:
break
else:
try:
URL = re.search("(?P<url>https?://[^\s]+)", str(i)).group("url")
except:
pass
requests.get(URL)
LINK = True
except:
LINK = False
if PARENT != None:
send_note(text=update.message.text, parent=PARENT)
else:
update.message.reply_text("Need some extra details...",
reply_markup=main_menu_keyboard())
return FIRST
def start_photo(update, context):
global PARENT, LINK, DOCUMENT, NOTE
LINK = False
DOCUMENT = False
media = context.bot.get_file(update.message.photo[-1])
NOTE = media.file_path
if PARENT != None:
send_note(text=NOTE, parent=PARENT)
else:
update.message.reply_text("Need some extra details...",
reply_markup=main_menu_keyboard())
return FIRST
def start_voice(update, context):
global PARENT, LINK, DOCUMENT, NOTE
LINK = False
DOCUMENT = False
media = context.bot.get_file(update.message.voice)
NOTE = "&[true](" + str(media.file_path) +")"
if PARENT != None:
send_note(text=NOTE, parent=PARENT)
else:
update.message.reply_text("Need some extra details...",
reply_markup=main_menu_keyboard())
return FIRST
def start_audio(update, context):
global PARENT, LINK, DOCUMENT, NOTE
LINK = False
DOCUMENT = False
media = context.bot.get_file(update.message.audio)
NOTE = "&[true](" + str(media.file_path) +")"
if PARENT != None:
send_note(text=NOTE, parent=PARENT)
else:
update.message.reply_text("Need some extra details...",
reply_markup=main_menu_keyboard())
return FIRST
def start_video(update, context):
global PARENT, LINK, DOCUMENT, NOTE
LINK = False
DOCUMENT = False
media = context.bot.get_file(update.message.video)
NOTE = "&[anything but true](" + str(media.file_path) +")"
if PARENT != None:
send_note(text=NOTE, parent=PARENT)
else:
update.message.reply_text("Need some extra details...",
reply_markup=main_menu_keyboard())
return FIRST
def start_video_note(update, context):
global PARENT, LINK, DOCUMENT, NOTE
LINK = False
DOCUMENT = False
media = context.bot.get_file(update.message.video_note)
NOTE = "&[anything but true](" + str(media.file_path) +")"
if PARENT != None:
send_note(text=NOTE, parent=PARENT)
else:
update.message.reply_text("Need some extra details...",
reply_markup=main_menu_keyboard())
return FIRST
def search_parent():
global HOME_DIR, REMNOT_API, USER_ID
name = HOME_DIR
url_get_by_name = "https://api.remnote.io/api/v0/get_by_name"
search={
"apiKey": REMNOT_API,
"userId":USER_ID,
"name":name}
res = requests.post(url_get_by_name, data=search)
if res.json()['found'] == True:
return res.json()['_id']
else:
url_post="https://api.remnote.io/api/v0/create"
par={
"apiKey": REMNOT_API,
"userId":USER_ID,
"text": name ,
"isDocument" : True
}
created_rem = requests.post(url_post, data=par)
return created_rem.json()['remId']
# First menu hangling
def daily_docs(update, context):
"""Echo the user message."""
global LINK, DOCUMENT, NOTE, NOTE_ID, SEPARATE
SEPARATE = False
query = update.callback_query
query.answer()
if LINK == True:
query.edit_message_text(text = fifth_menu_message(),
reply_markup=fifth_menu_keyboard())
return FIFTH
else:
NOTE_ID = send_note(text=NOTE, link=LINK)
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
query.edit_message_text(text = first_menu_message(),
reply_markup=first_menu_keyboard())
return SECOND
def separate_dir(update, context):
"""Echo the user message."""
global LINK, DOCUMENT, NOTE, NOTE_ID, today, SEPARATE
SEPARATE = True
query = update.callback_query
query.answer()
if LINK == True:
query.edit_message_text(text = fifth_menu_message(),
reply_markup=fifth_menu_keyboard())
return FIFTH
else:
NOTE_ID = send_note(text=NOTE + "#[["+today+"]]", link=LINK, parent=search_parent())
query.edit_message_text(text = first_menu_message(),
reply_markup=first_menu_keyboard())
return SECOND
# Second menu hangling
def update_rem(update, context):
global PARENT, NOTE_ID
PARENT = NOTE_ID
query = update.callback_query
query.answer()
query.edit_message_text("When you done adding notes, please type /stop command")
return ConversationHandler.END
def stop_conv(update, context):
query = update.callback_query
query.answer()
query.edit_message_text("Sure.")
return ConversationHandler.END
# Fifth menu
def video_embed(update, context):
global NOTE, NOTE_ID, SEPARATE, URL
query = update.callback_query
query.answer()
NOTE = "&[anything but true](" + str(URL) +")"
if SEPARATE == True:
NOTE_ID = send_note(text=NOTE + "#[["+today+"]]", link=None, parent=search_parent())
else:
NOTE_ID = send_note(text=NOTE, link=None)
query.edit_message_text(text = first_menu_message(),
reply_markup=first_menu_keyboard())
return SECOND
def not_video_embed(update, context):
global NOTE, LINK
query = update.callback_query
query.answer()
if SEPARATE == True:
NOTE_ID = send_note(text=NOTE + "#[["+today+"]]", link=LINK, parent=search_parent())
else:
NOTE_ID = send_note(text=NOTE, link=LINK)
query.edit_message_text(text = first_menu_message(),
reply_markup=first_menu_keyboard())
return SECOND
# Stop notes adding
def stop(update, context):
global PARENT, DOCUMENT, LINK
PARENT=None
DOCUMENT = False
LINK = False
# Menus
def main_menu(update, context):
query = update.callback_query
query.answer()
query.edit_message_text(main_menu_message(),
reply_markup=main_menu_keyboard())
return FIRST
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('Save to daily notes', callback_data='daily_docs')],
[InlineKeyboardButton('Save to separate dir', callback_data='separate_dir')]]
return InlineKeyboardMarkup(keyboard)
def first_menu_keyboard():
keyboard = [[InlineKeyboardButton('Yes', callback_data='update_rem')],
[InlineKeyboardButton('No', callback_data='stop')]]
return InlineKeyboardMarkup(keyboard)
def fifth_menu_keyboard():
keyboard = [[InlineKeyboardButton('Yes', callback_data='video_embed')],
[InlineKeyboardButton('No', callback_data='not_video_embed')]]
return InlineKeyboardMarkup(keyboard)
def main_menu_message():
return 'Where to save?:'
def first_menu_message():
return 'Add some additional notes? (inside created rem):'
def fifth_menu_message():
return 'Link detected. Are you trying to embed a video?'
# Error messages
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
global PARENT, TOKEN, HEROKU_NAME, PORT
updater = Updater(TOKEN, use_context=True)
dp = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[
MessageHandler(Filters.voice, start_voice),
MessageHandler(Filters.audio, start_audio),
MessageHandler(Filters.video, start_video),
MessageHandler(Filters.video_note, start_video_note),
MessageHandler(Filters.text, start),
MessageHandler(Filters.document, start_doc),
MessageHandler(Filters.photo, start_photo)
],
states={
FIRST: [
CallbackQueryHandler(main_menu, pattern='main'),
CallbackQueryHandler(daily_docs, pattern='daily_docs'),
CallbackQueryHandler(separate_dir, pattern='separate_dir'),
],
SECOND: [
CallbackQueryHandler(update_rem, pattern="update_rem"),
CallbackQueryHandler(stop_conv, pattern="stop"),
],
FIFTH: [
CallbackQueryHandler(video_embed, pattern="video_embed"),
CallbackQueryHandler(not_video_embed, pattern="not_video_embed"),
],
},
fallbacks=[CommandHandler('start', start)],
)
dp.add_handler(CommandHandler("stop", stop))
dp.add_handler(conv_handler)
dp.add_error_handler(error)
# updater.start_polling()
updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN)
updater.bot.setWebhook(HEROKU_NAME + TOKEN)
updater.idle()
if __name__ == '__main__':
main()