From f762661878f6e00fbd8c1f00b7811bff686f6658 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 6 May 2022 20:40:27 +0200 Subject: [PATCH 1/4] Search-only branch for v13.x --- components/callbacks.py | 41 ++--------- components/const.py | 8 +- components/taghints.py | 6 +- rules_bot.py | 159 +++++++++++++++++----------------------- 4 files changed, 79 insertions(+), 135 deletions(-) diff --git a/components/callbacks.py b/components/callbacks.py index 794cf88..3cd1eb9 100644 --- a/components/callbacks.py +++ b/components/callbacks.py @@ -1,9 +1,9 @@ import datetime as dtm import html import logging +import random import time from collections import deque -import random from copy import deepcopy from typing import cast, Match, List, Dict, Any, Optional, Tuple @@ -34,7 +34,6 @@ ONTOPIC_RULES, OFFTOPIC_RULES, OFFTOPIC_CHAT_ID, - GITHUB_PATTERN, ONTOPIC_RULES_MESSAGE_LINK, OFFTOPIC_RULES_MESSAGE_LINK, NEW_CHAT_MEMBERS_LIMIT_SPACING, @@ -52,7 +51,6 @@ reply_or_edit, try_to_delete, ) -from components.github import github_issues def start(update: Update, context: CallbackContext) -> None: @@ -85,10 +83,10 @@ def inlinequery_help(update: Update, context: CallbackContext) -> None: f"but you need an {char}InlineQueryHandler{char} for it.\n\n" f"*becomes:*\n" f"I 💙 [InlineQueries](" - "https://python-telegram-bot.readthedocs.io/en/latest/telegram.html#telegram" + "https://python-telegram-bot.readthedocs.io/en/stable/telegram.html#telegram" f".InlineQuery), but you need an [InlineQueryHandler](" f"https://python-telegram-bot.readthedocs.io/en" - f"/latest/telegram.ext.html#telegram.ext.InlineQueryHandler) for it.\n\n" + f"/stable/telegram.ext.html#telegram.ext.InlineQueryHandler) for it.\n\n" f"Some wiki pages have spaces in them. Please replace such spaces with underscores. " f"The bot will automatically change them back desired space." ) @@ -130,7 +128,7 @@ def docs(update: Update, _: CallbackContext) -> None: message = cast(Message, update.effective_message) text = ( "You can find our documentation at " - "[Read the Docs](https://python-telegram-bot.readthedocs.io/)" + "[Read the Docs](https://python-telegram-bot.readthedocs.io/en/stable)" ) reply_id = message.reply_to_message.message_id if message.reply_to_message else None message.reply_text( @@ -148,7 +146,7 @@ def wiki(update: Update, _: CallbackContext) -> None: message = cast(Message, update.effective_message) text = ( "You can find our wiki on " - "[GitHub](https://github.com/python-telegram-bot/python-telegram-bot/wiki)" + "[GitHub](https://github.com/python-telegram-bot/v13.x-wiki/wiki)" ) message.reply_text( text, @@ -268,39 +266,12 @@ def keep_typing(last: float, chat: Chat, action: str) -> float: def reply_search(update: Update, context: CallbackContext) -> None: message = cast(Message, update.effective_message) last = 0.0 - thing_matches: List[Tuple[int, Tuple[str, str, str, str, str]]] = [] things: List[Tuple[int, BaseEntry]] = [] no_entity_text = get_text_not_in_entities(message).strip() - # Parse exact matches for GitHub threads & ptbcontrib things first - for match in GITHUB_PATTERN.finditer(no_entity_text): - logging.debug(match.groupdict()) - owner, repo, number, sha, ptbcontrib = [ - cast(str, match.groupdict()[x]) - for x in ("owner", "repo", "number", "sha", "ptbcontrib") - ] - if number or sha or ptbcontrib: - thing_matches.append((match.start(), (owner, repo, number, sha, ptbcontrib))) - - for thing_match in thing_matches: - last = keep_typing(last, cast(Chat, update.effective_chat), ChatAction.TYPING) - owner, repo, number, sha, ptbcontrib = thing_match[1] - if number: - issue = github_issues.get_issue(int(number), owner, repo) - if issue is not None: - things.append((thing_match[0], issue)) - elif sha: - commit = github_issues.get_commit(sha, owner, repo) - if commit is not None: - things.append((thing_match[0], commit)) - elif ptbcontrib: - contrib = github_issues.ptbcontribs.get(ptbcontrib) - if contrib: - things.append((thing_match[0], contrib)) - # Parse fuzzy search next - if no_entity_text.startswith("!search") or no_entity_text.endswith("!search"): + if no_entity_text.endswith("!search13") or no_entity_text.endswith("!searchv13"): for match in ENCLOSED_REGEX.finditer(no_entity_text): last = keep_typing(last, cast(Chat, update.effective_chat), ChatAction.TYPING) things.append((match.start(), search.search(match.group(0), amount=1)[0])) diff --git a/components/const.py b/components/const.py index a6097d6..27d9550 100644 --- a/components/const.py +++ b/components/const.py @@ -28,10 +28,10 @@ OFFTOPIC_RULES_MESSAGE_ID = 161133 OFFTOPIC_RULES_MESSAGE_LINK = "https://t.me/pythontelegrambottalk/161133" PTBCONTRIB_LINK = "https://github.com/python-telegram-bot/ptbcontrib/" -DOCS_URL = "https://python-telegram-bot.readthedocs.io/" +DOCS_URL = "https://python-telegram-bot.readthedocs.io/en/stable/" OFFICIAL_URL = "https://core.telegram.org/bots/api" PROJECT_URL = urljoin(GITHUB_URL, DEFAULT_REPO + "/") -WIKI_URL = urljoin(PROJECT_URL, "wiki/") +WIKI_URL = "https://github.com/python-telegram-bot/v13.x-wiki/wiki/" WIKI_CODE_SNIPPETS_URL = urljoin(WIKI_URL, "Code-snippets") WIKI_FAQ_URL = urljoin(WIKI_URL, "Frequently-Asked-Questions") WIKI_FRDP_URL = urljoin(WIKI_URL, "Frequently-requested-design-patterns") @@ -58,10 +58,10 @@ - If you have a userbot, deactivate it in here. Otherwise you'll get banned at least temporarily. Before asking, please take a look at our wiki and wiki and example bots or, depending on your question, the \ official API docs and python-telegram-bot docs). +python-telegram-bot.readthedocs.io/en/stable/">python-telegram-bot docs). For off-topic discussions, please use our \ off-topic group. """ diff --git a/components/taghints.py b/components/taghints.py index ca8fe2f..4dc137b 100644 --- a/components/taghints.py +++ b/components/taghints.py @@ -17,7 +17,7 @@ "askright": { "message": ( '{query} Please read this short article and try again ;)' + 'v13.x-wiki/wiki/Ask-Right">this short article and try again ;)' ), "help": "The wiki page about asking technical questions", "default": ( @@ -27,8 +27,8 @@ }, "mwe": { "message": ( - '{query} Have a look at this short article for information on what a MWE is.' + '{query} Have a look at this short article for information on what a MWE is.' ), "help": "How to build an MWE for PTB.", "default": "Hey. Please provide a minimal working example (MWE).", diff --git a/rules_bot.py b/rules_bot.py index 98c2f08..a7e1984 100644 --- a/rules_bot.py +++ b/rules_bot.py @@ -6,56 +6,29 @@ ParseMode, Bot, Update, - BotCommandScopeAllPrivateChats, - BotCommandScopeChat, - BotCommandScopeAllGroupChats, - BotCommandScopeChatAdministrators, ) from telegram.error import BadRequest, Unauthorized from telegram.ext import ( - CommandHandler, Updater, MessageHandler, Filters, Defaults, - ChatMemberHandler, InlineQueryHandler, - CallbackQueryHandler, ) from components import inlinequeries from components.callbacks import ( - start, - rules, - docs, - wiki, - help_callback, - off_on_topic, - sandwich, reply_search, - delete_new_chat_members_message, - greet_new_chat_members, - tag_hint, - say_potato_command, - say_potato_button, - ban_sender_channels, ) -from components.errorhandler import error_handler from components.const import ( OFFTOPIC_RULES, - OFFTOPIC_USERNAME, ONTOPIC_RULES, - ONTOPIC_USERNAME, ONTOPIC_RULES_MESSAGE_ID, OFFTOPIC_RULES_MESSAGE_ID, ONTOPIC_CHAT_ID, OFFTOPIC_CHAT_ID, ) -from components.taghints import TagHintFilter -from components.util import ( - rate_limit_tracker, - build_command_list, -) +from components.errorhandler import error_handler from components.github import github_issues if os.environ.get("ROOLSBOT_DEBUG"): @@ -102,31 +75,31 @@ def main() -> None: # Note: Order matters! - dispatcher.add_handler(MessageHandler(~Filters.command, rate_limit_tracker), group=-1) - dispatcher.add_handler( - MessageHandler( - Filters.sender_chat.channel & ~Filters.is_automatic_forward, ban_sender_channels - ) - ) - - # Simple commands - # The first one also handles deep linking /start commands - dispatcher.add_handler(CommandHandler("start", start)) - dispatcher.add_handler(CommandHandler("rules", rules)) - dispatcher.add_handler(CommandHandler("docs", docs)) - dispatcher.add_handler(CommandHandler("wiki", wiki)) - dispatcher.add_handler(CommandHandler("help", help_callback)) - - # Stuff that runs on every message with regex - dispatcher.add_handler( - MessageHandler( - Filters.regex(r"(?i)[\s\S]*?((sudo )?make me a sandwich)[\s\S]*?"), sandwich - ) - ) - dispatcher.add_handler(MessageHandler(Filters.regex("/(on|off)_topic"), off_on_topic)) - - # Tag hints - works with regex - dispatcher.add_handler(MessageHandler(TagHintFilter(), tag_hint)) + # dispatcher.add_handler(MessageHandler(~Filters.command, rate_limit_tracker), group=-1) + # dispatcher.add_handler( + # MessageHandler( + # Filters.sender_chat.channel & ~Filters.is_automatic_forward, ban_sender_channels + # ) + # ) + + # # Simple commands + # # The first one also handles deep linking /start commands + # dispatcher.add_handler(CommandHandler("start", start)) + # dispatcher.add_handler(CommandHandler("rules", rules)) + # dispatcher.add_handler(CommandHandler("docs", docs)) + # dispatcher.add_handler(CommandHandler("wiki", wiki)) + # dispatcher.add_handler(CommandHandler("help", help_callback)) + + # # Stuff that runs on every message with regex + # dispatcher.add_handler( + # MessageHandler( + # Filters.regex(r"(?i)[\s\S]*?((sudo )?make me a sandwich)[\s\S]*?"), sandwich + # ) + # ) + # dispatcher.add_handler(MessageHandler(Filters.regex("/(on|off)_topic"), off_on_topic)) + + # # Tag hints - works with regex + # dispatcher.add_handler(MessageHandler(TagHintFilter(), tag_hint)) # We need several matches so Filters.regex is basically useless # therefore we catch everything and do regex ourselves @@ -134,31 +107,31 @@ def main() -> None: MessageHandler(Filters.text & Filters.update.messages & ~Filters.command, reply_search) ) - # Status updates - dispatcher.add_handler( - ChatMemberHandler(greet_new_chat_members, chat_member_types=ChatMemberHandler.CHAT_MEMBER) - ) - dispatcher.add_handler( - MessageHandler( - Filters.chat(username=[ONTOPIC_USERNAME, OFFTOPIC_USERNAME]) - & Filters.status_update.new_chat_members, - delete_new_chat_members_message, - ), - group=1, - ) + # # Status updates + # dispatcher.add_handler( + # ChatMemberHandler(greet_new_chat_members, chat_member_types=ChatMemberHandler.CHAT_MEMBER) + # ) + # dispatcher.add_handler( + # MessageHandler( + # Filters.chat(username=[ONTOPIC_USERNAME, OFFTOPIC_USERNAME]) + # & Filters.status_update.new_chat_members, + # delete_new_chat_members_message, + # ), + # group=1, + # ) # Inline Queries dispatcher.add_handler(InlineQueryHandler(inlinequeries.inline_query)) - # Captcha for userbots - dispatcher.add_handler( - CommandHandler( - "say_potato", - say_potato_command, - filters=Filters.chat(username=[ONTOPIC_USERNAME, OFFTOPIC_USERNAME]), - ) - ) - dispatcher.add_handler(CallbackQueryHandler(say_potato_button, pattern="^POTATO")) + # # Captcha for userbots + # dispatcher.add_handler( + # CommandHandler( + # "say_potato", + # say_potato_command, + # filters=Filters.chat(username=[ONTOPIC_USERNAME, OFFTOPIC_USERNAME]), + # ) + # ) + # dispatcher.add_handler(CallbackQueryHandler(say_potato_button, pattern="^POTATO")) # Error Handler dispatcher.add_error_handler(error_handler) @@ -176,25 +149,25 @@ def main() -> None: github_issues.init_ptb_contribs(dispatcher.job_queue) # type: ignore[arg-type] github_issues.init_issues(dispatcher.job_queue) # type: ignore[arg-type] - # set commands - updater.bot.set_my_commands( - build_command_list(private=True), - scope=BotCommandScopeAllPrivateChats(), - ) - updater.bot.set_my_commands( - build_command_list(private=False), - scope=BotCommandScopeAllGroupChats(), - ) - - for group_name in [ONTOPIC_CHAT_ID, OFFTOPIC_CHAT_ID]: - updater.bot.set_my_commands( - build_command_list(private=False, group_name=group_name), - scope=BotCommandScopeChat(group_name), - ) - updater.bot.set_my_commands( - build_command_list(private=False, group_name=group_name, admins=True), - scope=BotCommandScopeChatAdministrators(group_name), - ) + # # set commands + # updater.bot.set_my_commands( + # build_command_list(private=True), + # scope=BotCommandScopeAllPrivateChats(), + # ) + # updater.bot.set_my_commands( + # build_command_list(private=False), + # scope=BotCommandScopeAllGroupChats(), + # ) + # + # for group_name in [ONTOPIC_CHAT_ID, OFFTOPIC_CHAT_ID]: + # updater.bot.set_my_commands( + # build_command_list(private=False, group_name=group_name), + # scope=BotCommandScopeChat(group_name), + # ) + # updater.bot.set_my_commands( + # build_command_list(private=False, group_name=group_name, admins=True), + # scope=BotCommandScopeChatAdministrators(group_name), + # ) updater.idle() From 71a6a601bd2b430134095053d43dbe5dcded3f50 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 6 May 2022 20:44:34 +0200 Subject: [PATCH 2/4] don't update rules messages --- rules_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules_bot.py b/rules_bot.py index a7e1984..ed3e6c9 100644 --- a/rules_bot.py +++ b/rules_bot.py @@ -71,7 +71,7 @@ def main() -> None: defaults = Defaults(parse_mode=ParseMode.HTML, disable_web_page_preview=True) updater = Updater(token=config["KEYS"]["bot_api"], defaults=defaults) dispatcher = updater.dispatcher - update_rules_messages(updater.bot) + # update_rules_messages(updater.bot) # Note: Order matters! From 7417c40e360c85d9adfb8a7eaec88d2242619813 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 6 May 2022 20:47:50 +0200 Subject: [PATCH 3/4] Don't fetch github data --- rules_bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rules_bot.py b/rules_bot.py index ed3e6c9..6e1c41a 100644 --- a/rules_bot.py +++ b/rules_bot.py @@ -146,8 +146,8 @@ def main() -> None: except KeyError: logging.info("No github api token set. Rate-limit is 60 requests/hour without auth.") - github_issues.init_ptb_contribs(dispatcher.job_queue) # type: ignore[arg-type] - github_issues.init_issues(dispatcher.job_queue) # type: ignore[arg-type] + # github_issues.init_ptb_contribs(dispatcher.job_queue) # type: ignore[arg-type] + # github_issues.init_issues(dispatcher.job_queue) # type: ignore[arg-type] # # set commands # updater.bot.set_my_commands( From a73a2935608be18ff46e0e90881f1f9379dfb0cf Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 6 May 2022 20:57:02 +0200 Subject: [PATCH 4/4] update examples link, remove github from inline search --- components/const.py | 2 +- components/search.py | 43 +++++++++++++++++++++---------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/components/const.py b/components/const.py index 27d9550..7fbfde0 100644 --- a/components/const.py +++ b/components/const.py @@ -35,7 +35,7 @@ WIKI_CODE_SNIPPETS_URL = urljoin(WIKI_URL, "Code-snippets") WIKI_FAQ_URL = urljoin(WIKI_URL, "Frequently-Asked-Questions") WIKI_FRDP_URL = urljoin(WIKI_URL, "Frequently-requested-design-patterns") -EXAMPLES_URL = urljoin(PROJECT_URL, "tree/master/examples/") +EXAMPLES_URL = urljoin(PROJECT_URL, "tree/v13.x/examples/") ONTOPIC_RULES = """ This group is for questions, answers and discussions around the \ python-telegram-bot library and, to some extent, \ diff --git a/components/search.py b/components/search.py index c1cece2..246b9d0 100644 --- a/components/search.py +++ b/components/search.py @@ -20,7 +20,6 @@ WIKI_CODE_SNIPPETS_URL, WIKI_FAQ_URL, EXAMPLES_URL, - GITHUB_PATTERN, WIKI_FRDP_URL, ) from .entrytypes import ( @@ -240,27 +239,27 @@ def search(self, search_query: Optional[str], amount: int = None) -> Optional[Li """ search_entries: Iterable[BaseEntry] = [] - match = GITHUB_PATTERN.fullmatch(search_query) if search_query else None - if match: - owner, repo, number, sha, gh_search_query, ptbcontrib = [ - match.groupdict()[x] - for x in ("owner", "repo", "number", "sha", "query", "ptbcontrib") - ] - - # If it's an issue - if number: - issue = github_issues.get_issue(int(number), owner, repo) - return [issue] if issue else None - # If it's a commit - if sha: - commit = github_issues.get_commit(sha, owner, repo) - return [commit] if commit else None - # If it's a search - if gh_search_query: - search_query = gh_search_query - search_entries = github_issues.all_issues - elif ptbcontrib: - search_entries = github_issues.all_ptbcontribs + # match = GITHUB_PATTERN.fullmatch(search_query) if search_query else None + # if match: + # owner, repo, number, sha, gh_search_query, ptbcontrib = [ + # match.groupdict()[x] + # for x in ("owner", "repo", "number", "sha", "query", "ptbcontrib") + # ] + # + # # If it's an issue + # if number: + # issue = github_issues.get_issue(int(number), owner, repo) + # return [issue] if issue else None + # # If it's a commit + # if sha: + # commit = github_issues.get_commit(sha, owner, repo) + # return [commit] if commit else None + # # If it's a search + # if gh_search_query: + # search_query = gh_search_query + # search_entries = github_issues.all_issues + # elif ptbcontrib: + # search_entries = github_issues.all_ptbcontribs if search_query and search_query.startswith("/"): search_entries = TAG_HINTS.values()