def _start(update: Update, context: CallbackContext) -> None: bot_data = BotData.from_context(context) try: ChatData.from_context(context) except MissingDataError: ChatData(update.effective_chat).update_context(context) try: if bot_data.uuid == context.args[0]: logger.info('Adding chat to admin chat list.') bot_data.admins.append(update.effective_chat.id) else: logger.warning(f'Incorrect deep-link token: {update}') except IndexError: pass
def _show_form(update: Update, context: CallbackContext): chat_data = ChatData.from_context(context) user_data = UserData.from_context(context) chat_data.send_form(user_data, callback_data=str(REMOVE)) update.callback_query.answer() return SELECT_ACTION
def _show_help(update: Update, context: CallbackContext): text = ( 'Позже здесь появятся правила и ссылки на более другие документы:\n\n' 'За любой помощью обращайтесь к разработчику бота: @srLuxint' ) button = InlineKeyboardButton(text='Назад', callback_data=str(BACK)) keyboard = InlineKeyboardMarkup.from_button(button) chat_data = ChatData.from_context(context) chat_data.print_messages({'text': text, 'reply_markup': keyboard}) return SELECT_ACTION
def __call__(self, parser, namespace, values, option_string = None): user_data = UserData.by_id(int(values[0])) bot_data = BotData.from_context(self._context) bot = Dispatcher.get_instance().bot if not bot_data.dating_channel: raise CommandError('Не указан канал для публикации!') logger.debug('Trying to post form..') try: channel = ChatData.by_id(bot_data.dating_channel) except MissingDataError: channel = ChatData(bot.getChat(bot_data.dating_channel)) finally: channel.send_form(user_data) bot_data.pending_forms.remove(user_data.id) user_data.status = FormStatus.PUBLISHED new_conv_status(user_data.id) logger.info(f'Form was posted: {str(user_data)}')
def _show_media(update: Update, context: CallbackContext): user_data = UserData.from_context(context) chat_data = ChatData.from_context(context) question = user_data.questions[user_data.current_question] try: chat_data.send_media(user_data, question.question_type) except NoMediaError: pass update.callback_query.answer() return ASK_QUESTION
def _state(func, back = None, *args, **kwargs): # pylint: disable=W1113 chat_data = ChatData.from_context(args[1]) user_data = UserData.from_context(args[1]) if back: user_data.back = back chat_data.clear_error() chat_data.delete_form(chat_data.id) if args[0].callback_query: args[0].callback_query.answer() return func(*args, *kwargs)
def _delete_form(update: Update, context: CallbackContext): bot_data = BotData.from_context(context) user_data = UserData.from_context(context) try: user_data.status = FormStatus.BLOCKING channel = ChatData.by_id(bot_data.dating_channel) except MissingDataError: pass else: channel.delete_form(user_data.id) logger.info(f'Form has been deleted: {str(user_data)}') update.callback_query.answer('Анкета успешно удалена.') return _manage_form(update, context)
def __call__(self, parser, namespace, values, option_string = None): bot_data = BotData.from_context(self._context) bot = Dispatcher.get_instance().bot text = 'Канал для публикаций: ' try: if bot_data.dating_channel: channel = ChatData.by_id(bot_data.dating_channel) else: channel = None except MissingDataError: try: channel = ChatData(bot.getChat(bot_data.dating_channel)) except TelegramError: logger.warning(f'Channel seems to be outdated: {bot_data.dating_channel}') bot_data.dating_channel = None channel = None finally: if channel: text += channel.mention() else: text += 'отсутствует' bot.send_message(self._update.effective_chat.id, text)
def new_status(var_id: int): """Send an update message to user.""" try: data = ChatData.by_id(var_id) except MissingDataError: return keyboard = InlineKeyboardMarkup.from_button(InlineKeyboardButton( text='Посмотреть', callback_data=str(MANAGE_FORM) )) # Send user a notification data.needs_update = True data.print_messages( {'text': 'Статус анкеты изменился.', 'reply_markup': keyboard} )
def _select_level(update: Update, context: CallbackContext) -> None: text = ( 'Для работы с ботом выберите один из вариантов:' ) buttons = [ [ InlineKeyboardButton(text='Управление анкетой', callback_data=str(MANAGE_FORM)), ], [ InlineKeyboardButton(text='Помощь', callback_data=str(SHOW_HELP)), ], ] keyboard = InlineKeyboardMarkup(buttons) chat_data = ChatData.from_context(context) chat_data.print_messages({'text': text, 'reply_markup': keyboard}) return SELECT_LEVEL
def _start(update: Update, context: CallbackContext) -> None: logger.debug(f'{update.message.text}') try: UserData.from_context(context) except MissingDataError: UserData(update.message.chat).update_context(context) ChatData(update.message.chat).update_context(context) logger.debug(f'Created new user data: {update.message.chat.id}') update.message.reply_text(text=HELP, parse_mode=ParseMode.MARKDOWN_V2) try: bot_data = BotData.from_context(context) bot_data.owner = (context.args[0], update.message.chat.id) except (IndexError, MissingDataError): pass return _select_level(update, context)
def _ask_question(update: Update, context: CallbackContext) -> None: user_data = UserData.from_context(context) chat_data = ChatData.from_context(context) question = user_data.questions[user_data.current_question] show: bool = False remove: bool = False try: answer = user_data.answers[question].value except MissingDataError as exc: if question.question_type in (QuestionType.TEXT, QuestionType.DIGITS): answer = 'Не отвечено' elif question.question_type == QuestionType.AUDIO: answer = 'Нет саундтрека' elif question.question_type == QuestionType.PHOTO: answer = 'Нет фото' else: raise AssertionError from exc else: if question.question_type in (QuestionType.TEXT, QuestionType.DIGITS): pass elif question.question_type == QuestionType.AUDIO: if answer[0]: show = True answer = 'Добавлено аудио' else: answer = answer[1] elif question.question_type == QuestionType.PHOTO: show = True answer = f'Добавлено {len(answer)} фото' else: raise AssertionError remove = True chat_data.print_messages( {'text': _format_question_text(question), 'parse_mode': 'MarkdownV2'}, {'text': answer, 'reply_markup': _question_keyboard(show, remove)} ) return ASK_QUESTION
def _proc_answer(update: Update, context: CallbackContext): user_data = UserData.from_context(context) chat_data = ChatData.from_context(context) question = user_data.questions[user_data.current_question] if question.question_type == QuestionType.PHOTO: queue = context.dispatcher.update_queue updates = [update] for _ in range(queue.qsize()): updates.append(queue.get()) data = [update.message for update in updates] else: data = update.message try: user_data.answers = (question.tag, data) user_data.current_question += 1 return _ask_question(update, context) except AnswerError as exc: chat_data.print_error(str(exc)) return ASK_QUESTION
def __call__(self, parser, namespace, values, option_string = None): bot_data = BotData.from_context(self._context) text = [list(), list()] for admin in bot_data.admins: try: if admin > 0: data = UserData.by_id(admin) else: data = ChatData.by_id(admin) except MissingDataError: logger.warning(f'Unknown ID: {admin}') bot_data.admins.remove(admin) continue item = f'{admin} ({data.mention()})' if admin > 0: text[0].append(item) else: text[1].append(item) text[0] = 'Список админов: ' + ', '.join(text[0]) text[1] = 'Список админских чатов: ' + ', '.join(text[1]) bot_data._bot.send_message(self._update.effective_chat.id, '\n'.join(text))
def __call__(self, parser, namespace, values, option_string = None): user_data = UserData.by_id(int(values[0])) chat_data = ChatData.from_context(self._context) chat_data.send_form(user_data, callback_data=f'{str(REMOVE)}{user_data.id}')
def _update_chat(update: Update, context: CallbackContext): try: ChatData.from_context(context).needs_update = True except MissingDataError: ChatData(update.message.chat).update_context(context)
def _remove(update: Update, context: CallbackContext): chat_data = ChatData.from_context(context) chat_data.delete_form(chat_data.id) update.callback_query.answer()
def _manage_form(update: Update, context: CallbackContext): user_data = UserData.from_context(context) chat_data = ChatData.from_context(context) answer_button = InlineKeyboardButton(text='Ответить на вопросы', callback_data=str(ASK_QUESTION)) show_form_button = InlineKeyboardButton(text='Показать анкету', callback_data=str(SHOW_FORM)) buttons = [[], [], []] text = str() status = user_data.status if status == FormStatus.BLOCKING: buttons[0].append( answer_button ) text = ( 'Заполни анкету для отправки.' ) elif status == FormStatus.IDLE: buttons[0].extend( (answer_button, show_form_button) ) buttons[1].append( InlineKeyboardButton(text='Отправить анкету', callback_data=str(SEND_FORM)) ) text = ( 'Анкета заполнена и готова к отправке на ревью админами.' ) elif status == FormStatus.PENDING: buttons[0].append( show_form_button ) buttons[1].append( InlineKeyboardButton(text='Отозвать анкету', callback_data=str(WITHDRAW_FORM)) ) text = ( 'Анкета отправлена и ожидает ревью. Если хочешь внести правки -- ' 'сначала отзови анкету назад.' ) elif status == FormStatus.PUBLISHED: buttons[1].append( InlineKeyboardButton(text='Удалить анкету', callback_data=str(DELETE_FORM)) ) text = ( 'Анкета успешно опубликована! Поздравляю. В любой момент ты можешь ' 'удалить её из канала.' ) elif status == FormStatus.RETURNED: buttons[0].extend( (answer_button, show_form_button) ) buttons[1].append( InlineKeyboardButton(text='Отправить анкету', callback_data=str(SEND_FORM)) ) text = ( 'К сожалению, твоя анкета не прошла ревью админами. Ознакомься ' 'с замечаниями и попробуй ещё раз.' ) buttons[2].append( InlineKeyboardButton(text='Назад', callback_data=str(BACK)) ) keyboard = InlineKeyboardMarkup(buttons) logger.debug(f'{text}, {keyboard}') chat_data.print_messages( {'text': text}, {'text': user_data.print_status(), 'reply_markup': keyboard} ) return SELECT_ACTION