def send_zip_file_with_files(call): creator_uid = call.from_user.id user = get_user(creator_uid) user.selected_group = int(call.data[len(action.download_files_from_group):]) group = get_group(user.selected_group) tmpdir = 'files' + str(uuid.uuid4()) if not os.path.exists(tmpdir): os.makedirs(tmpdir) print('created dir', tmpdir) for uid, file_id in group.files.items(): file_info = bot.get_file(file_id) downloaded_file = bot.download_file(file_info.file_path) full_file_name = os.path.join(tmpdir, get_user(uid).username + '_' + file_info.file_path.replace('/', '_')) with open(full_file_name, 'wb') as new_file: new_file.write(downloaded_file) zip_filename = group.name + '.zip' with zipfile.ZipFile(zip_filename, 'w') as zf: for dirname, subdirs, files in os.walk(tmpdir): zf.write(dirname) for filename in files: zf.write(os.path.join(dirname, filename)) bot.send_document(call.message.chat.id, open(zip_filename, 'rb')) print('start deleting dir', tmpdir) os.remove(zip_filename) shutil.rmtree(tmpdir, ignore_errors=True) print('deleted dir', tmpdir) user.last_markup = bot.send_message(call.message.chat.id, "select an action", reply_markup=generate_menu_markup(user))
def update_user(message): uid = message.from_user.id user = get_user(uid) if user is None: # if it's new user, create User() user = add_user(message.from_user.username, uid) bot.reply_to(message, 'Hello!') return user
def parse_group_id_to_send(call): print(call.from_user) user = get_user(call.from_user.id) user.selected_group = int(call.data[len(action.send_to_group):]) bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=user.last_markup.message_id, reply_markup=types.InlineKeyboardMarkup()) bot.send_message(chat_id=call.message.chat.id, text='Now send your file, please')
def get_document(message): print(message.document.file_id) print(message.from_user.username) uid = message.from_user.id user = get_user(uid) if user.selected_group is None: bot.reply_to(message, 'Can\'t understand you') else: group = get_group(user.selected_group) group.insert(uid, message.document.file_id) user.last_markup = bot.send_message(message.chat.id, 'Ok, I received your file for group ' + group.name, reply_markup=back_to_menu) user.selected_group = None
def text_messages(message): user = get_user(message.from_user.id) print(user.username, user.status) if user.status == 'creating_group_name': group_id = create_group(user.uid, message.text) user.status = '' bot.send_message(chat_id=message.chat.id, text='group ' + message.text + ' has been created. Group invite key:') bot.send_message(chat_id=message.chat.id, text=get_group(group_id).invite_key) bot.send_message(chat_id=message.chat.id, text='Or you can just share group invite link:') bot.send_message(chat_id=message.chat.id, text=generate_invite_link(get_group(group_id).invite_key)) user.last_markup = bot.send_message(message.chat.id, "select an action", reply_markup=generate_menu_markup(user)) elif user.status == 'entering_invite_key': user.status = '' invite_key = message.text group = get_group_by_invite_key(invite_key) if group is not None: user.groups.append(group.id) bot.send_message(message.chat.id, 'You have been successfully added to the ' + group.name + ' group.') user.last_markup = bot.send_message(message.chat.id, 'select an action', reply_markup=generate_menu_markup(user))
def show_groups_to_get_files(call): print(call.from_user.username, call.message.text, call.data) user = get_user(call.from_user.id) markup = types.InlineKeyboardMarkup() if len(user.groups) == 0: markup.row_width = 1 markup.add(types.InlineKeyboardButton('You didn\'t create any group, return to menu', callback_data=action.return_to_menu)) else: markup.row_width = len(user.groups) + 1 for group_id in user.groups: group = get_group(group_id) markup.add(types.InlineKeyboardButton(group.name, callback_data=action.download_files_from_group + group_id)) markup.add(types.InlineKeyboardButton('Return to menu', callback_data=action.return_to_menu)) print(call.data, user.last_markup.message_id) bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=user.last_markup.message_id, reply_markup=markup)
def show_groups_to_send(call): print(call.from_user.username, call.message.text, call.data) user = get_user(call.from_user.id) markup = types.InlineKeyboardMarkup() if len(user.groups) == 0: markup.row_width = 1 markup.add(types.InlineKeyboardButton('You are not a member of any groups, return to menu', callback_data=action.return_to_menu)) else: markup.row_width = len(user.groups) + 1 for group in user.groups: group_name = user.name markup.add(types.InlineKeyboardButton(group_name, callback_data=action.send_to_group + groups[group].name)) markup.add(types.InlineKeyboardButton('Return to menu', callback_data=action.return_to_menu)) bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=user.last_markup.message_id, reply_markup=markup)
def create_group(creator_uid, name): group_id = len(groups) groups.append(Group(group_id, creator_uid, name, str(uuid.uuid4()))) get_user(creator_uid).created_groups.append(group_id) return group_id
def join_group_via_invite_key(call): user = get_user(call.from_user.id) user.status = 'entering_invite_key' bot.send_message(user.uid, 'Enter invite key')
def show_create_a_group_name(call): user = get_user(call.from_user.id) user.status = 'creating_group_name' bot.send_message(chat_id=call.message.chat.id, text='Create and enter group name')
def show_menu(call): user = get_user(call.from_user.id) bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=user.last_markup.message_id, reply_markup=generate_menu_markup(user))