def get_reactions_file(self, tg_id: int): msg = ('Загрузите .csv первая строка название реакции,' 'столбцами варианты, реакция "std" присваивается автоматически') tools.send_menu_msg(tg_id, msg, exit_menu=True) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': UPLOAD_REACTIONS_PREFIX})
def show(self, tg_id: int): msg = self.name user_context = mem.UserContext(tg_id) user_context.update_context('story_id', str(self.id)) buttons = [ [('Посмотреть главы', tools.make_call_back(SHOW_CHAPTERS_PREFIX))], [ ('Переименовать', tools.make_call_back(RENAME_PREFIX)), ('Загрузить реакции', tools.make_call_back(UPLOAD_REACTIONS_PREFIX)), ], [ (f'Задержка {self.base_timeout}c', tools.make_call_back(SET_BASE_TIMEOUT)), (f'Скорость набора {self.k_timeout} знак/мин', tools.make_call_back(SET_K_TIMEOUT)), ], [('Удалить', tools.make_call_back(RM_PREFIX, {'is_sure': False}))], [('Все истории', tools.make_call_back(tg_user.SHOW_STORIES_PREFIX)) ], ] if self.is_reactions: buttons[1].append( ('Реакции', tools.make_call_back(DOWNLOAD_REACTIONS_PREFIX))) tools.send_menu_msg(tg_id, msg, buttons)
def rename_chapter(call): user_context = mem.UserContext(call.from_user.id) _chapter = chapter.Chapter( int(user_context.get_context('story_id')), int(user_context.get_context('chapter_id')), ) _chapter.get_new_name(call.from_user.id)
def show(self, tg_id: int): msg = f'id: {self.id}\n{self.name}' user_context = mem.UserContext(tg_id) user_context.update_context('chapter_id', str(self.id)) if self.start_message: buttons = [[('Все сообщения', None, '{app_url}chapter/{chapter_uid}'.format( app_url=APP_URL, chapter_uid=hashlib.sha224( bytes(f'{self.id}{tg_id}', 'utf-8')).hexdigest(), ))]] else: buttons = [[('Написать первое сообщение', tools.make_call_back(message.MAKE_PREFIX, { 'is_start_chapter_msg': True, }))]] buttons.extend([ [ ('Переименовать', tools.make_call_back(RENAME_PREFIX)), ('Удалить', tools.make_call_back(RM_PREFIX, {'is_sure': False})), ], [ ('Все главы', tools.make_call_back(story.SHOW_CHAPTERS_PREFIX)), ], ]) tools.send_menu_msg(tg_id, msg, buttons)
def wait_line_upload_reactions(message): user_context = mem.UserContext(message.from_user.id) user_context.rm_status() _story = story.Story(user_context.get_context('story_id')) document = bot.get_file(message.document.file_id) _story.set_reaction(message.from_user.id, bot.download_file(document.file_path))
def rm_story(call): params = tools.get_call_back_params(call.data) user_context = mem.UserContext(call.from_user.id) _story = story.Story(int(user_context.get_context('story_id'))) if params.get('is_sure'): _story.rm(call.from_user.id) else: _story.make_sure_rm(call.from_user.id)
def wait_line_rename(message): user_context = mem.UserContext(message.from_user.id) user_context.rm_status() _chapter = chapter.Chapter( user_context.get_context('story_id'), user_context.get_context('chapter_id'), ) _chapter.rename(message.from_user.id, message.text)
def down_chapter(call): user_context = mem.UserContext(call.from_user.id) params = tools.get_call_back_params(call.data) _chapter = chapter.Chapter( int(user_context.get_context('story_id')), params.get('chapter_id'), ) _chapter.replace(_chapter.number + 1, call.from_user.id)
def get_name_for_new_story(tg_id: int): msg = 'Напишите название своей истории' tools.send_menu_msg(tg_id, msg, exit_menu=True) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({ 'call_to': MAKE_PREFIX, })
def show_chapter(call): params = tools.get_call_back_params(call.data) user_context = mem.UserContext(call.from_user.id) _chapter = chapter.Chapter( int(user_context.get_context('story_id')), params.get('chapter_id') or user_context.get_context('chapter_id'), ) _chapter.show(call.from_user.id)
def get_line(self, tg_id: int, text: str, prefix: str): buttons = [ [('Назад', tools.make_call_back(SHOW_PREFIX))], ] tools.send_menu_msg(tg_id, text, buttons) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': prefix})
def get_direct_link(self, tg_id: int): text = 'ID сообщения:' buttons = [ [('Назад', tools.make_call_back(SHOW_PREFIX))], ] tools.send_menu_msg(tg_id, text, buttons) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': ADD_DIRECT_LINK_PREFIX})
def get_new_btn(tg_id: int, is_start_chapter_msg: bool = False): text = 'Ответ:' buttons = [ [('Назад', tools.make_call_back(SHOW_PREFIX))], ] tools.send_menu_msg(tg_id, text, buttons) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': MAKE_PREFIX})
def get_timeout(self, tg_id: int): text = 'Новая задержка:' buttons = [ [('Назад', tools.make_call_back(SHOW_PREFIX))], ] tools.send_menu_msg(tg_id, text, buttons) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': EDIT_TIMEOUT_PREFIX})
def get_new_btn(self, tg_id: int): text = 'Ответ:' buttons = [ [('Назад', tools.make_call_back(SHOW_PREFIX))], ] tools.send_menu_msg(tg_id, text, buttons) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': ADD_BUTTON_PREFIX})
def get_new_msg(self, tg_id: int): text = 'Новое сообщение:' buttons = [ [('Назад', tools.make_call_back(SHOW_PREFIX))], ] tools.send_menu_msg(tg_id, text, buttons) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': EDIT_PREFIX})
def wait_line_base_timeout(message): user_context = mem.UserContext(message.from_user.id) user_context.rm_status() _story = story.Story(user_context.get_context('story_id')) try: int(message.text) except: _story.show(message.from_user.id) else: _story.set_base_timeout(message.from_user.id, int(message.text))
def rm_chapter(call): params = tools.get_call_back_params(call.data) user_context = mem.UserContext(call.from_user.id) _chapter = chapter.Chapter( int(user_context.get_context('story_id')), int(user_context.get_context('chapter_id')), ) if params.get('is_sure'): _chapter.rm(call.from_user.id) else: _chapter.make_sure_rm(call.from_user.id)
def get_text_btn(self, tg_id: int, btn_id: int): btn = list(btn for btn in self.buttons if btn.get('id') == btn_id)[0] text = f'Текущий ответ: {btn["text"]}\n\n Новый ответ:' buttons = [ [('Назад', tools.make_call_back(SHOW_PREFIX))], ] tools.send_menu_msg(tg_id, text, buttons) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({ 'call_to': EDIT_BUTTON_PREFIX, 'btn_id': btn_id })
def make_new_chapter(tg_id: int, chapter_name: str): user_context = mem.UserContext(tg_id) new_chapter_resp = json.loads( requests.post( DB_URL.format(item='chapter', cmd='make'), json={ 'tg_id': tg_id, 'story_id': user_context.get_context('story_id'), 'chapter_name': chapter_name, }, ).text) _chapter = Chapter(user_context.get_context('story_id'), new_chapter_resp.get('id')) _chapter.show(tg_id)
def make_new_msg(tg_id: int, data: dict, content_type: str): user_context = mem.UserContext(tg_id) params = user_context.get_params() from_msg_id = params.get('from_msg_id') from_btn_id = params.get('from_btn_id') req_data = { 'tg_id': tg_id, 'content_type': content_type, 'message': data.get('message'), 'story_id': user_context.get_context('story_id'), 'chapter_id': user_context.get_context('chapter_id'), 'parrent_message_id': from_msg_id if from_msg_id and not from_btn_id else None, 'is_start_msg': True if params.get('is_start_chapter_msg') else False, } new_msg_resp = json.loads( requests.post( DB_URL.format(item='message', cmd='make'), json=req_data, ).text) if content_type in ['photo', 'voice', 'video_note']: files = { 'file_data': (data['name'], data['media'], data['content_type']) } payload = { 'tg_id': tg_id, 'message_id': new_msg_resp.get('id'), } requests.post( DB_URL.format(item='media', cmd='make'), files=files, data=payload, ) if from_msg_id and from_btn_id: requests.post( DB_URL.format(item='message', cmd='edit_button'), json={ 'tg_id': tg_id, 'msg_id': from_msg_id, 'button_id': from_btn_id, 'link_to_msg_id': new_msg_resp.get('id'), }, ) user_context.flush_params() _message = Message(new_msg_resp.get('id')) _message.show(tg_id)
def show(self, tg_id: int): text = self.message user_context = mem.UserContext(tg_id) user_context.rm_status() user_context.update_context('message_id', str(self.id)) buttons = [] for btn in self.buttons: buttons.append([(btn['text'], tools.make_call_back(ADD_BUTTON_PREFIX))]) buttons.extend([ [ ('Добавить ответ', tools.make_call_back(ADD_BUTTON_PREFIX)), ], [ ('Редактировать', tools.make_call_back(EDIT_PREFIX)), ('К главе', tools.make_call_back(chapter.SHOW_PREFIX)) ], ]) tools.send_menu_msg(tg_id, text, buttons)
def get_new_msg( tg_id: int, is_start_chapter_msg: bool = False, from_msg_id: int = None, from_btn_id: int = None, ): msg = 'Ваше сообщение:' tools.send_menu_msg(tg_id, msg, exit_menu=True) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({ 'call_to': MAKE_PREFIX, 'is_start_chapter_msg': '1' if is_start_chapter_msg else '', 'from_msg_id': str(from_msg_id) if from_msg_id else '', 'from_btn_id': str(from_btn_id) if from_btn_id else '', })
def start_message(msg): link_data = extract_link_data(msg.text) user = tg_user.TelegramUser(msg.chat.id) user_context = mem.UserContext(user.telegram_id) user_context.flush_all() if link_data and link_data.get('edit_message_id') and link_data.get( 'story_id'): if int(link_data.get('story_id')) in [ story.id for story in user.stories ]: _message = message.Message(link_data.get('edit_message_id')) user_context.update_context('story_id', link_data.get('story_id')) try: chapter.Chapter(link_data.get('story_id'), _message.chapter_id) except Exception: user.show_stories() return user_context.update_context('chapter_id', _message.chapter_id) _message.show(user.telegram_id) else: user.show_stories()
def make_new_btn(tg_id: int, msg: str, text: str): user_context = mem.UserContext(tg_id) new_btn_msg_resp = json.loads( requests.post( DB_URL.format(item='message', cmd='add_button'), json={ 'msg_id': user_context.get_context('message_id'), 'chapter_id': user_context.get_context('chapter_id'), 'tg_id': tg_id, 'text': text, }, ).text ) if new_btn_msg_resp.get('error'): msg = new_btn_msg_resp.get('error') tools.send_menu_msg(tg_id, msg) else: _button = Button( user_context.get_context('message_id'), new_btn_msg_resp.get('buttons')[-1].get('id'), ) _button.show(tg_id)
def wait_line_make(message): user_context = mem.UserContext(message.from_user.id) user_context.rm_status() chapter.make_new_chapter(message.from_user.id, message.text)
def get_timeout(self, tg_id: int, prefix: str): msg = 'Hовое значение' tools.send_menu_msg(tg_id, msg, exit_menu=True) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': prefix})
def get_new_name(self, tg_id: int): msg = 'Напишите новое название для истории "{}"'.format(self.name) tools.send_menu_msg(tg_id, msg, exit_menu=True) user_context = mem.UserContext(tg_id) user_context.set_status('wait_line') user_context.set_params({'call_to': RENAME_PREFIX})
def show_chapters(call): user_context = mem.UserContext(call.from_user.id) _story = story.Story(int(user_context.get_context('story_id'))) _story.show_chapters(call.from_user.id)
def wait_line_rename(message): user_context = mem.UserContext(message.from_user.id) user_context.rm_status() _story = story.Story(user_context.get_context('story_id')) _story.rename(message.from_user.id, message.text)