def send_random_bot(bot, update): from components.explore import send_bot_details random_bot = (Bot.select().where( (Bot.approved == True, Bot.disabled == False), (Bot.description.is_null(False)), ).order_by(fn.Random()).limit(1)[0]) send_bot_details(bot, update, random_bot)
def forward_router(bot, update, chat_data): text = update.effective_message.text # match first username in forwarded message try: username = re.match(settings.REGEX_BOT_IN_TEXT, text).groups()[0] if username == "@" + settings.SELF_BOT_NAME: return # ignore item = Bot.get(Bot.username == username) send_bot_details(bot, update, chat_data, item) except (AttributeError, TypeError, Bot.DoesNotExist): pass # no valid username in forwarded message
def ban_bot(bot, update, chat_data, to_ban: Bot, ban_state: bool): if to_ban.disabled and ban_state is True: update.message.reply_text( mdformat.none_action("{} is already banned.".format(to_ban)), parse_mode="markdown", ) return if not to_ban.disabled and ban_state is False: update.message.reply_text( mdformat.none_action("{} is not banned.".format(to_ban)), parse_mode="markdown", ) return if ban_state: to_ban.disable(Bot.DisabledReason.banned) update.message.reply_text("Bot was banned.") else: to_ban.enable() update.message.reply_text("Bot was unbanned.") to_ban.save() from components.explore import send_bot_details return send_bot_details(bot, update, chat_data, to_ban)
def search_query(bot, update, chat_data, query, send_errors=True): cid = update.effective_chat.id results = search.search_bots(query) is_admin = cid in settings.MODERATORS reply_markup = ReplyKeyboardMarkup( basic.main_menu_buttons(is_admin), resize_keyboard=True) if util.is_private_message(update) else None if results: if len(results) == 1: return send_bot_details(bot, update, chat_data, results[0]) too_many_results = len(results) > settings.MAX_SEARCH_RESULTS bots_list = '' if cid in settings.MODERATORS: # append edit buttons bots_list += '\n'.join([ "{} — /edit{} 🛃".format(b, b.id) for b in list(results)[:100] ]) else: bots_list += '\n'.join( [str(b) for b in list(results)[:settings.MAX_SEARCH_RESULTS]]) bots_list += '\n…' if too_many_results else '' bots_list = messages.SEARCH_RESULTS.format( bots=bots_list, num_results=len(results), plural='s' if len(results) > 1 else '', query=query) msg = update.message.reply_text(bots_list, parse_mode=ParseMode.MARKDOWN, reply_markup=reply_markup) else: if send_errors: update.message.reply_text(util.failure( "Sorry, I couldn't find anything related " "to *{}* in the @BotList. /search".format( util.escape_markdown(query))), parse_mode=ParseMode.MARKDOWN, reply_markup=reply_markup) return ConversationHandler.END
def new_bot_submission(bot, update, chat_data, args=None): tg_user = update.message.from_user user = User.from_telegram_object(tg_user) if util.stop_banned(update, user): return reply_to = util.original_reply_id(update) if args: text = ' '.join(args) else: text = update.message.text command_no_args = len( re.findall(r'^/new\s*$', text)) > 0 or text.lower().strip() == '/new@botlistbot' if command_no_args: update.message.reply_text(util.action_hint( "Please use this command with an argument. For example:\n/new @mybot 🔎" ), reply_to_message_id=reply_to) return # `#new` is already checked by handler try: username = re.match(settings.REGEX_BOT_IN_TEXT, text).groups()[0] if username.lower() == '@' + settings.SELF_BOT_NAME.lower(): log.info("Ignoring {}".format(text)) return except AttributeError: if args: update.message.reply_text(util.failure( "Sorry, but you didn't send me a bot `@username`."), quote=True, parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply_to) log.info("Ignoring {}".format(text)) # no bot username, ignore update return try: new_bot = Bot.by_username(username) if new_bot.approved: update.message.reply_text(util.action_hint( "Sorry fool, but {} is already in the @BotList 😉".format( new_bot.username)), reply_to_message_id=reply_to) else: update.message.reply_text(util.action_hint( "{} has already been submitted. Please have patience...". format(new_bot.username)), reply_to_message_id=reply_to) return except Bot.DoesNotExist: new_bot = Bot(revision=Revision.get_instance().next, approved=False, username=username, submitted_by=user) new_bot.inlinequeries = "🔎" in text new_bot.official = "🔹" in text # find language languages = Country.select().execute() for lang in languages: if lang.emoji in text: new_bot.country = lang new_bot.date_added = datetime.date.today() description_reg = re.match(settings.REGEX_BOT_IN_TEXT + ' -\s?(.*)', text) description_notify = '' if description_reg: description = description_reg.group(2) new_bot.description = description description_notify = ' Your description was included.' new_bot.save() if util.is_private_message(update) and util.uid_from_update( update) in settings.MODERATORS: from components.explore import send_bot_details send_bot_details(bot, update, chat_data, new_bot) else: msg = update.message.reply_text(util.success( "You submitted {} for approval.{}".format(new_bot, description_notify)), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply_to) return ConversationHandler.END
def callback_router(bot, update, chat_data, user_data, job_queue): obj = json.loads(str(update.callback_query.data)) user = User.from_update(update) try: if "a" in obj: action = obj["a"] # BOTLISTCHAT if action == CallbackActions.DELETE_CONVERSATION: botlistchat.delete_conversation(bot, update, chat_data) # HELP elif action == CallbackActions.HELP: help.help(bot, update) elif action == CallbackActions.CONTRIBUTING: help.contributing(bot, update) elif action == CallbackActions.EXAMPLES: help.examples(bot, update) # BASIC QUERYING elif action == CallbackActions.SELECT_CATEGORY: select_category(bot, update, chat_data) elif action == CallbackActions.SELECT_BOT_FROM_CATEGORY: category = Category.get(id=obj["id"]) send_category(bot, update, chat_data, category) elif action == CallbackActions.SEND_BOT_DETAILS: item = Bot.get(id=obj["id"]) send_bot_details(bot, update, chat_data, item) # FAVORITES elif action == CallbackActions.TOGGLE_FAVORITES_LAYOUT: value = obj["v"] favorites.toggle_favorites_layout(bot, update, value) elif action == CallbackActions.ADD_FAVORITE: favorites.add_favorite_handler(bot, update) elif action == CallbackActions.REMOVE_FAVORITE_MENU: favorites.remove_favorite_menu(bot, update) elif action == CallbackActions.REMOVE_FAVORITE: to_remove = Favorite.get(id=obj["id"]) bot_details = to_remove.bot to_remove.delete_instance() if obj.get("details"): send_bot_details(bot, update, chat_data, bot_details) else: favorites.remove_favorite_menu(bot, update) elif action == CallbackActions.SEND_FAVORITES_LIST: favorites.send_favorites_list(bot, update) elif action == CallbackActions.ADD_ANYWAY: favorites.add_custom(bot, update, obj["u"]) elif action == CallbackActions.ADD_TO_FAVORITES: details = obj.get("details") discreet = obj.get("discreet", False) or details item = Bot.get(id=obj["id"]) favorites.add_favorite(bot, update, item, callback_alert=discreet) if details: send_bot_details(bot, update, chat_data, item) # ACCEPT/REJECT BOT SUBMISSIONS elif action == CallbackActions.APPROVE_REJECT_BOTS: custom_approve_list = [Bot.get(id=obj["id"])] admin.approve_bots(bot, update, override_list=custom_approve_list) elif action == CallbackActions.ACCEPT_BOT: to_accept = Bot.get(id=obj["id"]) admin.edit_bot_category(bot, update, to_accept, CallbackActions.BOT_ACCEPTED) # Run in x minutes, giving the moderator enough time to edit bot details job_queue.run_once( lambda b, job: botlistchat. notify_group_submission_accepted(b, job, to_accept), settings.BOT_ACCEPTED_IDLE_TIME * 60, ) elif action == CallbackActions.RECOMMEND_MODERATOR: bot_in_question = Bot.get(id=obj["id"]) admin.recommend_moderator(bot, update, bot_in_question, obj["page"]) elif action == CallbackActions.SELECT_MODERATOR: bot_in_question = Bot.get(id=obj["bot_id"]) moderator = User.get(id=obj["uid"]) admin.share_with_moderator(bot, update, bot_in_question, moderator) admin.approve_bots(bot, update, obj["page"]) elif action == CallbackActions.REJECT_BOT: to_reject = Bot.get(id=obj["id"]) notification = obj.get("ntfc", True) admin.reject_bot_submission( bot, update, None, to_reject, verbose=False, notify_submittant=notification, ) admin.approve_bots(bot, update, obj["page"]) elif action == CallbackActions.BOT_ACCEPTED: to_accept = Bot.get(id=obj["bid"]) category = Category.get(id=obj["cid"]) admin.accept_bot_submission(bot, update, to_accept, category) elif action == CallbackActions.COUNT_THANK_YOU: new_count = obj.get("count", 1) basic.count_thank_you(bot, update, new_count) # ADD BOT # elif action == CallbackActions.ADD_BOT_SELECT_CAT: # category = Category.get(id=obj['id']) # admin.add_bot(bot, update, chat_data, category) # EDIT BOT elif action == CallbackActions.EDIT_BOT: to_edit = Bot.get(id=obj["id"]) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_SELECT_CAT: to_edit = Bot.get(id=obj["id"]) admin.edit_bot_category(bot, update, to_edit) elif action == CallbackActions.EDIT_BOT_CAT_SELECTED: to_edit = Bot.get(id=obj["bid"]) cat = Category.get(id=obj["cid"]) botproperties.change_category(bot, update, to_edit, cat) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_COUNTRY: to_edit = Bot.get(id=obj["id"]) botproperties.set_country_menu(bot, update, to_edit) elif action == CallbackActions.SET_COUNTRY: to_edit = Bot.get(id=obj["bid"]) if obj["cid"] == "None": country = None else: country = Country.get(id=obj["cid"]) botproperties.set_country(bot, update, to_edit, country) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_DESCRIPTION: to_edit = Bot.get(id=obj["id"]) botproperties.set_text_property(bot, update, chat_data, "description", to_edit) elif action == CallbackActions.EDIT_BOT_EXTRA: to_edit = Bot.get(id=obj["id"]) # SAME IS DONE HERE, but manually botproperties.set_text_property(bot, update, chat_data, "extra", to_edit) elif action == CallbackActions.EDIT_BOT_NAME: to_edit = Bot.get(id=obj["id"]) botproperties.set_text_property(bot, update, chat_data, "name", to_edit) elif action == CallbackActions.EDIT_BOT_USERNAME: to_edit = Bot.get(id=obj["id"]) botproperties.set_text_property(bot, update, chat_data, "username", to_edit) # elif action == CallbackActions.EDIT_BOT_KEYWORDS: # to_edit = Bot.get(id=obj['id']) # botproperties.set_keywords_init(bot, update, chat_data, to_edit) elif action == CallbackActions.APPLY_ALL_CHANGES: to_edit = Bot.get(id=obj["id"]) admin.apply_all_changes(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_INLINEQUERIES: to_edit = Bot.get(id=obj["id"]) value = bool(obj["value"]) botproperties.toggle_value(bot, update, "inlinequeries", to_edit, value) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_OFFICIAL: to_edit = Bot.get(id=obj["id"]) value = bool(obj["value"]) botproperties.toggle_value(bot, update, "official", to_edit, value) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_OFFLINE: to_edit = Bot.get(id=obj["id"]) value = bool(obj["value"]) botproperties.toggle_value(bot, update, "offline", to_edit, value) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_SPAM: to_edit = Bot.get(id=obj["id"]) value = bool(obj["value"]) botproperties.toggle_value(bot, update, "spam", to_edit, value) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.CONFIRM_DELETE_BOT: to_delete = Bot.get(id=obj["id"]) botproperties.delete_bot_confirm(bot, update, to_delete) elif action == CallbackActions.DELETE_BOT: to_edit = Bot.get(id=obj["id"]) botproperties.delete_bot(bot, update, to_edit) # send_category(bot, update, chat_data, to_edit.category) elif action == CallbackActions.ACCEPT_SUGGESTION: suggestion = Suggestion.get(id=obj["id"]) components.botproperties.accept_suggestion( bot, update, suggestion) admin.approve_suggestions(bot, update, page=obj["page"]) elif action == CallbackActions.REJECT_SUGGESTION: suggestion = Suggestion.get(id=obj["id"]) suggestion.delete_instance() admin.approve_suggestions(bot, update, page=obj["page"]) elif action == CallbackActions.CHANGE_SUGGESTION: suggestion = Suggestion.get(id=obj["id"]) botproperties.change_suggestion(bot, update, suggestion, page_handover=obj["page"]) elif action == CallbackActions.SWITCH_SUGGESTIONS_PAGE: page = obj["page"] admin.approve_suggestions(bot, update, page) elif action == CallbackActions.SWITCH_APPROVALS_PAGE: admin.approve_bots(bot, update, page=obj["page"]) elif action == CallbackActions.SET_NOTIFICATIONS: set_notifications(bot, update, obj["value"]) elif action == CallbackActions.NEW_BOTS_SELECTED: show_new_bots(bot, update, chat_data, back_button=True) elif action == CallbackActions.ABORT_SETTING_KEYWORDS: to_edit = Bot.get(id=obj["id"]) admin.edit_bot(bot, update, chat_data, to_edit) # SENDING BOTLIST elif action == CallbackActions.SEND_BOTLIST: silent = obj.get("silent", False) re_send = obj.get("re", False) botlist.send_botlist(bot, update, resend=re_send, silent=silent) elif action == CallbackActions.RESEND_BOTLIST: botlist.send_botlist(bot, update, resend=True) # BROADCASTING elif action == "send_broadcast": broadcasts.send_broadcast(bot, update, user_data) elif action == "pin_message": broadcasts.pin_message(bot, update, obj["mid"]) elif action == "add_thank_you": basic.add_thank_you_button(bot, update, obj["cid"], obj["mid"]) # EXPLORING elif action == CallbackActions.EXPLORE_NEXT: explore.explore(bot, update, chat_data) except Exception as e: traceback.print_exc() # get the callback action in plaintext actions = dict(CallbackActions.__dict__) a = next(k for k, v in actions.items() if v == obj.get("a")) util.send_md_message( bot, settings.DEVELOPER_ID, "Exception in callback query for {}:\n{}\n\nWith CallbackAction {}\n\nWith data:\n{}" .format( user.markdown_short, util.escape_markdown(e), util.escape_markdown(a), util.escape_markdown(str(obj)), ), ) finally: bot.answerCallbackQuery(update.callback_query.id) return ConversationHandler.END
def search_query(bot, update: Update, chat_data, query, send_errors=True): cid: int = update.effective_chat.id user: User = User.from_update(update) is_admin: bool = cid in settings.MODERATORS replied_to_message_id: Optional[int] = util.original_reply_id(update) results = search.search_bots(query) reply_markup = ( ReplyKeyboardMarkup(basic.main_menu_buttons(is_admin), resize_keyboard=True) if util.is_private_message(update) else None ) if results: if len(results) == 1: update.effective_message.delete() header = f"{user.markdown_short} found the following bot for you:" return send_bot_details(bot, update, chat_data, results[0], header_msg=header) too_many_results = len(results) > settings.MAX_SEARCH_RESULTS bots_list = "" if cid in settings.MODERATORS: # private chat with moderator # append edit buttons bots_list += "\n".join( ["{} — /edit{} 🛃".format(b, b.id) for b in list(results)[:100]] ) else: bots_list += "\n".join( [str(b) for b in list(results)[: settings.MAX_SEARCH_RESULTS]] ) bots_list += "\n…" if too_many_results else "" bots_list = messages.SEARCH_RESULTS.format( bots=bots_list, num_results=len(results), plural="s" if len(results) > 1 else "", query=query, ) if util.is_group_message(update) and not update.message.reply_to_message: try: bot.formatter.send_message( update.effective_user.id, bots_list, reply_markup=reply_markup, disable_web_page_preview=True, ) reply_markup, callback = botlistchat.append_restricted_delete_button( update, chat_data, InlineKeyboardMarkup([[]]) ) msg = bot.formatter.send_message( update.effective_chat.id, f"Hey {user.plaintext}, let's not annoy the others. I sent you the search results " f"[in private chat](https://t.me/{settings.SELF_BOT_NAME}).", disable_web_page_preview=True, reply_markup=reply_markup, ) callback(msg) update.effective_message.delete() except TelegramError: hint_msg, hint_reply_markup, _ = get_hint_data("#private") bot.formatter.send_message( update.effective_chat.id, hint_msg, reply_markup=hint_reply_markup, reply_to_message_id=update.effective_message.id, disable_web_page_preview=True, ) return ConversationHandler.END if replied_to_message_id: bots_list = f"{user.markdown_short} suggests to search and {bots_list}" bot.formatter.send_message( update.effective_chat.id, bots_list, parse_mode=ParseMode.MARKDOWN, reply_markup=reply_markup, disable_web_page_preview=True, reply_to_message_id=replied_to_message_id, ) update.effective_message.delete() else: if send_errors: callback = None if util.is_group_message(update): reply_markup, callback = botlistchat.append_restricted_delete_button( update, chat_data, InlineKeyboardMarkup([[]]) ) msg = update.message.reply_text( util.failure( "Sorry, I couldn't find anything related " "to *{}* in the @BotList. /search".format( util.escape_markdown(query) ) ), parse_mode=ParseMode.MARKDOWN, reply_markup=reply_markup, ) if callback: callback(msg) return ConversationHandler.END