-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
201 lines (163 loc) · 5.42 KB
/
main.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
import os
import logging
import putiopy
from database import User
from menus import MAIN_MENU, FILES_MENU
from utils import human_readable_bytes, required_user_data
from telegram import InlineKeyboardMarkup
from telegram.ext import (
Updater,
CommandHandler,
CallbackQueryHandler,
# ConversationHandler,
# MessageHandler,
# Filters,
# RegexHandler,
)
# LOGGER for warnings, errors, debug outputs etc.
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
# fundamentals
logger = logging.getLogger(__name__)
bot_token = os.environ['BOT_TOKEN']
client = putiopy.Client(os.environ['PUT_IO_TOKEN'])
# put.io folder to collect user files (/BOT_USERS/)
USERS_DIR_ID = 111111111
# catch errors
def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)
# /start function
def start(bot, update, user_data):
# create user instance in the db if doesn't exist
markup = InlineKeyboardMarkup(MAIN_MENU)
user = User.select().where(User.telegram_id == update.effective_user.id)
user = user.first() or False
# create new user from update
if not user:
try:
# but before, we need to create a folder for user
create_dir = client.File.create_folder(
name=update.effective_user.id, parent_id=USERS_DIR_ID)
# then create a user
if create_dir:
user = User.create(
telegram_id=update.effective_user.id,
first_name=update.effective_user.first_name,
last_name=update.effective_user.last_name,
username=update.effective_user.username,
user_dir=create_dir.id,
)
# greeting
if user:
message = (
"New user added to the DB. Welcome message "
"will be placed here.")
except Exception as e:
# sure we could check for specific putio errors but it has no
# specific errors. it just returns client error if something goes
# wrong
logger.error(e)
message = "There is something wrong. Please try again later."
markup = None
else:
# user already exist
message = "Greeting message for the existing user."
# send the message
update.message.reply_text(
message,
reply_markup=markup,
)
user_data[update.effective_user.id] = user or None
# main menu
def main_menu(bot, update, user_data):
callback = update.callback_query
markup = InlineKeyboardMarkup(MAIN_MENU)
callback.message.edit_text(
"Sample text for main menu with inline buttons.",
reply_markup=markup,
)
# menu to list files
@required_user_data
def menu_files(bot, update, user_data):
# TODO: Pagination
user = user_data[update.effective_user.id]
callback = update.callback_query
markup = InlineKeyboardMarkup(FILES_MENU)
try:
files = client.File.list(parent_id=user.user_dir)
except Exception as e:
logger.error(e)
file_list = ''
folder_list = ''
for f in files:
size = human_readable_bytes(f.size)[0]
if f.file_type.upper() != 'FOLDER':
# if the file type is not "FOLDER"
file_list += (
"<code>{}</code> " # FILE ID
"<a href='{}'>{}</a> " # FILE LINK AND NAME
"<b>{}</b>" # FILE SIZE
"".format(f.id, '#', f.name, size)
)
file_list += '\n'
else:
# the case if the file type is equal to "FOLDER"
folder_list += (
"<code>{}</code> " # FOLDER ID
"<a href='{}'>{}</a> " # FOLDER LINK AND NAME
"<b>{}</b>" # FOLDER SIZE
"".format(f.id, '#', f.name, size)
)
folder_list += '\n'
message = (
"The message of the files menu which presents the files of the user."
"\n\n"
"<b>FILES</b>\n{}\n\n"
"<b>FOLDERS</b>\n{}\n\n".format(file_list, folder_list)
)
callback.message.edit_text(
message,
reply_markup=markup,
disable_web_page_preview=True,
parse_mode='HTML',
)
# definition to do some tests
@required_user_data
def test_case(bot, update, user_data):
update.message.reply_text('TEST CASE.')
def main():
updater = Updater(bot_token)
# Get the dispatcher to register handlers
dp = updater.dispatcher
dp.add_handler(CommandHandler(
'start',
start,
pass_user_data=True,
))
dp.add_handler(CommandHandler(
'test',
test_case,
pass_user_data=True,
))
dp.add_handler(CallbackQueryHandler(
main_menu,
pattern='^main$',
pass_user_data=True
))
dp.add_handler(CallbackQueryHandler(
menu_files,
pattern='^files$',
pass_user_data=True
))
# 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()