-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote_bot.py
119 lines (90 loc) · 3.6 KB
/
note_bot.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
import os
from telegram.ext import Updater, CommandHandler, Filters
from telegram.ext import CallbackQueryHandler, CallbackContext, MessageHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
class LinkNoteHandler:
def __init__(self, path):
self._folder_path = os.path.abspath(path)
def get_links(self):
with open(os.path.join(self._folder_path, 'links.md'), 'r') as f:
links_note = f.read()
return links_note
@property
def categories(self):
contents = self.get_links()
# Find names of all categories from the whole doc
proj_names = contents.split('\n## ')
# split for the first 'word'
proj_names = [x.split('\n')[0] for x in proj_names[1:]]
return proj_names
class Bot:
def __init__(self, token):
self._note_handler = LinkNoteHandler(
path=os.environ['PATH_TO_NOTES']
)
updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('s', self.start))
dp.add_handler(CallbackQueryHandler(self.main_menu, pattern='main'))
dp.add_handler(CallbackQueryHandler(self.second_menu, pattern='m1'))
dp.add_handler(CallbackQueryHandler(self.log, pattern='log'))
# on non command i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(
Filters.text & ~Filters.command,
self.receive_message
))
print('bot has started')
updater.start_polling()
updater.idle()
def receive_message(self,
update: Update,
context: CallbackContext) -> None:
"""Echo the user message."""
if 'http' in update.message.text:
update.message.reply_text(
'Link:category',
reply_markup=self.link_categories_menu()
)
self.current_link = update.message.text
def start(self, update: Update, context: CallbackContext):
update.message.reply_text(
'Hi!',
reply_markup=self.main_menu_keyboard()
)
def main_menu(self, update: Update, context: CallbackContext):
query = update.callback_query
context.bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=self.main_menu_message(),
reply_markup=self.main_menu_keyboard()
)
def main_menu_keyboard(self):
keyboard = [[
InlineKeyboardButton('Log New Activity', callback_data='m1')
]]
return InlineKeyboardMarkup(keyboard)
def second_menu(self, update: Update, context: CallbackContext):
query = update.callback_query
context.bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text='Which activity?',
reply_markup=self.second_menu_keyboard()
)
def link_categories_menu(self):
keyboard = [
[InlineKeyboardButton(
name,
callback_data=f'log {name}'
)] for name in self._note_handler.categories
]
keyboard.append([InlineKeyboardButton(
'/add new/',
callback_data='add_new')])
return InlineKeyboardMarkup(keyboard)
def log(self, update: Update, context: CallbackContext):
category = update.callback_query.data.split('log ')[1]
print(update.callback_query.data)
if __name__ == '__main__':
bot = Bot(token=os.environ['NOTE_BOT_TOKEN'])