async def bot_about(_, query): cb_wrapper = MethodWrapper(query) await cb_wrapper.edit_message_text( text=CREDITS.format(VERSION=VERSION), disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(BACK_BUTTON, "back_start")]])) await cb_wrapper.answer()
async def bot_disclaimer_handler(_, query): cb_wrapper = MethodWrapper(query) await cb_wrapper.edit_message_text( text=plate("bot_disclaimer_text"), disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(plate("back_button"), "back_start")]] ), ) await cb_wrapper.answer()
async def join_chat(_, query): cb_wrapper = MethodWrapper(query) if CACHE[query.from_user.id][0] != "IN_CHAT": user_id = int(query.data.split("_")[1]) user = await wrapper.get_users(user_id) if isinstance(user, Exception): user_name = "Anonymous" elif user.first_name: user_name = user.first_name elif user.username: user_name = user.username else: user_name = "Anonymous" if CACHE[user_id][0] != "AWAITING_ADMIN": await cb_wrapper.answer(CHAT_BUSY) else: buttons = InlineKeyboardMarkup([[ InlineKeyboardButton(CLOSE_CHAT_BUTTON, f"close_chat_{user_id}") ]]) admin_id, admin_name = query.from_user.id, ADMINS[ query.from_user.id] CACHE[user_id] = ["IN_CHAT", admin_id, CACHE[user_id][-1]] CACHE[query.from_user.id] = ["IN_CHAT", user_id] message = await wrapper.send_message(query.from_user.id, USER_JOINS_CHAT, reply_markup=buttons) admin_joins = await wrapper.send_message( user_id, ADMIN_JOINS_CHAT.format(admin_name=admin_name, admin_id=NAME.format(admin_id)), reply_markup=buttons) logging.warning( f"{admin_name} [{admin_id}] has joined a chat with user {user_name} [{user_id}]" ) for chatid, message_ids in CACHE[user_id][-1]: await wrapper.delete_messages(chatid, message_ids) for admin in ADMINS: if CACHE[admin] != "IN_CHAT" and admin != admin_id: await wrapper.send_message( admin, ADMIN_ACCEPTED_CHAT.format( admin=f"[{admin_name}]({NAME.format(admin_id)})", user=f"[{user_name}]({NAME.format(user_id)})")) CACHE[user_id][-1].append((message.chat.id, message.message_id)) CACHE[user_id][-1].append( (admin_joins.chat.id, admin_joins.message_id)) else: await cb_wrapper.answer(LEAVE_CURRENT_CHAT)
async def bot_about_handler(_, query): cb_wrapper = MethodWrapper(query) await cb_wrapper.edit_message_text( text=plate( "credits", python_version=PY_VERSION, botbase_version=BOTBASE_VERSION, bot_version=BOT_VERSION, ), disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(plate("back_button"), "back_start")]] ), ) await cb_wrapper.answer()
async def start_handler(_, update): """Simply handles the /start command sending a pre-defined greeting and saving new users to the database""" update_wrapper = MethodWrapper(update) if update.from_user.first_name: name = update.from_user.first_name elif update.from_user.username: name = update.from_user.username else: name = "Anonymous" if update.from_user.id not in itertools.chain(*get_users()): logging.warning( f"New user detected ({update.from_user.id}), adding to database") set_user( update.from_user.id, update.from_user.username.lower() if update.from_user.username else None) if GREET: if isinstance(update, Message): await update_wrapper.reply(text=GREET.format( mention=f"[{name}](tg://user?id={update.from_user.id})", id=update.from_user.id, username=update.from_user.username), reply_markup=BUTTONS) elif isinstance(update, CallbackQuery): if CACHE[update.from_user.id][0] == "AWAITING_ADMIN": data = CACHE[update.from_user.id][-1] if isinstance(data, list): for chatid, message_ids in data: await wrapper.delete_messages(chatid, message_ids) for admin in ADMINS: await wrapper.send_message( chat_id=admin, text=USER_LEFT_QUEUE.format( user=f"[{name}]({NAME.format(update.from_user.id)})" )) await update_wrapper.edit_message_text(text=GREET.format( mention=f"[{name}](tg://user?id={update.from_user.id})", id=update.from_user.id, username=update.from_user.username), reply_markup=BUTTONS) del CACHE[update.from_user.id] await update_wrapper.answer()
async def update_admins_list(_, query): cb_wrapper = MethodWrapper(query) if CACHE[query.from_user.id][0] == "AWAITING_ADMIN": if time.time() - CACHE[ query.from_user.id][1] >= ADMINS_LIST_UPDATE_DELAY: CACHE[query.from_user.id][1] = time.time() queue = LIVE_CHAT_STATUSES for admin_id, admin_name in ADMINS.items(): status = CACHE[admin_id][0] if status != "IN_CHAT": queue += f"- {STATUS_FREE}" else: queue += f"- {STATUS_BUSY}" queue += f"[{admin_name}]({NAME.format(admin_id)})\n" await cb_wrapper.edit_message_text(SUPPORT_REQUEST_SENT.format( queue=queue, date=time.strftime('%d/%m/%Y %T')), reply_markup=BUTTONS) join_chat_button = InlineKeyboardMarkup([[ InlineKeyboardButton(JOIN_CHAT_BUTTON, f"join_{query.from_user.id}") ]]) user = get_user(query.from_user.id) tg_id, tg_uname, date, banned = user text = USER_INFO.format(tg_id=tg_id, tg_uname='@' + tg_uname if tg_uname else 'N/A', date=date, status=YES if banned else NO, admin=YES if tg_id in ADMINS else NO) for admin in ADMINS: status = CACHE[admin][0] if status != "IN_CHAT": if status != "NOTIFICATION_SENT" and CACHE[admin][ 1] != tg_id: message = await wrapper.send_message( admin, SUPPORT_NOTIFICATION.format(uinfo=text), reply_markup=join_chat_button) CACHE[query.from_user.id][-1].append( (message.chat.id, message.message_id)) else: await cb_wrapper.answer(TOO_FAST, show_alert=True) else: await cb_start_handler(_, query)
async def services_status_history_handler(_, query): cb_wrapper = MethodWrapper(query) await cb_wrapper.edit_message_text( text=plate( "services_status_history_text", website_30d_avg=await get_30d_website_history_ratio(), website_30d_avg_symbol="✅" if await get_30d_website_history_label() else "⚠️", qr_code_api_30d_avg=await get_30d_qr_api_history_ratio(), qr_code_api_30d_avg_symbol="✅" if await get_30d_qr_api_history_ratio() else "⚠️", api_30d_avg=await get_30d_api_history_ratio(), api_30d_avg_symbol="✅" if await get_30d_api_history_label() else "⚠️", docs_30d_avg=await get_30d_api_docs_history_ratio(), docs_30d_avg_symbol="✅" if await get_30d_api_docs_history_label() else "⚠️", internal_api_30d_avg=await get_30d_internal_api_history_ratio(), internal_api_30d_avg_symbol="✅" if await get_30d_internal_api_history_label() else "⚠️", website_90d_avg=await get_90d_website_history_ratio(), website_90d_avg_symbol="✅" if await get_90d_website_history_label() else "⚠️", qr_code_api_90d_avg=await get_90d_qr_api_history_ratio(), qr_code_api_90d_avg_symbol="✅" if await get_90d_qr_api_history_ratio() else "⚠️", api_90d_avg=await get_90d_api_history_ratio(), api_90d_avg_symbol="✅" if await get_90d_api_history_label() else "⚠️", docs_90d_avg=await get_90d_api_docs_history_ratio(), docs_90d_avg_symbol="✅" if await get_90d_api_docs_history_label() else "⚠️", internal_api_90d_avg=await get_90d_internal_api_history_ratio(), internal_api_90d_avg_symbol="✅" if await get_90d_internal_api_history_label() else "⚠️", ), disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(plate("back_button"), "back_start")]] ), ) await cb_wrapper.answer()
async def begin_chat(_, query): cb_wrapper = MethodWrapper(query) if query.from_user.id in ADMINS: await cb_wrapper.answer(CANNOT_REQUEST_SUPPORT) else: CACHE[query.from_user.id] = ["AWAITING_ADMIN", time.time()] queue = LIVE_CHAT_STATUSES for admin_id, admin_name in ADMINS.items(): status = CACHE[admin_id][0] if status != "IN_CHAT": queue += f"- {STATUS_FREE}" else: queue += f"- {STATUS_BUSY}" queue += f"[{admin_name}]({NAME.format(admin_id)})\n" msg = await cb_wrapper.edit_message_text(SUPPORT_REQUEST_SENT.format( queue=queue, date=time.strftime('%d/%m/%Y %T')), reply_markup=BUTTONS) join_chat_button = InlineKeyboardMarkup([[ InlineKeyboardButton(JOIN_CHAT_BUTTON, f"join_{query.from_user.id}") ]]) user = get_user(query.from_user.id) tg_id, tg_uname, date, banned = user text = USER_INFO.format(tg_id=tg_id, tg_uname='@' + tg_uname if tg_uname else 'N/A', date=date, status=YES if banned else NO, admin=YES if tg_id in ADMINS else NO) CACHE[query.from_user.id].append([]) for admin in ADMINS: status = CACHE[admin][0] if status != "IN_CHAT": message = await wrapper.send_message( admin, SUPPORT_NOTIFICATION.format(uinfo=text), reply_markup=join_chat_button) CACHE[query.from_user.id][-1].append( (message.chat.id, message.message_id)) CACHE[admin] = ["NOTIFICATION_SENT", query.from_user.id] CACHE[query.from_user.id][-1].append((msg.chat.id, msg.message_id))
from BotBase.config import CACHE, ADMINS, ADMINS_LIST_UPDATE_DELAY, LIVE_CHAT_STATUSES, STATUS_BUSY, STATUS_FREE, \ USER_INFO, ADMIN_MESSAGE, USER_MESSAGE, NAME, bot from BotBase.strings.default_strings import SUPPORT_REQUEST_SENT, SUPPORT_NOTIFICATION, ADMIN_JOINS_CHAT, \ USER_CLOSES_CHAT, JOIN_CHAT_BUTTON, USER_LEAVES_CHAT, TOO_FAST, CHAT_BUSY, LEAVE_CURRENT_CHAT, USER_JOINS_CHAT, \ CANNOT_REQUEST_SUPPORT, YES, NO, CLOSE_CHAT_BUTTON, BACK_BUTTON, UPDATE_BUTTON, ADMIN_ACCEPTED_CHAT from BotBase.methods.custom_filters import callback_regex, admin_is_chatting, user_is_chatting, user_banned from BotBase.database.query import get_user from BotBase.methods import MethodWrapper from BotBase.modules.antiflood import BANNED_USERS from BotBase.modules.start import cb_start_handler ADMINS_FILTER = filters.user(list(ADMINS.keys())) BUTTONS = InlineKeyboardMarkup( [[InlineKeyboardButton(BACK_BUTTON, "back_start")], [InlineKeyboardButton(UPDATE_BUTTON, "update_admins_list")]]) wrapper = MethodWrapper(bot) @Client.on_callback_query( filters.regex("begin_chat") & ~BANNED_USERS & ~user_banned()) async def begin_chat(_, query): cb_wrapper = MethodWrapper(query) if query.from_user.id in ADMINS: await cb_wrapper.answer(CANNOT_REQUEST_SUPPORT) else: CACHE[query.from_user.id] = ["AWAITING_ADMIN", time.time()] queue = LIVE_CHAT_STATUSES for admin_id, admin_name in ADMINS.items(): status = CACHE[admin_id][0] if status != "IN_CHAT": queue += f"- {STATUS_FREE}"
async def start_handler(_, update): """Simply handles the /start command sending a pre-defined greeting and saving new users to the database""" update_wrapper = MethodWrapper(update) if update.from_user.first_name: name = update.from_user.first_name elif update.from_user.username: name = update.from_user.username else: name = "Anonymous" if update.from_user.id not in itertools.chain(*get_users()): logging.warning( f"New user detected ({update.from_user.id}), adding to database" ) set_user( update.from_user.id, update.from_user.username.lower() if update.from_user.username else None, ) if GREET: if isinstance(update, Message): await update_wrapper.reply( text=plate( "greet", mention=f"[{name}](tg://user?id={update.from_user.id})", id=update.from_user.id, username=update.from_user.username, website_status="Online" if await get_website_status() else "Offline", qr_code_api_status="Online" if await get_qr_api_status() else "Offline", api_status="Online" if await get_api_status() else "Offline", api_docs_status="Online" if await get_api_docs_status() else "Offline", internal_api_status="Online" if await get_internal_api_status() else "Offline", ), reply_markup=BUTTONS, ) elif isinstance(update, CallbackQuery): if CACHE[update.from_user.id][0] == "AWAITING_ADMIN": data = CACHE[update.from_user.id][-1] if isinstance(data, list): for chatid, message_ids in data: await wrapper.delete_messages(chatid, message_ids) for admin in ADMINS: await wrapper.send_message( chat_id=admin, text=plate( "user_left_queue", user=f"[{name}]({NAME.format(update.from_user.id)})", ), ), await update_wrapper.edit_message_text( text=plate( "greet", mention=f"[{name}](tg://user?id={update.from_user.id})", id=update.from_user.id, username=update.from_user.username, website_status="Online" if await get_website_status() else "Offline", qr_code_api_status="Online" if await get_qr_api_status() else "Offline", api_status="Online" if await get_api_status() else "Offline", api_docs_status="Online" if await get_api_docs_status() else "Offline", internal_api_status="Online" if await get_internal_api_status() else "Offline", ), reply_markup=BUTTONS, ) del CACHE[update.from_user.id] await update_wrapper.answer()