async def _query_command(bot, update, user_record): query = get_cleaned_text(update, bot, [ 'query', ]) query_id = None if len(query) == 0: return bot.get_message('admin', 'query_command', 'help', update=update, user_record=user_record) try: with bot.db as db: record = db.query(query) try: record = list(record) except ResourceClosedError: record = bot.get_message('admin', 'query_command', 'no_iterable', update=update, user_record=user_record) query_id = db['queries'].upsert(dict(query=query), ['query']) if query_id is True: query_id = db['queries'].find_one(query=query)['id'] result = json.dumps(record, indent=2) if len(result) > 500: result = ( f"{result[:200]}\n" # First 200 characters f"[...]\n" # Interruption symbol f"{result[-200:]}" # Last 200 characters ) except Exception as e: result = "{first_line}\n{e}".format(first_line=bot.get_message( 'admin', 'query_command', 'exception', update=update, user_record=user_record), e=e) result = ("<b>{first_line}</b>\n".format( first_line=bot.get_message('admin', 'query_command', 'result', update=update, user_record=user_record)) + f"<code>{query}</code>\n\n" f"{result}") if query_id: reply_markup = make_inline_keyboard([ make_button( text='CSV', prefix='db_query:///', data=['csv', query_id]) ], 1) else: reply_markup = None return dict(chat_id=update['chat']['id'], text=result, parse_mode='HTML', reply_markup=reply_markup)
async def _start_command(bot, update, user_record): text = get_cleaned_text(update=update, bot=bot, replace=['start']) if not text: return await _help_command(bot, update, user_record) update['text'] = text await bot.text_message_handler( update=update, user_record=None ) return
async def _talk_command(bot, update, user_record): text = get_cleaned_text(update, bot, ['talk']) text, reply_markup = get_talk_panel(bot=bot, update=update, user_record=user_record, text=text) return dict( text=text, parse_mode='HTML', reply_markup=reply_markup, )
async def _talk_command(update, bot): text = get_cleaned_text( update, bot, ['talk'] ) text, reply_markup = get_talk_panel(update, bot, text) return dict( text=text, parse_mode='HTML', reply_markup=reply_markup, )
def criterion(update): if 'message' not in update: return False update = update['message'] text = get_cleaned_text(update, bot, []) if ('from' not in update or 'id' not in update['from']): return False with bot.db as db: user_record = db['users'].find_one( telegram_id=update['from']['id']) if not bot.authorization_function( update=update, user_record=user_record, authorization_level=2): return False return text == allowed_command.strip('/')
async def _log_command(bot, update, user_record): if bot.log_file_path is None: return bot.get_message('admin', 'log_command', 'no_log', update=update, user_record=user_record) # Always send log file in private chat chat_id = update['from']['id'] text = get_cleaned_text(update, bot, ['log']) reversed_ = 'r' not in text text = text.strip('r') if text.isnumeric(): limit = int(text) else: limit = 100 if limit is None: sent = await bot.send_document(chat_id=chat_id, document_path=bot.log_file_path, caption=bot.get_message( 'admin', 'log_command', 'here_is_log_file', update=update, user_record=user_record)) else: sent = await send_part_of_text_file( bot=bot, update=update, user_record=user_record, chat_id=chat_id, file_path=bot.log_file_path, file_name=bot.log_file_name, caption=bot.get_message('admin', 'log_command', ('log_file_last_lines' if reversed_ else 'log_file_first_lines'), update=update, user_record=user_record, lines=limit), reversed_=reversed_, limit=limit) if isinstance(sent, Exception): return bot.get_message('admin', 'log_command', 'sending_failure', update=update, user_record=user_record, e=sent) return
async def _maintenance_command(bot, update, user_record): maintenance_status = bot.change_maintenance_status( maintenance_message=get_cleaned_text(update, bot, ['maintenance'])) if maintenance_status: return bot.get_message('admin', 'maintenance_command', 'maintenance_started', update=update, user_record=user_record, message=bot.maintenance_message) return bot.get_message('admin', 'maintenance_command', 'maintenance_ended', update=update, user_record=user_record)
async def bar_parser(bot, update): text_except_foo = get_cleaned_text(update, bot, ['bar']) return f"Foo!\n{text_except_foo}"