├── .github └── workflows │ └── pre-commit_dependencies_notifier.yml ├── .gitignore ├── .pre-commit-config.yaml ├── LICENSE ├── README.md ├── bot_example.ini ├── components ├── __init__.py ├── callbacks.py ├── const.py ├── entrytypes.py ├── errorhandler.py ├── github.py ├── graphql_queries │ ├── getCommit.gql │ ├── getDiscussions.gql │ ├── getExamples.gql │ ├── getIssues.gql │ ├── getPTBContribs.gql │ ├── getPullRequests.gql │ └── getThread.gql ├── graphqlclient.py ├── inlinequeries.py ├── joinrequests.py ├── rulesjobqueue.py ├── search.py ├── taghints.py └── util.py ├── pyproject.toml ├── requirements-dev.txt ├── requirements.txt ├── rules_bot.py └── setup.cfg /.github/workflows/pre-commit_dependencies_notifier.yml: -------------------------------------------------------------------------------- 1 | name: Warning maintainers 2 | on: 3 | pull_request_target: 4 | paths: 5 | - requirements.txt 6 | - .pre-commit-config.yaml 7 | permissions: 8 | pull-requests: write 9 | jobs: 10 | job: 11 | runs-on: ubuntu-latest 12 | name: about pre-commit and dependency change 13 | steps: 14 | - name: running the check 15 | uses: Poolitzer/notifier-action@master 16 | with: 17 | notify-message: Hey! Looks like you edited the requirements or the pre-commit hooks. I'm just a friendly reminder to keep the additional dependencies for the hooks in sync with the requirements :) 18 | repo-token: ${{ secrets.GITHUB_TOKEN }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Python template 3 | # Byte-compiled / optimized / DLL files 4 | __pycache__/ 5 | *.py[cod] 6 | *$py.class 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 13 | env/ 14 | build/ 15 | develop-eggs/ 16 | dist/ 17 | downloads/ 18 | eggs/ 19 | .eggs/ 20 | lib/ 21 | lib64/ 22 | parts/ 23 | sdist/ 24 | var/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *,cover 48 | .hypothesis/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | 58 | # Flask stuff: 59 | instance/ 60 | .webassets-cache 61 | 62 | # Scrapy stuff: 63 | .scrapy 64 | 65 | # Sphinx documentation 66 | docs/_build/ 67 | 68 | # PyBuilder 69 | target/ 70 | 71 | # IPython Notebook 72 | .ipynb_checkpoints 73 | 74 | # pyenv 75 | .python-version 76 | 77 | # celery beat schedule file 78 | celerybeat-schedule 79 | 80 | # dotenv 81 | .env 82 | 83 | # virtualenv 84 | venv/ 85 | ENV/ 86 | 87 | # Spyder project settings 88 | .spyderproject 89 | 90 | # Rope project settings 91 | .ropeproject 92 | 93 | .idea/ 94 | bot.ini 95 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | # Make sure that 2 | # * the revs specified here match requirements-dev.txt 3 | # * the additional_dependencies here match requirements.txt 4 | 5 | ci: 6 | autofix_prs: false 7 | autoupdate_schedule: monthly 8 | 9 | repos: 10 | - repo: https://github.com/psf/black 11 | rev: 25.1.0 12 | hooks: 13 | - id: black 14 | args: 15 | - --diff 16 | - --check 17 | - repo: https://github.com/PyCQA/flake8 18 | rev: 7.2.0 19 | hooks: 20 | - id: flake8 21 | - repo: https://github.com/PyCQA/pylint 22 | rev: v3.3.7 23 | hooks: 24 | - id: pylint 25 | args: 26 | - --rcfile=setup.cfg 27 | additional_dependencies: 28 | - beautifulsoup4~=4.11.0 29 | - thefuzz~=0.19.0 30 | - python-telegram-bot[job-queue]==20.2 31 | - Sphinx~=5.0.2 32 | - httpx~=0.23.0 33 | - gql[aiohttp]~=3.5.0 34 | - async-lru~=1.0.3 35 | - repo: https://github.com/pre-commit/mirrors-mypy 36 | rev: v1.16.0 37 | hooks: 38 | - id: mypy 39 | additional_dependencies: 40 | - beautifulsoup4~=4.11.0 41 | - thefuzz~=0.19.0 42 | - python-telegram-bot[job-queue]==20.2 43 | - Sphinx~=5.0.2 44 | - httpx~=0.23.0 45 | - gql[aiohttp]~=3.5.0 46 | - async-lru~=1.0.3 47 | - repo: https://github.com/asottile/pyupgrade 48 | rev: v3.20.0 49 | hooks: 50 | - id: pyupgrade 51 | args: 52 | - --py36-plus 53 | - repo: https://github.com/pycqa/isort 54 | rev: 6.0.1 55 | hooks: 56 | - id: isort 57 | name: isort 58 | args: 59 | - --diff 60 | - --check 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rules-bot 2 | 3 | The Telegram bot @roolsbot serves the python-telegram-bot [group](https://telegram.me/pythontelegrambotgroup) [chats](https://t.me/pythontelegrambottalk) by announcing the rules and searching the [docs](https://python-telegram-bot.readthedocs.io/) & [wiki](https://github.com/python-telegram-bot/python-telegram-bot/wiki) of [python-telegram-bot](https://python-telegram-bot.org) 4 | 5 | So what exactly can this bot do? 6 | 7 | ## Inline Mode 8 | 9 | rules-bot has an extensive inline functionality. It has basically two components: 10 | 11 | ### Direct Search 12 | 13 | Typing `@roolsbot ` will present you with a list of search results, from which you can select. Things than can be searched for: 14 | 15 | * [Wiki](https://github.com/python-telegram-bot/python-telegram-bot/wiki) pages 16 | * entries on the [FAQ](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Frequently-Asked-Questions) and [code snippets](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets) pages 17 | * Entries in the [documentation](https://python-telegram-bot.readthedocs.io/en/stable/) 18 | * Examples from the [examples directory](https://github.com/python-telegram-bot/python-telegram-bot/tree/master/examples#examples) 19 | * Tag hints as described [in this section](#short-replies) 20 | 21 | rules-bot tries really hard to provide you with the closest match to your query. This is not always easy, so you might need to scroll a bit. 22 | 23 | Also, special prefixes restrict the search results: 24 | 25 | * Prepending the query with `/` will search *only* for [tag hints](#short-replies) 26 | * Prepending the query with `#`/`PR-`/`GH-` will search *only* for entries on GitHub as described [in this section](#link-to-github-threads). This also allows you to search issues & pull request titles on the [GitHub repository](https://github.com/python-telegram-bot/python-telegram-bot). 27 | 28 | ### Insertion Search 29 | 30 | Instead of searching for just one result, you can also insert links into a message by wrapping search queries in `++`. The syntax for the search queries is exactly as described above. For example 31 | 32 | ``` 33 | @roolsbot I 💙 +InlineQueries+, but you need an +InlineQueryHandler+ for it. 34 | ``` 35 | becomes 36 | 37 | > I 💙 [InlineQueries](https://python-telegram-bot.readthedocs.io/en/stable/telegram.inlinequery.html#telegram.InlineQuery), but you need an [InlineQueryHandler](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.inlinequeryhandler.html#telegram.ext.InlineQueryHandler) for it. 38 | 39 | For each inserted search query, rules-bot will search for the three best matches and will offer you all possible combinations of the corresponding results. 40 | 41 | Please note that Telegram will only parse the first 256 characters of your inline query. Everything else will be cut off. 42 | 43 | # Texting Mode 44 | 45 | ## Short-Replies 46 | 47 | rules-bot provides a number of predefined messages that are frequently needed. A list of available tag hints is available in the command menu. Simply send `/` and rules-bot will delete your message and send the corresponding text instead. Reply to a message with `/` to make rules-bot send the message as reply to that message. Type `/ `, to insert the personal message at a meaningful spot within the message. For most tag hints this will just prepend the personal message. You can even send multiple short messages at once by typing `/ / ...` 48 | 49 | ## Redirect to On- & Off-Topic 50 | 51 | To redirect a user to the on-/off-topic group simply reply with `/on_topic` or `/off_topic` to their message. The hint may also be part of a longer message. 52 | 53 | ## Link To GitHub Threads 54 | 55 | When mentioning issues, pull requests, commit SHAs or `ptbcontrib` contributions in the same manner, rules-bot will automatically reply to your message with the corresponding links to the [GitHub repository](https://github.com/python-telegram-bot/python-telegram-bot) of python-telegram-bot. If your message is a reply to another message, the links will be sent as reply to that message. 56 | 57 | Mentioning those works in the following forms: 58 | 59 | * `ptbcontrib/name` with the (directory) name of a contribution of [ptbcontrib](https://github.com/python-telegram-bot/ptbcontrib/tree/main/ptbcontrib) 60 | * `#number` with the number of an issue/pull request 61 | * `#phrase` with a phrase to search for in issue/pull request titles 62 | * `@sha` with a commit SHA 63 | 64 | In the last three cases, `#` may be replaced by `GH-` or `PR-` or you can prepend 65 | 66 | * `repo` to search in the repo `https://github.com/python-telegram-bot/repo` 67 | * `owner/` to search in the repo `https://github.com/owner/repo` 68 | 69 | ## Link to search results 70 | 71 | The searching functionality described [above](#inline-mode) is also available outside of the inline mode. In a message, mark your search queries as `++` put `!search` at the very start or end of your message. Then rules-bot will automatically reply with the best match for each of the queries. If your message is a reply to another message, the message will sent as reply to that message. 72 | The search results are combined with links to GitHub threads, if applicable. 73 | 74 | ## Welcome Members 75 | 76 | rules-bot will automatically delete the service messages announcing new members. Instead, it will welcome new members by mentioning them in a short message that links to a message stating the rules of the group. New members are welcomed in batches. Currently, there will be at most one welcoming message per hour. The linked rules messages are updated with the current rules on start-up. 77 | 78 | ## Fixed Commands 79 | 80 | * `/docs`: Sends the link to the docs. 81 | * `/wiki`: Sends the link to the wiki. 82 | * `/help`: Links to this readme. 83 | * `/say_potato`: Asks a user to verify that they are not a userbot. Only available to group admins. 84 | 85 | ## Other 86 | 87 | rules-bot can make sandwiches. You can ask it to do so by typing `make me a sandwich`. We'll see if it actually does 😉 88 | 89 | # Setting up the bot for development and testing 90 | 91 | Copy the [example INI file `bot_example.ini`](bot_example.ini) to `bot.ini` and fill in your credentials: 92 | - `bot_api`: your Bot API you [got from Botfather](https://core.telegram.org/bots/features#botfather); 93 | - `github_auth`: your GitHub personal access token (see [instructions](https://docs.github.com/en/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql)). 94 | GraphQL will need this token to be prepended with `Bearer `, but it will be done automatically if you don't. 95 | 96 | **Be sure not to commit `bot.ini`.** 97 | 98 | # Token Detection 99 | 100 | rules-bot will detect **valid** bot tokens on messages and warn users about the leak, posting the name(s) of the affected bot(s) and telling them to go revoke at @BotFather 101 | -------------------------------------------------------------------------------- /bot_example.ini: -------------------------------------------------------------------------------- 1 | [KEYS] 2 | bot_api = 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 3 | github_auth = Bearer ghp_(and the rest of your GitHub token) 4 | -------------------------------------------------------------------------------- /components/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python-telegram-bot/rules-bot/6eb6758b2d657f43bbead3bdc297ed29508a10e0/components/__init__.py -------------------------------------------------------------------------------- /components/callbacks.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import contextlib 3 | import html 4 | import json 5 | import logging 6 | import random 7 | import re 8 | import time 9 | from collections import deque 10 | from collections.abc import MutableSequence, Sequence 11 | from copy import deepcopy 12 | from typing import Dict, List, Match, Optional, Tuple, cast 13 | 14 | from httpx import codes 15 | from telegram import ( 16 | CallbackQuery, 17 | Chat, 18 | InlineKeyboardButton, 19 | InlineKeyboardMarkup, 20 | Message, 21 | MessageEntity, 22 | Update, 23 | User, 24 | ) 25 | from telegram.constants import ChatAction, MessageLimit 26 | from telegram.error import BadRequest 27 | from telegram.ext import Application, ApplicationHandlerStop, ContextTypes, Job, JobQueue 28 | from telegram.helpers import escape_markdown 29 | 30 | from components import const 31 | from components.const import ( 32 | ALLOWED_CHAT_IDS, 33 | ALLOWED_USERNAMES, 34 | BUY_TEXT, 35 | DEFAULT_REPO_NAME, 36 | DEFAULT_REPO_OWNER, 37 | ENCLOSED_REGEX, 38 | ENCLOSING_REPLACEMENT_CHARACTER, 39 | GITHUB_PATTERN, 40 | OFFTOPIC_CHAT_ID, 41 | OFFTOPIC_RULES, 42 | OFFTOPIC_USERNAME, 43 | ONTOPIC_CHAT_ID, 44 | ONTOPIC_RULES, 45 | ONTOPIC_USERNAME, 46 | PRIVACY_POLICY, 47 | TOKEN_TEXT, 48 | VEGETABLES, 49 | ) 50 | from components.entrytypes import BaseEntry 51 | from components.search import Search 52 | from components.taghints import TAG_HINTS 53 | from components.util import ( 54 | admin_check, 55 | get_bot_from_token, 56 | get_reply_id, 57 | get_text_not_in_entities, 58 | rate_limit, 59 | reply_or_edit, 60 | try_to_delete, 61 | update_shared_token_timestamp, 62 | ) 63 | 64 | 65 | async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 66 | message = cast(Message, update.message) 67 | username = cast(Chat, update.effective_chat) 68 | args = context.args 69 | 70 | # For deep linking 71 | if args: 72 | if args[0] == "inline-help": 73 | await inlinequery_help(update, context) 74 | if args[0] == "inline-entity-parsing": 75 | await inlinequery_entity_parsing(update, context) 76 | elif username not in (OFFTOPIC_USERNAME, ONTOPIC_USERNAME): 77 | await message.reply_text( 78 | "Hi. I'm a bot that will announce the rules of the " 79 | "python-telegram-bot groups when you type /rules." 80 | ) 81 | 82 | 83 | async def inlinequery_help(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 84 | message = cast(Message, update.message) 85 | char = ENCLOSING_REPLACEMENT_CHARACTER 86 | self_chat_id = f"@{context.bot.username}" 87 | text = ( 88 | f"Use the `{char}`-character in your inline queries and I will replace " 89 | f"them with a link to the corresponding article from the documentation or wiki.\n\n" 90 | f"*Example:*\n" 91 | f"{escape_markdown(self_chat_id)} I 💙 {char}InlineQueries{char}, " 92 | f"but you need an {char}InlineQueryHandler{char} for it.\n\n" 93 | f"*becomes:*\n" 94 | f"I 💙 [InlineQueries](" 95 | f"{const.DOCS_URL}en/latest/telegram.html#telegram" 96 | f".InlineQuery), but you need an [InlineQueryHandler]({const.DOCS_URL}en" 97 | f"/latest/telegram.ext.html#telegram.ext.InlineQueryHandler) for it.\n\n" 98 | f"Some wiki pages have spaces in them. Please replace such spaces with underscores. " 99 | f"The bot will automatically change them back desired space." 100 | ) 101 | await message.reply_markdown(text) 102 | 103 | 104 | async def inlinequery_entity_parsing(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: 105 | text = ( 106 | "Your inline query produced invalid message entities. If you are trying to combine " 107 | "custom text with a tag hint or search result, please keep in mind that the text is " 108 | "is processed with telegram.ParseMode.HTML formatting. You will therefore " 109 | "have to either use valid HTML-formatted text or escape reserved characters. For a list " 110 | "of reserved characters, please see the official " 111 | f"Telegram docs." 112 | ) 113 | await cast(Message, update.effective_message).reply_text(text) 114 | 115 | 116 | @rate_limit 117 | async def rules(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 118 | """Load and send the appropriate rules based on which group we're in""" 119 | message = cast(Message, update.effective_message) 120 | if message.chat.username == ONTOPIC_USERNAME: 121 | await message.reply_text(ONTOPIC_RULES, quote=False) 122 | context.application.create_task(try_to_delete(message), update=update) 123 | elif message.chat.username == OFFTOPIC_USERNAME: 124 | await message.reply_text(OFFTOPIC_RULES, quote=False) 125 | context.application.create_task(try_to_delete(message), update=update) 126 | else: 127 | await message.reply_text( 128 | "Hmm. You're not in a python-telegram-bot group, " 129 | "and I don't know the rules around here." 130 | ) 131 | 132 | 133 | async def off_on_topic(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 134 | """Redirect users to the off-topic or on-topic group""" 135 | # Minimal effort LRU cache 136 | # We store the newest 64 messages that lead to redirection to minimize the chance that 137 | # editing a message falsely triggers the redirect again 138 | parsed_messages = cast(Dict, context.chat_data).setdefault( 139 | "redirect_messages", deque(maxlen=64) 140 | ) 141 | 142 | message = cast(Message, update.effective_message) 143 | if message.message_id in parsed_messages: 144 | return 145 | 146 | # Standalone on/off-topic commands don't make any sense 147 | # But we only delete them if they contain nothing but the command 148 | if not message.reply_to_message: 149 | entities = message.parse_entities() 150 | if len(entities) == 1: 151 | entity, text = entities.popitem() 152 | if entity.type == MessageEntity.BOT_COMMAND and text == message.text: 153 | context.application.create_task(try_to_delete(message), update=update) 154 | return 155 | 156 | chat_username = cast(Chat, update.effective_chat).username 157 | group_one = cast(Match, context.match).group(1) 158 | if chat_username == ONTOPIC_USERNAME and group_one.lower() == "off": 159 | reply = message.reply_to_message 160 | if reply.text: 161 | issued_reply = get_reply_id(update) 162 | 163 | if reply.from_user: 164 | if reply.from_user.username: 165 | name = "@" + reply.from_user.username 166 | else: 167 | name = reply.from_user.first_name 168 | else: 169 | # Probably never happens anyway ... 170 | name = "Somebody" 171 | 172 | replied_message_text = reply.text_html 173 | replied_message_id = reply.message_id 174 | 175 | text = ( 176 | f'{name} wrote:\n' 177 | f"{replied_message_text}\n\n" 178 | f"⬇️ ᴘʟᴇᴀsᴇ ᴄᴏɴᴛɪɴᴜᴇ ʜᴇʀᴇ ⬇️" 179 | ) 180 | 181 | offtopic_msg = await context.bot.send_message(OFFTOPIC_CHAT_ID, text) 182 | 183 | await message.reply_text( 184 | text=( 185 | 'I moved this discussion to the off-topic group.' 187 | ), 188 | reply_to_message_id=issued_reply, 189 | ) 190 | 191 | else: 192 | await message.reply_text( 193 | f'The off-topic group is here. ' 194 | "Come join us!", 195 | ) 196 | 197 | elif chat_username == OFFTOPIC_USERNAME and group_one.lower() == "on": 198 | await message.reply_text( 199 | f'The on-topic group is here. ' 200 | "Come join us!", 201 | ) 202 | 203 | parsed_messages.append(message.message_id) 204 | 205 | 206 | async def sandwich(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 207 | message = cast(Message, update.effective_message) 208 | username = cast(Chat, update.effective_chat).username 209 | if username == OFFTOPIC_USERNAME: 210 | if "sudo" in cast(Match, context.match).group(0): 211 | await message.reply_text("Okay.", quote=True) 212 | else: 213 | await message.reply_text("What? Make it yourself.", quote=True) 214 | 215 | 216 | def keep_typing(last: float, chat: Chat, action: str, application: Application) -> float: 217 | now = time.time() 218 | if (now - last) > 1: 219 | application.create_task(chat.send_action(action)) 220 | return now 221 | 222 | 223 | async def reply_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 224 | """When sending a message of the form `!search foo +search query+ bar` or 225 | `foo +search query+ bar !search`, the bot will reply with links to the closet search results. 226 | If the message is a reply, the bot will reply to the referenced message directly. 227 | """ 228 | message = cast(Message, update.effective_message) 229 | if not message.text: 230 | return 231 | 232 | last = 0.0 233 | github_matches: List[Tuple[int, Tuple[str, str, str, str, str]]] = [] 234 | found_entries: List[Tuple[int, BaseEntry]] = [] 235 | no_entity_text = get_text_not_in_entities(message).strip() 236 | 237 | search = cast(Search, context.bot_data["search"]) 238 | github = search.github 239 | 240 | # Parse exact matches for GitHub threads & ptbcontrib found_entries first 241 | for match in GITHUB_PATTERN.finditer(no_entity_text): 242 | logging.debug(match.groupdict()) 243 | owner, repo, number, sha, ptbcontrib = ( 244 | cast(str, match.groupdict()[x]) 245 | for x in ("owner", "repo", "number", "sha", "ptbcontrib") 246 | ) 247 | if number or sha or ptbcontrib: 248 | github_matches.append((match.start(), (owner, repo, number, sha, ptbcontrib))) 249 | 250 | for gh_match in github_matches: 251 | last = keep_typing( 252 | last, 253 | cast(Chat, update.effective_chat), 254 | ChatAction.TYPING, 255 | application=context.application, 256 | ) 257 | owner, repo, number, sha, ptbcontrib = gh_match[1] 258 | owner = owner or DEFAULT_REPO_OWNER 259 | repo = repo or DEFAULT_REPO_NAME 260 | if number: 261 | issue = await github.get_thread(int(number), owner, repo) 262 | if issue is not None: 263 | found_entries.append((gh_match[0], issue)) 264 | elif sha: 265 | commit = await github.get_commit(sha, owner, repo) 266 | if commit is not None: 267 | found_entries.append((gh_match[0], commit)) 268 | elif ptbcontrib: 269 | contrib = github.ptb_contribs.get(ptbcontrib) 270 | if contrib: 271 | found_entries.append((gh_match[0], contrib)) 272 | 273 | # Parse fuzzy search next, if requested. Here we use message.text instead of no_entity_text 274 | # to avoid tlds like .bot and .app to mess things up for us 275 | if message.text.startswith("!search") or message.text.endswith("!search"): 276 | for match in ENCLOSED_REGEX.finditer(message.text): 277 | last = keep_typing( 278 | last, 279 | cast(Chat, update.effective_chat), 280 | ChatAction.TYPING, 281 | application=context.application, 282 | ) 283 | found_entries.append( 284 | (match.start(), (await search.search(match.group(0), amount=1))[0]) 285 | ) 286 | 287 | # Sort the found_entries - only necessary if we appended something here 288 | found_entries.sort(key=lambda thing: thing[0]) 289 | 290 | if found_entries: 291 | # Make sure that user gets unique hyperlinks. 292 | # (Using dict instead of set to preserve the order) 293 | html_markup_items = {entry[1].html_reply_markup(): None for entry in found_entries}.keys() 294 | await reply_or_edit(update, context, "\n".join(html_markup_items)) 295 | 296 | 297 | async def delete_message(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: 298 | await try_to_delete(cast(Message, update.effective_message)) 299 | 300 | 301 | async def leave_chat(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: 302 | if ( 303 | not (chat := update.effective_chat) 304 | or chat.type == chat.PRIVATE 305 | or chat.username in ALLOWED_USERNAMES 306 | or chat.id in ALLOWED_CHAT_IDS 307 | ): 308 | return 309 | 310 | with contextlib.suppress(BadRequest): 311 | await chat.leave() 312 | 313 | raise ApplicationHandlerStop 314 | 315 | 316 | async def raise_app_handler_stop(_: Update, __: ContextTypes.DEFAULT_TYPE) -> None: 317 | raise ApplicationHandlerStop 318 | 319 | 320 | async def tag_hint(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 321 | """Replies to tag hints like /docs, /xy, /askright.""" 322 | message = cast(Message, update.effective_message) 323 | reply_to = message.reply_to_message 324 | first_match = cast(int, MessageLimit.MAX_TEXT_LENGTH) 325 | 326 | messages = [] 327 | buttons: Optional[MutableSequence[Sequence[InlineKeyboardButton]]] = None 328 | for match in cast(List[Match], context.matches): 329 | first_match = min(first_match, match.start(0)) 330 | 331 | # get the hints name, e.g. "askright" 332 | hint = TAG_HINTS[match.groupdict()["tag_hint"].lstrip("/")] 333 | 334 | # Store the message 335 | messages.append(hint.html_markup(None or match.group(0))) 336 | 337 | # Merge keyboards into one 338 | if entry_kb := hint.inline_keyboard: 339 | if buttons is None: 340 | buttons = [ 341 | [deepcopy(button) for button in row] for row in entry_kb.inline_keyboard 342 | ] 343 | else: 344 | buttons.extend(entry_kb.inline_keyboard) 345 | 346 | keyboard = InlineKeyboardMarkup(buttons) if buttons else None 347 | 348 | effective_text = "\n➖\n".join(messages) 349 | await message.reply_text( 350 | effective_text, 351 | reply_markup=keyboard, 352 | reply_to_message_id=get_reply_id(update), 353 | ) 354 | 355 | if reply_to and first_match == 0: 356 | await try_to_delete(message) 357 | 358 | 359 | async def ban_sender_channels(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: 360 | message = cast(Message, update.effective_message) 361 | await cast(Chat, update.effective_chat).ban_sender_chat(cast(Chat, message.sender_chat).id) 362 | await try_to_delete(message) 363 | 364 | 365 | async def say_potato_job(context: ContextTypes.DEFAULT_TYPE) -> None: 366 | user_id, message, who_banned = cast(Tuple[int, Message, User], cast(Job, context.job).data) 367 | await asyncio.gather( 368 | context.bot.ban_chat_member(chat_id=ONTOPIC_CHAT_ID, user_id=user_id), 369 | context.bot.ban_chat_member(chat_id=OFFTOPIC_CHAT_ID, user_id=user_id), 370 | ) 371 | 372 | text = ( 373 | "You have been banned for userbot-like behavior. If you are not a userbot and wish to be " 374 | f"unbanned, please contact {who_banned.mention_html()}." 375 | ) 376 | await message.edit_text(text=text) 377 | 378 | 379 | async def say_potato_button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 380 | callback_query = cast(CallbackQuery, update.callback_query) 381 | _, user_id, correct = cast(str, callback_query.data).split() 382 | 383 | if str(callback_query.from_user.id) != user_id: 384 | await callback_query.answer( 385 | text="This button is obviously not meant for you. 😉", show_alert=True 386 | ) 387 | return 388 | 389 | jobs = cast(JobQueue, context.job_queue).get_jobs_by_name(f"POTATO {user_id}") 390 | if not jobs: 391 | return 392 | job = jobs[0] 393 | 394 | if correct == "True": 395 | await callback_query.answer( 396 | text="Thanks for the verification! Have fun in the group 🙂", show_alert=True 397 | ) 398 | await cast(Message, callback_query.message).delete() 399 | else: 400 | await callback_query.answer(text="That was wrong. Ciao! 👋", show_alert=True) 401 | await job.run(context.application) 402 | 403 | job.schedule_removal() 404 | 405 | 406 | async def say_potato_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 407 | message = cast(Message, update.effective_message) 408 | who_banned = cast(User, message.from_user) 409 | chat = cast(Chat, update.effective_chat) 410 | 411 | if not await admin_check(cast(Dict, context.chat_data), chat, who_banned): 412 | await message.reply_text( 413 | "This command is only available for admins. You are not an admin." 414 | ) 415 | return 416 | 417 | await try_to_delete(message) 418 | 419 | if not message.reply_to_message: 420 | return 421 | 422 | user = cast(User, message.reply_to_message.from_user) 423 | 424 | try: 425 | time_limit = int(context.args[0]) # type: ignore[index] 426 | except (ValueError, IndexError): 427 | time_limit = 60 428 | 429 | correct, incorrect_1, incorrect_2 = random.sample(VEGETABLES, 3) 430 | 431 | message_text = ( 432 | f"You display behavior that is common for userbots, i.e. automated Telegram " 433 | f"accounts that usually produce spam. Please verify that you are not a userbot by " 434 | f"clicking the button that says »{correct}«.\nIf you don't press the button " 435 | f"within {time_limit} minutes, you will be banned from the PTB groups. If you miss the " 436 | f"time limit but are not a userbot and want to get unbanned, please contact " 437 | f"{who_banned.mention_html()}." 438 | ) 439 | 440 | answers = random.sample([(correct, True), (incorrect_1, False), (incorrect_2, False)], 3) 441 | keyboard = InlineKeyboardMarkup.from_row( 442 | [ 443 | InlineKeyboardButton(text=veg, callback_data=f"POTATO {user.id} {truth}") 444 | for veg, truth in answers 445 | ] 446 | ) 447 | 448 | potato_message = await message.reply_to_message.reply_text(message_text, reply_markup=keyboard) 449 | cast(JobQueue, context.job_queue).run_once( 450 | say_potato_job, 451 | time_limit * 60, 452 | data=( 453 | user.id, 454 | potato_message, 455 | message.from_user, 456 | ), 457 | name=f"POTATO {user.id}", 458 | ) 459 | 460 | 461 | async def buy(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 462 | """Tells people to not do job offers in our group""" 463 | message = cast(Message, update.effective_message) 464 | who_banned = cast(User, message.from_user) 465 | chat = cast(Chat, update.effective_chat) 466 | 467 | if not message.reply_to_message: 468 | await try_to_delete(message) 469 | return 470 | 471 | user = cast(User, message.reply_to_message.from_user) 472 | await message.reply_to_message.reply_text(BUY_TEXT.format(user.mention_html())) 473 | 474 | if await admin_check(cast(Dict, context.chat_data), chat, who_banned): 475 | await try_to_delete(message.reply_to_message) 476 | 477 | await try_to_delete(message) 478 | 479 | 480 | async def _token_warning( 481 | message: Message, context: ContextTypes.DEFAULT_TYPE, middle_text: str = "" 482 | ) -> None: 483 | """Warn people when they share their bot's token, and tell them to revoke it""" 484 | # Update timestamp on chat_data, and get "x time since last time" text 485 | last_time = update_shared_token_timestamp(message, context) 486 | 487 | # Send the message 488 | await message.reply_text( 489 | f"{TOKEN_TEXT}{middle_text}Days since last token was shared: {last_time}" 490 | ) 491 | 492 | 493 | async def regex_token_warning(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 494 | """Check the potential-token substrings in a message (matches with a Regex) 495 | If any is valid: 496 | - Warn the user about it, by sending the exposed bot(s) name(s) 497 | - Point them to @BotFather to revoke them 498 | """ 499 | matches = cast(List[str], context.matches) 500 | 501 | bots = [] 502 | for match in matches: 503 | bot = await get_bot_from_token(match) 504 | if bot is not None: 505 | bots.append(f"@{bot.username}") 506 | 507 | # Limit to 10 bots as checking if the token is valid takes time 508 | if len(bots) == 10: 509 | break 510 | 511 | if bots: 512 | bots_set = set(bots) # Use a set to not duplicate names 513 | many = len(bots_set) > 1 514 | await _token_warning( 515 | cast(Message, update.effective_message), 516 | context, 517 | f"Token{'s' if many else ''} exposed: {', '.join(bots_set)}\n\n", 518 | ) 519 | 520 | 521 | async def command_token_warning(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 522 | """When user shares a token by any way which is not the message's content: 523 | Reply their message with `/token` to warn them about it 524 | """ 525 | message = cast(Message, update.effective_message) 526 | await try_to_delete(message) 527 | 528 | if message.reply_to_message: 529 | await _token_warning(message.reply_to_message, context) 530 | 531 | 532 | async def compat_warning(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: 533 | """When someone posts an error message indicative of a compatibility issue: 534 | Reply with the /compat taghint 535 | """ 536 | message = cast(Message, update.effective_message) 537 | 538 | # Get the compat hint 539 | hint = TAG_HINTS["compat"] 540 | 541 | await message.reply_text( 542 | hint.html_markup(), 543 | reply_markup=hint.inline_keyboard, 544 | ) 545 | 546 | 547 | async def long_code_handling(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 548 | """When someone posts a long code snippet: 549 | Reply with the /pastebin taghint. 550 | Because we do the regexing in here rather than in the filter, the corresponding handler 551 | will have to be in a lower group. 552 | """ 553 | message = cast(Message, update.effective_message) 554 | text = cast(str, message.text) 555 | has_long_code = False 556 | 557 | # We make some educated guesses about the message's content. This is nothing more than 558 | # a few simple heuristics, but it should catch the most common cases. 559 | # If we have a code block longer than 15 lines, we assume it's a long code snippet 560 | parsed_entities = message.parse_entities(types=[MessageEntity.CODE, MessageEntity.PRE]) 561 | if any(len(text.split("\n")) >= 15 for text in parsed_entities.values()): 562 | has_long_code = True 563 | 564 | # if the text contains more than 5 import lines, we assume it's a long code snippet 565 | # regex from https://stackoverflow.com/a/44988666/10606962 566 | pattern = re.compile(r"(?m)^(?:from +(\S+) +)?import +(\S+)(?: +as +\S+)? *$") 567 | if not has_long_code and len(pattern.findall(text)) >= 5: 568 | has_long_code = True 569 | 570 | # if the text contains more than 3 class or function definitions, ... 571 | pattern = re.compile(r"(class|def) [a-zA-Z]+[a-zA-Z0-9_]*\(") 572 | if not has_long_code and len(pattern.findall(text)) >= 3: 573 | has_long_code = True 574 | 575 | if not has_long_code: 576 | return 577 | 578 | # Get the long_code hint 579 | hint = TAG_HINTS["pastebin"] 580 | 581 | # the leading ". " is important here since html_markup() splits on whitespaces! 582 | mention = f". {update.effective_user.mention_html()}" if update.effective_user else None 583 | 584 | text = ( 585 | f"Hi {hint.html_markup(mention)}, we like to keep our groups readable and thus" 586 | f" require long code to be in a pastebin. \n\n⚠️ Your message will be deleted in 1 minute." 587 | ) 588 | 589 | # check if pastebin was setup 590 | if pastebin_client := context.bot_data.get("pastebin_client"): 591 | # if there are code formatted snippets we only move those 592 | if parsed_entities: 593 | content = "\n\n".join(parsed_entities.values()) 594 | beginning = "⚠️ The code snippet(s) in your message have" 595 | else: 596 | content = text 597 | beginning = "⚠️ Your message has" 598 | r = await pastebin_client.post(const.PASTEBIN_URL, content=content) 599 | # if the request was successful we put the link in the message 600 | if r.status_code == codes.OK: 601 | text = ( 602 | f"Hi {hint.html_markup(mention)}, we like to keep our groups readable and thus " 603 | f"require long code to be in a pastebin. \n\n{beginning} been moved to " 604 | f"{const.PASTEBIN_URL}{r.text}.py. Your original message will be deleted in a " 605 | f"minute, please reply to this message with your query." 606 | ) 607 | else: 608 | logging.info( 609 | "The pastebin request failed with the status code %s and the text %s. The " 610 | "triggering update: %s", 611 | r.status_code, 612 | r.text, 613 | html.escape(json.dumps(update.to_dict(), indent=2, ensure_ascii=False)), 614 | exc_info=True, 615 | ) 616 | 617 | await message.reply_text( 618 | text, 619 | reply_markup=hint.inline_keyboard, 620 | ) 621 | 622 | async def job_callback(_: ContextTypes.DEFAULT_TYPE) -> None: 623 | await try_to_delete(message) 624 | 625 | context.job_queue.run_once( # type: ignore[union-attr] 626 | callback=job_callback, 627 | when=60, 628 | ) 629 | 630 | # We don't want this message to be processed any further 631 | raise ApplicationHandlerStop 632 | 633 | 634 | async def privacy(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: 635 | """Reply with the privacy policy""" 636 | message = cast(Message, update.effective_message) 637 | await message.reply_text( 638 | f"Please read my privacy policy in here.", quote=False 639 | ) 640 | if message.chat.type != message.chat.PRIVATE: 641 | await try_to_delete(message) 642 | -------------------------------------------------------------------------------- /components/const.py: -------------------------------------------------------------------------------- 1 | import re 2 | from urllib.parse import urljoin 3 | 4 | ARROW_CHARACTER = "➜" 5 | GITHUB_URL = "https://github.com/" 6 | DEFAULT_REPO_OWNER = "python-telegram-bot" 7 | DEFAULT_REPO_NAME = "python-telegram-bot" 8 | PTBCONTRIB_REPO_NAME = "ptbcontrib" 9 | DEFAULT_REPO = f"{DEFAULT_REPO_OWNER}/{DEFAULT_REPO_NAME}" 10 | # Require x non-command messages between each /rules etc. 11 | RATE_LIMIT_SPACING = 2 12 | # Welcome new chat members at most ever X minutes 13 | NEW_CHAT_MEMBERS_LIMIT_SPACING = 60 14 | USER_AGENT = "Github: python-telegram-bot/rules-bot" 15 | DEFAULT_HEADERS = {"User-Agent": USER_AGENT} 16 | ENCLOSING_REPLACEMENT_CHARACTER = "+" 17 | _ERC = ENCLOSING_REPLACEMENT_CHARACTER 18 | ENCLOSED_REGEX = re.compile(rf"\{_ERC}([^{_ERC}]*)\{_ERC}") 19 | OFFTOPIC_USERNAME = "pythontelegrambottalk" 20 | ONTOPIC_USERNAME = "pythontelegrambotgroup" 21 | DEV_GROUP_USERNAME = "pythontelegrambotdev" 22 | CHANNEL_USERNAME = "pythontelegrambotchannel" 23 | CHANNEL_CHAT_ID = "@" + CHANNEL_USERNAME 24 | OFFTOPIC_CHAT_ID = "@" + OFFTOPIC_USERNAME 25 | ONTOPIC_CHAT_ID = "@" + ONTOPIC_USERNAME 26 | ERROR_CHANNEL_CHAT_ID = -1001397960657 27 | TELEGRAM_SUPERSCRIPT = "ᵀᴱᴸᴱᴳᴿᴬᴹ" 28 | FAQ_CHANNEL_ID = "@ptbfaq" 29 | SELF_BOT_NAME = "roolsbot" 30 | PTBCONTRIB_LINK = "https://github.com/python-telegram-bot/ptbcontrib/" 31 | DOCS_URL = "https://docs.python-telegram-bot.org/" 32 | OFFICIAL_URL = "https://core.telegram.org/bots/api" 33 | PROJECT_URL = urljoin(GITHUB_URL, DEFAULT_REPO + "/") 34 | PASTEBIN_URL = "https://pastebin.poolitzer.eu" 35 | WIKI_URL = urljoin(PROJECT_URL, "wiki/") 36 | WIKI_CODE_SNIPPETS_URL = urljoin(WIKI_URL, "Code-snippets") 37 | WIKI_FAQ_URL = urljoin(WIKI_URL, "Frequently-Asked-Questions") 38 | WIKI_FRDP_URL = urljoin(WIKI_URL, "Frequently-requested-design-patterns") 39 | EXAMPLES_URL = urljoin(DOCS_URL, "/examples.html") 40 | ALLOWED_USERNAMES = (OFFTOPIC_USERNAME, ONTOPIC_USERNAME, DEV_GROUP_USERNAME, CHANNEL_USERNAME) 41 | ALLOWED_CHAT_IDS = ( 42 | ERROR_CHANNEL_CHAT_ID, 43 | -1001494805131, # dev chat 44 | -1001101839433, # Church 45 | ) 46 | ONTOPIC_RULES = f"""This group is for questions, answers and discussions around the \ 47 | python-telegram-bot library and, to some extent, \ 48 | Telegram bots in general. 49 | 50 | Rules: 51 | - The group language is English 52 | - Stay on topic 53 | - Advertisement (including coding job offers) or posting as channels is disallowed 54 | - No meta questions (eg. "Can I ask something?") 55 | - Use a pastebin when you have a question about your code, like this one. If you really can't explain your problem without showing a picture, upload\ 57 | it somewhere and share a link. 58 | - Use /wiki and /docs in a private chat if possible. 59 | - Only mention or reply to users directly if you're answering their question or following up on a \ 60 | conversation with them. 61 | - Please abide by our Code of Conduct 62 | - Use @admin to report spam or abuse and only for that. 63 | - If you have a userbot, deactivate it in here. Otherwise you'll get banned at least temporarily. 64 | - Don't copy-paste answers that were generated by an AI/LLM tool (e.g. ChatGPT). 65 | - If you have asked your question elsewhere before (e.g. StackOverflow or PTBs GitHub repo), 66 | include a link to it in your question. 67 | 68 | Before asking, please take a look at our wiki and \ 69 | example bots or, depending on your question, the \ 70 | official API docs and \ 71 | python-telegram-bot docs). Please also make sure to read the \ 72 | wiki page on how to ask good questions. 73 | For off-topic discussions, please use our \ 74 | off-topic group. 75 | """ 76 | 77 | OFFTOPIC_RULES = f"""Topics: 78 | - Discussions about Python in general 79 | - Meta discussions about python-telegram-bot 80 | - Friendly, respectful talking about non-tech topics 81 | 82 | Rules: 83 | - The group language is English 84 | - Advertisement (including coding job offers) or posting as channels is disallowed 85 | - Use a pastebin to share code. If you really can't explain your problem without showing a\ 86 | picture, upload it somewhere and share a link. 87 | - No shitposting, flamewars or excessive trolling 88 | - Max. 1 meme per user per day 89 | - Only mention or reply to users directly if you're answering their question or following up on a \ 90 | conversation with them. 91 | - Please abide by our Code of Conduct 92 | - Use @admin to report spam or abuse and only for that. 93 | - If you have a userbot, deactivate it in here. Otherwise you'll get banned at least temporarily. 94 | - Don't copy-paste answers that were generated by an AI/LLM tool (e.g. ChatGPT). 95 | - If you have asked your question elsewhere before (e.g. StackOverflow or PTBs GitHub repo), 96 | include a link to it in your question. 97 | """ 98 | 99 | # Github Pattern 100 | # This matches two different kinds of things: 101 | # 1. ptbcontrib/description 102 | # 2. owner/repo(#|GH-|PR-|@)number/query, where both owner/ and repo are optional 103 | # 104 | # Per https://github.com/join 105 | # Github username may only contain alphanumeric characters or hyphens. 106 | # Github username cannot have multiple consecutive hyphens. 107 | # Github username cannot begin or end with a hyphen. 108 | # Maximum is 39 characters. 109 | # Therefore we use: 110 | # [a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38} 111 | # 112 | # Repo names seem to allow alphanumeric, -, . and _ 113 | # And the form at https://github.com/new has a maxlength of 100 114 | # Therefore we use 115 | # [A-Za-z0-9-._]{0,100} 116 | 117 | GITHUB_PATTERN = re.compile( 118 | r""" 119 | (?i) # Case insensitivity 120 | [\s\S]*? # Any characters 121 | (?P # Capture for the the whole thing 122 | (?: # Optional non-capture group for owner/repo#number/sha/query matches 123 | (?: # Optional non-capture group for username/repo 124 | # Matches username or org - only if ends with slash 125 | (?:(?P[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38})/)? 126 | (?P[A-Za-z0-9-._]{0,100})? # Optionally matches repo 127 | )? # End optional non-capture group 128 | (?: # Match either 129 | ( 130 | # "#" or "GH-" or "PR-" or "/issues/" or "/pull" 131 | (?P\#|GH-|PR-|/issues/|/pull/) 132 | (?: # Followed by either 133 | (?P\d+) # Numbers 134 | | # Or 135 | (?P.+) # A search query (only works inline) 136 | ) 137 | ) 138 | | # Or 139 | (?:(/commit/|@) # /commit/ or @ 140 | (?P[0-9a-f]{7,40})) # sha: 7-40 hexadecimal chars 141 | ) 142 | ) 143 | | # Or ptbcontrib match 144 | ptbcontrib/(?P[\w_]+) 145 | ) 146 | """, 147 | re.VERBOSE, 148 | ) 149 | VEGETABLES = [ 150 | "amaranth", 151 | "anise", 152 | "artichoke", 153 | "arugula", 154 | "asparagus", 155 | "aubergine", 156 | "basil", 157 | "beet", 158 | "broccoflower", 159 | "broccoli", 160 | "cabbage", 161 | "calabrese", 162 | "caraway", 163 | "carrot", 164 | "cauliflower", 165 | "celeriac", 166 | "celery", 167 | "chamomile", 168 | "chard", 169 | "chayote", 170 | "chickpea", 171 | "chives", 172 | "cilantro", 173 | "corn", 174 | "corn salad", 175 | "courgette", 176 | "cucumber", 177 | "daikon", 178 | "delicata", 179 | "dill", 180 | "eggplant", 181 | "endive", 182 | "fennel", 183 | "fiddlehead", 184 | "frisee", 185 | "garlic", 186 | "ginger", 187 | "habanero", 188 | "horseradish", 189 | "jalapeno", 190 | "jicama", 191 | "kale", 192 | "kohlrabi", 193 | "lavender", 194 | "leek ", 195 | "legume", 196 | "lentils", 197 | "lettuce", 198 | "mamey", 199 | "mangetout", 200 | "marjoram", 201 | "mushroom", 202 | "nopale", 203 | "okra", 204 | "onion", 205 | "oregano", 206 | "paprika", 207 | "parsley", 208 | "parsnip", 209 | "pea", 210 | "potato", 211 | "pumpkin", 212 | "radicchio", 213 | "radish", 214 | "rhubarb", 215 | "rosemary", 216 | "rutabaga", 217 | "sage", 218 | "scallion", 219 | "shallot", 220 | "skirret", 221 | "spinach", 222 | "squash", 223 | "taro", 224 | "thyme", 225 | "topinambur", 226 | "tubers", 227 | "turnip", 228 | "wasabi", 229 | "watercress", 230 | "yam", 231 | "zucchini", 232 | ] 233 | BUY_TEXT = ( 234 | "Hello there, {0}. You are writing your job offer in our technical focused groups, which is " 235 | "against our rules. To find a bot developer, please look at agencies dedicated towards " 236 | "freelancers. An example of those would be https://fiverr.com, which we are not " 237 | "associated with." 238 | ) 239 | TOKEN_TEXT = "⚠️ You posted a token, go revoke it with @BotFather.\n\n" 240 | 241 | COMPAT_ERRORS = re.compile( 242 | r""" 243 | ( 244 | (Updater\._{0,2}init_{0,2}\(\))? 245 | ( 246 | \ got\ an\ unexpected\ keyword\ argument 247 | \ ['"]*(use_context|token|use_controls|dispatcher)['"]* 248 | | 249 | \ missing\ 1\ required\ positional\ argument:\ ['"]*update_queue['"]* 250 | ) 251 | )|( 252 | updater\.(idle\(\)|dispatcher) 253 | )|( 254 | dispatcher.add_handler\( 255 | )|( 256 | cannot\ import\ name\ ['"]*Filters['"]* 257 | ) 258 | """, 259 | flags=re.VERBOSE, 260 | ) 261 | 262 | PRIVACY_POLICY = "https://github.com/python-telegram-bot/rules-bot/wiki/Privacy-Policy" 263 | 264 | SHORT_DESCRIPTION = ( 265 | "Helper bot of the python-telegram-bot groups | Help and source at " 266 | "https://github.com/python-telegram-bot/rules-bot" 267 | ) 268 | DESCRIPTION = f""" 269 | Helper bot of the https://python-telegram-bot.org community groups: 270 | {ONTOPIC_CHAT_ID} and {OFFTOPIC_CHAT_ID}. 271 | 272 | The privacy policy of this bot can be found at {PRIVACY_POLICY}. 273 | 274 | Usage instructions and source code can be found at 275 | https://github.com/python-telegram-bot/rules-bot. 276 | """ 277 | -------------------------------------------------------------------------------- /components/entrytypes.py: -------------------------------------------------------------------------------- 1 | import re 2 | from abc import ABC, abstractmethod 3 | from dataclasses import dataclass 4 | from typing import ClassVar, List, Optional 5 | from urllib.parse import urljoin 6 | 7 | from telegram import InlineKeyboardMarkup 8 | from thefuzz import fuzz 9 | 10 | from components.const import ( 11 | ARROW_CHARACTER, 12 | DEFAULT_REPO_NAME, 13 | DEFAULT_REPO_OWNER, 14 | DOCS_URL, 15 | TELEGRAM_SUPERSCRIPT, 16 | ) 17 | 18 | 19 | class BaseEntry(ABC): 20 | """Base class for all searchable entries.""" 21 | 22 | @property 23 | @abstractmethod 24 | def display_name(self) -> str: 25 | """Name to display in the search results""" 26 | 27 | @property 28 | def short_name(self) -> str: 29 | """Potentially shorter name to display. Defaults to :attr:`display_name`""" 30 | return self.display_name 31 | 32 | @property 33 | @abstractmethod 34 | def description(self) -> str: 35 | """Description of the entry to display in the search results""" 36 | 37 | @property 38 | def short_description(self) -> str: 39 | """Short description of the entry to display in the search results. Useful when displaying 40 | multiple search results in one entry. Defaults to :attr:`short_name` if not overridden.""" 41 | return self.short_name 42 | 43 | @abstractmethod 44 | def html_markup(self, search_query: str = None) -> str: 45 | """HTML markup to be used if this entry is selected in the search. May depend on the search 46 | query.""" 47 | 48 | @abstractmethod 49 | def html_insertion_markup(self, search_query: str = None) -> str: 50 | """HTML markup to be used for insertion search. May depend on the search query.""" 51 | 52 | def html_reply_markup(self, search_query: str = None) -> str: 53 | """HTML markup to be used for reply search. May depend on the search query. 54 | Defaults to :meth:`html_insertion_markup`, but may be overridden. 55 | """ 56 | return self.html_insertion_markup(search_query=search_query) 57 | 58 | @abstractmethod 59 | def compare_to_query(self, search_query: str) -> float: 60 | """Gives a number ∈[0,100] describing how similar the search query is to this entry.""" 61 | 62 | @property 63 | def inline_keyboard(self) -> Optional[InlineKeyboardMarkup]: 64 | """Inline Keyboard markup that can be attached to this entry. Returns :obj:`None`, if 65 | not overridden.""" 66 | return None 67 | 68 | 69 | class ReadmeSection(BaseEntry): 70 | """A section of the readme. 71 | 72 | Args: 73 | name: The name of the section 74 | anchor: the URL anchor of the section 75 | """ 76 | 77 | def __init__(self, name: str, anchor: str): 78 | self.name = name 79 | self.anchor = anchor 80 | 81 | @property 82 | def url(self) -> str: 83 | return urljoin(DOCS_URL, self.anchor) 84 | 85 | @property 86 | def display_name(self) -> str: 87 | return f"Readme {ARROW_CHARACTER} {self.name}" 88 | 89 | @property 90 | def short_name(self) -> str: 91 | return self.name 92 | 93 | @property 94 | def description(self) -> str: 95 | return "Readme of python-telegram-bot" 96 | 97 | def html_markup(self, search_query: str = None) -> str: 98 | return ( 99 | f"Readme of python-telegram-bot\n" f"{self.html_insertion_markup(search_query)}" 100 | ) 101 | 102 | def html_insertion_markup(self, search_query: str = None) -> str: 103 | return f'{self.short_name}' 104 | 105 | def html_reply_markup(self, search_query: str = None) -> str: 106 | return f'Readme Section: {self.short_name}' 107 | 108 | def compare_to_query(self, search_query: str) -> float: 109 | return fuzz.token_set_ratio(f"readme {self.name}", search_query) 110 | 111 | 112 | class Example(BaseEntry): 113 | """An example in the examples directory. 114 | 115 | Args: 116 | name: The name of the example 117 | """ 118 | 119 | def __init__(self, name: str): 120 | self._name = name 121 | self._search_name = f"example {self._name}" 122 | 123 | if name.endswith(".py"): 124 | href = name[:-3] 125 | else: 126 | href = name 127 | self.url = f"{DOCS_URL}examples.html#examples-{href}" 128 | 129 | @property 130 | def display_name(self) -> str: 131 | return f"Examples {ARROW_CHARACTER} {self._name}" 132 | 133 | @property 134 | def short_name(self) -> str: 135 | return self._name 136 | 137 | @property 138 | def description(self) -> str: 139 | return "Examples directory of python-telegram-bot" 140 | 141 | def html_markup(self, search_query: str = None) -> str: 142 | return ( 143 | "Examples directory of python-telegram-bot:" 144 | f"\n{self.html_insertion_markup(search_query)}" 145 | ) 146 | 147 | def html_insertion_markup(self, search_query: str = None) -> str: 148 | return f'{self.short_name}' 149 | 150 | def compare_to_query(self, search_query: str) -> float: 151 | if search_query.endswith(".py"): 152 | search_query = search_query[:-3] 153 | 154 | return fuzz.partial_token_set_ratio(self._search_name, search_query) 155 | 156 | 157 | class WikiPage(BaseEntry): 158 | """A wiki page. 159 | 160 | Args: 161 | category: The .py of the page, as listed in the sidebar 162 | name: The name of the page 163 | url: URL of the page 164 | """ 165 | 166 | def __init__(self, category: str, name: str, url: str): 167 | self.category = category 168 | self.name = name 169 | self.url = url 170 | self._compare_name = f"{self.category} {self.name}" 171 | 172 | @property 173 | def display_name(self) -> str: 174 | return f"{self.category} {ARROW_CHARACTER} {self.name}" 175 | 176 | @property 177 | def short_name(self) -> str: 178 | return self.name 179 | 180 | @property 181 | def description(self) -> str: 182 | return "Wiki of python-telegram-bot" 183 | 184 | def html_markup(self, search_query: str = None) -> str: 185 | return ( 186 | f"Wiki of python-telegram-bot - Category {self.category}\n" 187 | f"{self.html_insertion_markup(search_query)}" 188 | ) 189 | 190 | def html_insertion_markup(self, search_query: str = None) -> str: 191 | return f'{self.short_name}' 192 | 193 | def html_reply_markup(self, search_query: str = None) -> str: 194 | return f'Wiki Category {self.category}: {self.short_name}' 195 | 196 | def compare_to_query(self, search_query: str) -> float: 197 | return fuzz.token_set_ratio(self._compare_name, search_query) 198 | 199 | 200 | class CodeSnippet(WikiPage): 201 | """A code snippet 202 | 203 | Args: 204 | name: The name of the snippet 205 | url: URL of the snippet 206 | """ 207 | 208 | def __init__(self, name: str, url: str): 209 | super().__init__(category="Code Snippets", name=name, url=url) 210 | 211 | 212 | class FAQEntry(WikiPage): 213 | """An FAQ entry 214 | 215 | Args: 216 | name: The name of the entry 217 | url: URL of the entry 218 | """ 219 | 220 | def __init__(self, name: str, url: str): 221 | super().__init__(category="FAQ", name=name, url=url) 222 | 223 | 224 | class FRDPEntry(WikiPage): 225 | """A frequently requested design pattern entry 226 | 227 | Args: 228 | name: The name of the entry 229 | url: URL of the entry 230 | """ 231 | 232 | def __init__(self, name: str, url: str): 233 | super().__init__(category="Design Pattern", name=name, url=url) 234 | 235 | 236 | class DocEntry(BaseEntry): 237 | """An entry to the PTB docs. 238 | 239 | Args: 240 | url: URL to the online documentation of the entry. 241 | entry_type: Which type of entry this is. 242 | name: Name of the entry. 243 | display_name: Optional. Display name for the entry. 244 | telegram_name: Optional: Name of the corresponding Telegram documentation entry. 245 | telegram_url: Optional. Link to the corresponding Telegram documentation. 246 | """ 247 | 248 | def __init__( 249 | self, 250 | url: str, 251 | entry_type: str, 252 | name: str, 253 | display_name: str = None, 254 | telegram_name: str = None, 255 | telegram_url: str = None, 256 | ): 257 | self.url = url 258 | self.entry_type = entry_type 259 | self.effective_type = self.entry_type.split(":")[-1] 260 | self.name = name 261 | self._display_name = display_name 262 | self.telegram_url = telegram_url 263 | self.telegram_name = telegram_name 264 | self._parsed_name: List[str] = self.parse_search_query(self.name) 265 | 266 | @staticmethod 267 | def parse_search_query(search_query: str) -> List[str]: 268 | """ 269 | Does some preprocessing of the query needed for comparison with the entries in the docs. 270 | 271 | Args: 272 | search_query: The search query. 273 | 274 | Returns: 275 | The query, split on ``.``, ``-`` and ``/``, in reversed order. 276 | """ 277 | # reversed, so that 'class' matches the 'class' part of 'module.class' exactly instead of 278 | # not matching the 'module' part 279 | return list(reversed(re.split(r"\.|/|-", search_query.strip()))) 280 | 281 | @property 282 | def display_name(self) -> str: 283 | return self._display_name or self.name 284 | 285 | @property 286 | def short_name(self) -> str: 287 | name = self._display_name or self.name 288 | 289 | if name.startswith("telegram."): 290 | return name[len("telegram.") :] 291 | return name 292 | 293 | @property 294 | def description(self) -> str: 295 | return "Documentation of python-telegram-bot" 296 | 297 | def html_markup(self, search_query: str = None) -> str: 298 | base = ( 299 | f"{self.short_name}\n" 300 | f"python-telegram-bot documentation for this {self.effective_type}:\n" 301 | f"{self.html_markup_no_telegram}" 302 | ) 303 | if not self.telegram_url and not self.telegram_name: 304 | tg_text = "" 305 | else: 306 | tg_text = ( 307 | "\n\nTelegram's official Bot API documentation has more info about " 308 | f'{self.telegram_name}.' 309 | ) 310 | return base + tg_text 311 | 312 | @property 313 | def html_markup_no_telegram(self) -> str: 314 | return f'{self.name}' 315 | 316 | def html_insertion_markup(self, search_query: str = None) -> str: 317 | if not self.telegram_name and not self.telegram_url: 318 | return self.html_markup_no_telegram 319 | return ( 320 | f'{self.html_markup_no_telegram} ' 321 | f"{TELEGRAM_SUPERSCRIPT}" 322 | ) 323 | 324 | def compare_to_query(self, search_query: str) -> float: 325 | score = 0.0 326 | processed_query = self.parse_search_query(search_query) 327 | 328 | # We compare all the single parts of the query … 329 | for target, value in zip(processed_query, self._parsed_name): 330 | score += fuzz.ratio(target, value) 331 | # ... and the full name because we're generous 332 | score += fuzz.ratio(search_query, self.name) 333 | # To stay <= 100 as not to overrule other results 334 | score = score / 2 335 | 336 | # IISC std: is the domain for general stuff like headlines and chapters. 337 | # we'll wanna give those a little less weight 338 | if self.entry_type.startswith("std:"): 339 | score *= 0.8 340 | return score 341 | 342 | 343 | class ParamDocEntry(DocEntry): 344 | """An entry to the PTB docs. Special case of a parameter of a function or method. 345 | 346 | Args: 347 | url: URL to the online documentation of the entry. 348 | entry_type: Which type of entry this is. 349 | name: Name of the entry. 350 | display_name: Optional. Display name for the entry. 351 | telegram_name: Optional: Name of the corresponding Telegram documentation entry. 352 | telegram_url: Optional. Link to the corresponding Telegram documentation. 353 | """ 354 | 355 | def __init__( 356 | self, 357 | url: str, 358 | entry_type: str, 359 | name: str, 360 | display_name: str = None, 361 | telegram_name: str = None, 362 | telegram_url: str = None, 363 | ): 364 | if ".params." not in name: 365 | raise ValueError("The passed name doesn't match a parameter name.") 366 | 367 | base_name, parameter_name = name.split(".params.") 368 | self._base_name = base_name 369 | self._parameter_name = parameter_name 370 | super().__init__( 371 | url=url, 372 | entry_type=entry_type, 373 | name=name, 374 | display_name=f"Parameter {self._parameter_name} of {self._base_name}", 375 | telegram_name=telegram_name, 376 | telegram_url=telegram_url, 377 | ) 378 | self._base_url = self.url.split(".params.")[0] 379 | self._parsed_name_wo_params = self.parse_search_query(self.name.replace(".params.", "")) 380 | 381 | def html_markup(self, search_query: str = None) -> str: 382 | base = ( 383 | f"{self._base_name}(..., {self._parameter_name}=...)\n" 384 | f"python-telegram-bot documentation for this {self.effective_type} " 385 | f'of {self._base_name}:\n' 386 | f"{self.html_markup_no_telegram}" 387 | ) 388 | if not self.telegram_url and not self.telegram_name: 389 | tg_text = "" 390 | else: 391 | tg_text = ( 392 | "\n\nTelegram's official Bot API documentation has more info about " 393 | f'{self.telegram_name}.' 394 | ) 395 | return base + tg_text 396 | 397 | @property 398 | def html_markup_no_telegram(self) -> str: 399 | return f'{self._parameter_name}' 400 | 401 | def html_insertion_markup(self, search_query: str = None) -> str: 402 | base_markup = ( 403 | f'Parameter {self._parameter_name} of ' 404 | f'{self._base_name}' 405 | ) 406 | if not self.telegram_name and not self.telegram_url: 407 | return base_markup 408 | return f'{base_markup} ' f"{TELEGRAM_SUPERSCRIPT}" 409 | 410 | def compare_to_query(self, search_query: str) -> float: 411 | score = 0.0 412 | processed_query = self.parse_search_query(search_query) 413 | 414 | # We compare all the single parts of the query, with & without the ".params." 415 | for target, value in zip(processed_query, self._parsed_name): 416 | score += fuzz.ratio(target, value) 417 | for target, value in zip(processed_query, self._parsed_name_wo_params): 418 | score += fuzz.ratio(target, value) 419 | # ... and the full name because we're generous with & without leading "parameter" 420 | score += fuzz.ratio(search_query, self.name) 421 | score += fuzz.ratio(search_query, f"parameter {self.name}") 422 | 423 | # To stay <= 100 as not to overrule other results 424 | return score / 4 425 | 426 | 427 | @dataclass 428 | class Commit(BaseEntry): 429 | """A commit on Github 430 | 431 | Args: 432 | owner: str 433 | repo: str 434 | sha: str 435 | url: str 436 | title: str 437 | author: str 438 | """ 439 | 440 | owner: str 441 | repo: str 442 | sha: str 443 | url: str 444 | title: str 445 | author: str 446 | 447 | @property 448 | def short_sha(self) -> str: 449 | return self.sha[:7] 450 | 451 | @property 452 | def short_name(self) -> str: 453 | return ( 454 | f'{"" if self.owner == DEFAULT_REPO_OWNER else self.owner + "/"}' 455 | f'{"" if self.repo == DEFAULT_REPO_NAME else self.repo}' 456 | f"@{self.short_sha}" 457 | ) 458 | 459 | @property 460 | def display_name(self) -> str: 461 | return f"Commit {self.short_name}: {self.title} by {self.author}" 462 | 463 | @property 464 | def description(self) -> str: 465 | return "Search on GitHub" 466 | 467 | def html_markup(self, search_query: str = None) -> str: 468 | return f'{self.display_name}' 469 | 470 | def html_insertion_markup(self, search_query: str = None) -> str: 471 | return f'{self.short_name}' 472 | 473 | def html_reply_markup(self, search_query: str = None) -> str: 474 | return self.html_markup(search_query=search_query) 475 | 476 | def compare_to_query(self, search_query: str) -> float: 477 | search_query = search_query.lstrip("@ ") 478 | if self.sha.startswith(search_query): 479 | return 100 480 | return 0 481 | 482 | 483 | @dataclass 484 | class _IssueOrPullRequestOrDiscussion(BaseEntry): 485 | _TYPE: ClassVar[str] = "" 486 | owner: str 487 | repo: str 488 | number: int 489 | title: str 490 | url: str 491 | author: Optional[str] 492 | 493 | @property 494 | def short_name(self) -> str: 495 | return ( 496 | f'{"" if self.owner == DEFAULT_REPO_OWNER else self.owner + "/"}' 497 | f'{"" if self.repo == DEFAULT_REPO_NAME else self.repo}' 498 | f"#{self.number}" 499 | ) 500 | 501 | @property 502 | def display_name(self) -> str: 503 | if self.author: 504 | return f"{self._TYPE} {self.short_name}: {self.title} by {self.author}" 505 | return f"{self._TYPE} {self.short_name}: {self.title}" 506 | 507 | @property 508 | def description(self) -> str: 509 | return "Search on GitHub" 510 | 511 | @property 512 | def short_description(self) -> str: 513 | # Needs to be here because of cyclical imports 514 | from .util import truncate_str # pylint:disable=import-outside-toplevel 515 | 516 | string = f"{self._TYPE} {self.short_name}: {self.title}" 517 | return truncate_str(string, 50) 518 | 519 | def html_markup(self, search_query: str = None) -> str: # pylint:disable=unused-argument 520 | return f'{self.display_name}' 521 | 522 | # pylint:disable=unused-argument 523 | def html_insertion_markup(self, search_query: str = None) -> str: 524 | return f'{self.short_name}' 525 | 526 | def html_reply_markup(self, search_query: str = None) -> str: 527 | return self.html_markup(search_query=search_query) 528 | 529 | def compare_to_query(self, search_query: str) -> float: 530 | search_query = search_query.lstrip("# ") 531 | if str(self.number) == search_query: 532 | return 100 533 | return fuzz.token_set_ratio(self.title, search_query) 534 | 535 | 536 | @dataclass 537 | class Issue(_IssueOrPullRequestOrDiscussion): 538 | """An issue on GitHub 539 | 540 | Args: 541 | number: the number 542 | repo: the repo name 543 | owner: the owner name 544 | url: the url of the issue 545 | title: title of the issue 546 | """ 547 | 548 | _TYPE: ClassVar[str] = "Issue" 549 | 550 | 551 | @dataclass 552 | class PullRequest(_IssueOrPullRequestOrDiscussion): 553 | """An pullRequest on GitHub 554 | 555 | Args: 556 | number: the number 557 | repo: the repo name 558 | owner: the owner name 559 | url: the url of the pull request 560 | title: title of the pull request 561 | """ 562 | 563 | _TYPE: ClassVar[str] = "PullRequest" 564 | 565 | 566 | @dataclass 567 | class Discussion(_IssueOrPullRequestOrDiscussion): 568 | """A Discussion on GitHub 569 | 570 | Args: 571 | number: the number 572 | repo: the repo name 573 | owner: the owner name 574 | url: the url of the pull request 575 | title: title of the pull request 576 | """ 577 | 578 | _TYPE: ClassVar[str] = "Discussion" 579 | 580 | 581 | class PTBContrib(BaseEntry): 582 | """A contribution of ptbcontrib 583 | 584 | Args: 585 | name: The name of the contribution 586 | url: The url to the contribution 587 | """ 588 | 589 | def __init__(self, name: str, url: str): 590 | self.name = name 591 | self.url = url 592 | 593 | @property 594 | def display_name(self) -> str: 595 | return f"ptbcontrib/{self.name}" 596 | 597 | @property 598 | def description(self) -> str: 599 | return "Community base extensions for python-telegram-bot" 600 | 601 | def html_markup(self, search_query: str = None) -> str: 602 | return f'{self.display_name}' 603 | 604 | def html_insertion_markup(self, search_query: str = None) -> str: 605 | return self.html_markup(search_query) 606 | 607 | def compare_to_query(self, search_query: str) -> float: 608 | # Here we just assume that everything before thi first / is ptbcontrib 609 | # (modulo typos). That could be wrong, but then it's the users fault :) 610 | search_query = search_query.split("/", maxsplit=1)[-1] 611 | return fuzz.ratio(self.name, search_query) 612 | 613 | 614 | class TagHint(BaseEntry): 615 | """A tag hint for frequently used texts in the groups. 616 | 617 | Attributes: 618 | tag: The tag of this hint. 619 | message: The message to display in HTML layout. It may contain a ``{query}`` part, which 620 | will be filled appropriately. 621 | description: Description of the tag hint. 622 | default_query: Optional. Inserted into the ``message`` if no other query is provided. 623 | inline_keyboard: Optional. In InlineKeyboardMarkup to attach to the hint. 624 | group_command: Optional. Whether this tag hint should be listed as command in the groups. 625 | """ 626 | 627 | def __init__( 628 | self, 629 | tag: str, 630 | message: str, 631 | description: str, 632 | default_query: str = None, 633 | inline_keyboard: InlineKeyboardMarkup = None, 634 | group_command: bool = False, 635 | ): 636 | self.tag = tag 637 | self._message = message 638 | self._default_query = default_query 639 | self._description = description 640 | self._inline_keyboard = inline_keyboard 641 | self.group_command = group_command 642 | 643 | @property 644 | def display_name(self) -> str: 645 | return f"Tag hint: {self.short_name}" 646 | 647 | @property 648 | def short_name(self) -> str: 649 | return f"/{self.tag}" 650 | 651 | @property 652 | def description(self) -> str: 653 | return self._description 654 | 655 | def html_markup(self, search_query: str = None) -> str: 656 | parts = search_query.split(maxsplit=1) if search_query else [] 657 | insert = parts[1] if len(parts) > 1 else None 658 | return self._message.format(query=insert or self._default_query) 659 | 660 | def html_insertion_markup(self, search_query: str = None) -> str: 661 | return self.html_markup(search_query=search_query) 662 | 663 | def compare_to_query(self, search_query: str) -> float: 664 | parts = search_query.lstrip("/").split(maxsplit=1) 665 | if parts: 666 | return fuzz.ratio(self.tag, parts[0]) 667 | return 0 668 | 669 | @property 670 | def inline_keyboard(self) -> Optional[InlineKeyboardMarkup]: 671 | return self._inline_keyboard 672 | -------------------------------------------------------------------------------- /components/errorhandler.py: -------------------------------------------------------------------------------- 1 | import html 2 | import json 3 | import logging 4 | import traceback 5 | from typing import cast 6 | 7 | from telegram import Update 8 | from telegram.error import BadRequest 9 | from telegram.ext import CallbackContext 10 | 11 | from components.const import ERROR_CHANNEL_CHAT_ID 12 | 13 | logger = logging.getLogger(__name__) 14 | 15 | 16 | async def error_handler(update: object, context: CallbackContext) -> None: 17 | """Log the error and send a telegram message to notify the developer.""" 18 | # Log the error before we do anything else, so we can see it even if something breaks. 19 | logger.error(msg="Exception while handling an update:", exc_info=context.error) 20 | 21 | # traceback.format_exception returns the usual python message about an exception, but as a 22 | # list of strings rather than a single string, so we have to join them together. 23 | tb_list = traceback.format_exception( 24 | None, context.error, cast(Exception, context.error).__traceback__ 25 | ) 26 | tb_string = "".join(tb_list) 27 | 28 | # Build the message with some markup and additional information about what happened. 29 | # You might need to add some logic to deal with messages longer than the 4096 character limit. 30 | update_str = update.to_dict() if isinstance(update, Update) else str(update) 31 | message_1 = ( 32 | f"An exception was raised while handling an update\n\n" 33 | f"
update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}
" 34 | ) 35 | message_2 = f"
{html.escape(tb_string)}
" 36 | 37 | # Finally, send the messages 38 | # We send update and traceback in two parts to reduce the chance of hitting max length 39 | try: 40 | sent_message = await context.bot.send_message( 41 | chat_id=ERROR_CHANNEL_CHAT_ID, text=message_1 42 | ) 43 | await sent_message.reply_html(message_2) 44 | except BadRequest as exc: 45 | if "too long" in str(exc): 46 | message = ( 47 | f"Hey.\nThe error {html.escape(str(context.error))} happened." 48 | f" The traceback is too long to send, but it was written to the log." 49 | ) 50 | await context.bot.send_message(chat_id=ERROR_CHANNEL_CHAT_ID, text=message) 51 | else: 52 | raise exc 53 | -------------------------------------------------------------------------------- /components/github.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import logging 3 | from typing import Dict, Iterable, List, Optional, Union 4 | 5 | from graphql import GraphQLError 6 | 7 | from components.const import DEFAULT_REPO_NAME, DEFAULT_REPO_OWNER, USER_AGENT 8 | from components.entrytypes import Commit, Discussion, Example, Issue, PTBContrib, PullRequest 9 | from components.graphqlclient import GraphQLClient 10 | 11 | 12 | class GitHub: 13 | def __init__(self, auth: str, user_agent: str = USER_AGENT) -> None: 14 | self._gql_client = GraphQLClient(auth=auth, user_agent=user_agent) 15 | 16 | self._logger = logging.getLogger(self.__class__.__qualname__) 17 | 18 | self.__lock = asyncio.Lock() 19 | self.issues: Dict[int, Issue] = {} 20 | self.pull_requests: Dict[int, PullRequest] = {} 21 | self.discussions: Dict[int, Discussion] = {} 22 | self.issue_iterator: Optional[Iterable[Issue]] = None 23 | self.ptb_contribs: Dict[str, PTBContrib] = {} 24 | self.examples: Dict[str, Example] = {} 25 | 26 | async def initialize(self) -> None: 27 | await self._gql_client.initialize() 28 | 29 | async def shutdown(self) -> None: 30 | await self._gql_client.shutdown() 31 | 32 | @property 33 | def all_ptbcontribs(self) -> List[PTBContrib]: 34 | return list(self.ptb_contribs.values()) 35 | 36 | @property 37 | def all_issues(self) -> List[Issue]: 38 | return list(self.issues.values()) 39 | 40 | @property 41 | def all_pull_requests(self) -> List[PullRequest]: 42 | return list(self.pull_requests.values()) 43 | 44 | @property 45 | def all_discussions(self) -> List[Discussion]: 46 | return list(self.discussions.values()) 47 | 48 | @property 49 | def all_examples(self) -> List[Example]: 50 | return list(self.examples.values()) 51 | 52 | async def update_examples(self) -> None: 53 | self._logger.info("Getting examples") 54 | examples = await self._gql_client.get_examples() 55 | async with self.__lock: 56 | self.examples.clear() 57 | for example in examples: 58 | self.examples[example.short_name] = example 59 | 60 | async def update_ptb_contribs(self) -> None: 61 | self._logger.info("Getting ptbcontribs") 62 | ptb_contribs = await self._gql_client.get_ptb_contribs() 63 | async with self.__lock: 64 | self.ptb_contribs.clear() 65 | for ptb_contrib in ptb_contribs: 66 | self.ptb_contribs[ptb_contrib.short_name.split("/")[1]] = ptb_contrib 67 | 68 | async def update_issues(self, cursor: str = None) -> Optional[str]: 69 | self._logger.info("Getting 100 issues before cursor %s", cursor) 70 | issues, cursor = await self._gql_client.get_issues(cursor=cursor) 71 | async with self.__lock: 72 | for issue in issues: 73 | self.issues[issue.number] = issue 74 | return cursor 75 | 76 | async def update_pull_requests(self, cursor: str = None) -> Optional[str]: 77 | self._logger.info("Getting 100 pull requests before cursor %s", cursor) 78 | pull_requests, cursor = await self._gql_client.get_pull_requests(cursor=cursor) 79 | async with self.__lock: 80 | for pull_request in pull_requests: 81 | self.pull_requests[pull_request.number] = pull_request 82 | return cursor 83 | 84 | async def update_discussions(self, cursor: str = None) -> Optional[str]: 85 | self._logger.info("Getting 100 discussions before cursor %s", cursor) 86 | discussions, cursor = await self._gql_client.get_discussions(cursor=cursor) 87 | async with self.__lock: 88 | for discussion in discussions: 89 | self.discussions[discussion.number] = discussion 90 | return cursor 91 | 92 | async def get_thread( 93 | self, number: int, owner: str = DEFAULT_REPO_OWNER, repo: str = DEFAULT_REPO_NAME 94 | ) -> Union[Issue, PullRequest, Discussion, None]: 95 | if owner != DEFAULT_REPO_OWNER or repo != DEFAULT_REPO_NAME: 96 | self._logger.info("Getting issue %d for %s/%s", number, owner, repo) 97 | try: 98 | thread = await self._gql_client.get_thread( 99 | number=number, organization=owner, repository=repo 100 | ) 101 | 102 | if owner == DEFAULT_REPO_OWNER and repo == DEFAULT_REPO_NAME: 103 | async with self.__lock: 104 | if isinstance(thread, Issue): 105 | self.issues[thread.number] = thread 106 | if isinstance(thread, PullRequest): 107 | self.pull_requests[thread.number] = thread 108 | if isinstance(thread, Discussion): 109 | self.discussions[thread.number] = thread 110 | 111 | return thread 112 | except GraphQLError as exc: 113 | self._logger.exception( 114 | "Error while getting issue %d for %s/%s", number, owner, repo, exc_info=exc 115 | ) 116 | return None 117 | 118 | async def get_commit( 119 | self, sha: str, owner: str = DEFAULT_REPO_OWNER, repo: str = DEFAULT_REPO_NAME 120 | ) -> Optional[Commit]: 121 | if owner != DEFAULT_REPO_OWNER or repo != DEFAULT_REPO_NAME: 122 | self._logger.info("Getting commit %s for %s/%s", sha[:7], owner, repo) 123 | try: 124 | return await self._gql_client.get_commit(sha=sha, organization=owner, repository=repo) 125 | except GraphQLError as exc: 126 | self._logger.exception( 127 | "Error while getting commit %s for %s/%s", sha[:7], owner, repo, exc_info=exc 128 | ) 129 | return None 130 | -------------------------------------------------------------------------------- /components/graphql_queries/getCommit.gql: -------------------------------------------------------------------------------- 1 | query getCommit($sha: String!, $organization: String = "python-telegram-bot", $repository: String = "python-telegram-bot") { 2 | repository(owner: $organization, name: $repository) { 3 | object(expression: $sha) { 4 | ... on Commit { 5 | author { 6 | user { 7 | login 8 | url 9 | } 10 | } 11 | url 12 | message 13 | oid 14 | } 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /components/graphql_queries/getDiscussions.gql: -------------------------------------------------------------------------------- 1 | query getDiscussions($cursor: String) { 2 | repository(owner: "python-telegram-bot", name: "python-telegram-bot") { 3 | discussions(last: 100, before: $cursor) { 4 | nodes { 5 | number 6 | title 7 | url 8 | author { 9 | login 10 | url 11 | } 12 | } 13 | pageInfo { 14 | hasPreviousPage 15 | startCursor 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /components/graphql_queries/getExamples.gql: -------------------------------------------------------------------------------- 1 | query getExamples { 2 | repository(owner: "python-telegram-bot", name: "python-telegram-bot") { 3 | object(expression: "master:examples") { 4 | ... on Tree { 5 | entries { 6 | name 7 | } 8 | } 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /components/graphql_queries/getIssues.gql: -------------------------------------------------------------------------------- 1 | query getIssues($cursor: String) { 2 | repository(owner: "python-telegram-bot", name: "python-telegram-bot") { 3 | issues(last: 100, before: $cursor) { 4 | nodes { 5 | number 6 | title 7 | url 8 | author { 9 | login 10 | url 11 | } 12 | } 13 | pageInfo { 14 | hasPreviousPage 15 | startCursor 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /components/graphql_queries/getPTBContribs.gql: -------------------------------------------------------------------------------- 1 | query getPTBContribs { 2 | repository(owner: "python-telegram-bot", name: "ptbcontrib") { 3 | object(expression: "main:ptbcontrib") { 4 | ... on Tree { 5 | entries { 6 | name 7 | type 8 | } 9 | } 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /components/graphql_queries/getPullRequests.gql: -------------------------------------------------------------------------------- 1 | query getPullRequests($cursor: String) { 2 | repository(owner: "python-telegram-bot", name: "python-telegram-bot") { 3 | pullRequests(last: 100, before: $cursor) { 4 | nodes { 5 | number 6 | title 7 | url 8 | author { 9 | login 10 | url 11 | } 12 | } 13 | pageInfo { 14 | hasPreviousPage 15 | startCursor 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /components/graphql_queries/getThread.gql: -------------------------------------------------------------------------------- 1 | query getThread($number: Int!, $organization: String = "python-telegram-bot", $repository: String = "python-telegram-bot") { 2 | repository(owner: $organization, name: $repository) { 3 | issueOrPullRequest(number: $number) { 4 | ... on Issue { 5 | number 6 | url 7 | title 8 | author { 9 | login 10 | url 11 | } 12 | __typename 13 | } 14 | ... on PullRequest { 15 | number 16 | url 17 | title 18 | author { 19 | login 20 | url 21 | } 22 | __typename 23 | } 24 | } 25 | discussion(number: $number) { 26 | number 27 | url 28 | title 29 | author { 30 | login 31 | url 32 | } 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /components/graphqlclient.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from typing import Any, Dict, List, Optional, Tuple, Union 3 | 4 | from gql import Client, gql 5 | from gql.client import AsyncClientSession 6 | from gql.transport.aiohttp import AIOHTTPTransport 7 | from gql.transport.exceptions import TransportQueryError 8 | 9 | from components.const import DEFAULT_REPO_NAME, DEFAULT_REPO_OWNER, PTBCONTRIB_LINK, USER_AGENT 10 | from components.entrytypes import Commit, Discussion, Example, Issue, PTBContrib, PullRequest 11 | 12 | 13 | class GraphQLClient: 14 | def __init__(self, auth: str, user_agent: str = USER_AGENT) -> None: 15 | # OAuth token must be prepended with "Bearer". User might forget to do this. 16 | authorization = auth if auth.casefold().startswith("bearer ") else f"Bearer {auth}" 17 | 18 | self._transport = AIOHTTPTransport( 19 | url="https://api.github.com/graphql", 20 | headers={ 21 | "Authorization": authorization, 22 | "user-agent": user_agent, 23 | }, 24 | ) 25 | self._session = AsyncClientSession(Client(transport=self._transport)) 26 | 27 | async def initialize(self) -> None: 28 | await self._transport.connect() 29 | 30 | async def shutdown(self) -> None: 31 | await self._transport.close() 32 | 33 | async def _do_request( 34 | self, query_name: str, variable_values: Dict[str, Any] = None 35 | ) -> Dict[str, Any]: 36 | return await self._session.execute( 37 | gql(Path(f"components/graphql_queries/{query_name}.gql").read_text(encoding="utf-8")), 38 | variable_values=variable_values, 39 | ) 40 | 41 | async def get_examples(self) -> List[Example]: 42 | """The all examples on the master branch""" 43 | result = await self._do_request("getExamples") 44 | return [ 45 | Example(name=file["name"]) 46 | for file in result["repository"]["object"]["entries"] 47 | if file["name"].endswith(".py") 48 | ] 49 | 50 | async def get_ptb_contribs(self) -> List[PTBContrib]: 51 | """The all ptb_contribs on the main branch""" 52 | result = await self._do_request("getPTBContribs") 53 | return [ 54 | PTBContrib( 55 | name=contrib["name"], 56 | url=f"{PTBCONTRIB_LINK}tree/main/ptbcontrib/{contrib['name']}", 57 | ) 58 | for contrib in result["repository"]["object"]["entries"] 59 | if contrib["type"] == "tree" 60 | ] 61 | 62 | async def get_thread( 63 | self, 64 | number: int, 65 | organization: str = DEFAULT_REPO_OWNER, 66 | repository: str = DEFAULT_REPO_NAME, 67 | ) -> Union[Issue, PullRequest, Discussion]: 68 | """Get a specific thread (issue/pr/discussion) on any repository. By default, ptb/ptb 69 | will be searched""" 70 | # The try-except is needed because we query for both issueOrPR & discussion at the same 71 | # time, but it will only ever be one of them. Unfortunately we don't know which one … 72 | try: 73 | result = await self._do_request( 74 | "getThread", 75 | variable_values={ 76 | "number": number, 77 | "organization": organization, 78 | "repository": repository, 79 | }, 80 | ) 81 | except TransportQueryError as exc: 82 | # … but the exc.data will contain the thread that is available 83 | if not exc.data: 84 | raise exc 85 | result = exc.data 86 | 87 | data = result["repository"] 88 | thread_data = data["issueOrPullRequest"] or data["discussion"] 89 | 90 | entry_type_data = { 91 | "owner": organization, 92 | "repo": repository, 93 | "number": number, 94 | "title": thread_data["title"], 95 | "url": thread_data["url"], 96 | "author": thread_data["author"]["login"], 97 | } 98 | 99 | if thread_data.get("__typename") == "Issue": 100 | return Issue(**entry_type_data) 101 | if thread_data.get("__typename") == "PullRequest": 102 | return PullRequest(**entry_type_data) 103 | return Discussion(**entry_type_data) 104 | 105 | async def get_commit( 106 | self, 107 | sha: str, 108 | organization: str = DEFAULT_REPO_OWNER, 109 | repository: str = DEFAULT_REPO_NAME, 110 | ) -> Commit: 111 | """Get a specific commit on any repository. By default, ptb/ptb 112 | will be searched""" 113 | result = await self._do_request( 114 | "getCommit", 115 | variable_values={ 116 | "sha": sha, 117 | "organization": organization, 118 | "repository": repository, 119 | }, 120 | ) 121 | data = result["repository"]["object"] 122 | return Commit( 123 | owner=organization, 124 | repo=repository, 125 | sha=data["oid"], 126 | url=data["url"], 127 | title=data["message"], 128 | author=data["author"]["user"]["login"], 129 | ) 130 | 131 | async def get_issues(self, cursor: str = None) -> Tuple[List[Issue], Optional[str]]: 132 | """Last 100 issues before cursor""" 133 | result = await self._do_request("getIssues", variable_values={"cursor": cursor}) 134 | return [ 135 | Issue( 136 | owner=DEFAULT_REPO_OWNER, 137 | repo=DEFAULT_REPO_NAME, 138 | number=issue["number"], 139 | title=issue["title"], 140 | url=issue["url"], 141 | author=issue["author"]["login"] if issue["author"] else None, 142 | ) 143 | for issue in result["repository"]["issues"]["nodes"] 144 | ], result["repository"]["issues"]["pageInfo"]["startCursor"] 145 | 146 | async def get_pull_requests( 147 | self, cursor: str = None 148 | ) -> Tuple[List[PullRequest], Optional[str]]: 149 | """Last 100 pull requests before cursor""" 150 | result = await self._do_request("getPullRequests", variable_values={"cursor": cursor}) 151 | return [ 152 | PullRequest( 153 | owner=DEFAULT_REPO_OWNER, 154 | repo=DEFAULT_REPO_NAME, 155 | number=pull_request["number"], 156 | title=pull_request["title"], 157 | url=pull_request["url"], 158 | author=pull_request["author"]["login"] if pull_request["author"] else None, 159 | ) 160 | for pull_request in result["repository"]["pullRequests"]["nodes"] 161 | ], result["repository"]["pullRequests"]["pageInfo"]["startCursor"] 162 | 163 | async def get_discussions(self, cursor: str = None) -> Tuple[List[Discussion], Optional[str]]: 164 | """Last 100 discussions before cursor""" 165 | result = await self._do_request("getDiscussions", variable_values={"cursor": cursor}) 166 | return [ 167 | Discussion( 168 | owner=DEFAULT_REPO_OWNER, 169 | repo=DEFAULT_REPO_NAME, 170 | number=discussion["number"], 171 | title=discussion["title"], 172 | url=discussion["url"], 173 | author=discussion["author"]["login"] if discussion["author"] else None, 174 | ) 175 | for discussion in result["repository"]["discussions"]["nodes"] 176 | ], result["repository"]["discussions"]["pageInfo"]["startCursor"] 177 | -------------------------------------------------------------------------------- /components/inlinequeries.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | from typing import cast 3 | from uuid import uuid4 4 | 5 | from telegram import ( 6 | InlineKeyboardMarkup, 7 | InlineQuery, 8 | InlineQueryResultArticle, 9 | InputTextMessageContent, 10 | Update, 11 | ) 12 | from telegram.error import BadRequest 13 | from telegram.ext import ContextTypes 14 | 15 | from components.const import ENCLOSED_REGEX, ENCLOSING_REPLACEMENT_CHARACTER 16 | from components.entrytypes import Issue 17 | from components.search import Search 18 | 19 | 20 | def article( 21 | title: str = "", 22 | description: str = "", 23 | message_text: str = "", 24 | key: str = None, 25 | reply_markup: InlineKeyboardMarkup = None, 26 | ) -> InlineQueryResultArticle: 27 | return InlineQueryResultArticle( 28 | id=key or str(uuid4()), 29 | title=title, 30 | description=description, 31 | input_message_content=InputTextMessageContent(message_text=message_text), 32 | reply_markup=reply_markup, 33 | ) 34 | 35 | 36 | async def inline_query( 37 | update: Update, context: ContextTypes.DEFAULT_TYPE 38 | ) -> None: # pylint: disable=R0915 39 | ilq = cast(InlineQuery, update.inline_query) 40 | query = ilq.query 41 | switch_pm_text = "❓ Help" 42 | search = cast(Search, context.bot_data["search"]) 43 | 44 | if ENCLOSED_REGEX.search(query): 45 | results_list = [] 46 | symbols = tuple(ENCLOSED_REGEX.findall(query)) 47 | search_results = await search.multi_search_combinations(symbols) 48 | 49 | for combination in search_results: 50 | if len(symbols) == 1: 51 | # If we have only one search term, we can show a more verbose description 52 | description = list(combination.values())[0].display_name 53 | else: 54 | description = ", ".join(entry.short_description for entry in combination.values()) 55 | 56 | message_text = query 57 | index = [] 58 | buttons = None 59 | 60 | for symbol, entry in combination.items(): 61 | char = ENCLOSING_REPLACEMENT_CHARACTER 62 | message_text = message_text.replace( 63 | f"{char}{symbol}{char}", entry.html_insertion_markup(symbol) 64 | ) 65 | if isinstance(entry, Issue): 66 | index.append(entry.html_markup(symbol)) 67 | # Merge keyboards into one 68 | if entry_kb := entry.inline_keyboard: 69 | if buttons is None: 70 | buttons = [ 71 | [deepcopy(button) for button in row] 72 | for row in entry_kb.inline_keyboard 73 | ] 74 | else: 75 | buttons.extend(entry_kb.inline_keyboard) 76 | 77 | keyboard = InlineKeyboardMarkup(buttons) if buttons else None 78 | 79 | if index: 80 | message_text += "\n\n" + "\n".join(index) 81 | 82 | results_list.append( 83 | article( 84 | title="Insert links into message", 85 | description=description, 86 | message_text=message_text, 87 | reply_markup=keyboard, 88 | ) 89 | ) 90 | else: 91 | simple_search_results = await search.search(query) 92 | if not simple_search_results: 93 | results_list = [] 94 | switch_pm_text = "❌ No Search Results Found" 95 | else: 96 | results_list = [ 97 | article( 98 | title=entry.display_name, 99 | description=entry.description, 100 | message_text=entry.html_markup(query), 101 | reply_markup=entry.inline_keyboard, 102 | ) 103 | for entry in simple_search_results 104 | ] 105 | 106 | try: 107 | await ilq.answer( 108 | results=results_list, 109 | switch_pm_text=switch_pm_text, 110 | switch_pm_parameter="inline-help", 111 | cache_time=0, 112 | auto_pagination=True, 113 | ) 114 | except BadRequest as exc: 115 | if "can't parse entities" not in exc.message: 116 | raise exc 117 | await ilq.answer( 118 | results=[], 119 | switch_pm_text="❌ Invalid entities. Click me.", 120 | switch_pm_parameter="inline-entity-parsing", 121 | ) 122 | -------------------------------------------------------------------------------- /components/joinrequests.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | from typing import Tuple, Union, cast 3 | 4 | from telegram import ( 5 | CallbackQuery, 6 | ChatJoinRequest, 7 | InlineKeyboardButton, 8 | InlineKeyboardMarkup, 9 | Message, 10 | Update, 11 | User, 12 | ) 13 | from telegram.error import BadRequest, Forbidden 14 | from telegram.ext import ContextTypes, Job, JobQueue 15 | 16 | from components.const import ( 17 | ERROR_CHANNEL_CHAT_ID, 18 | OFFTOPIC_CHAT_ID, 19 | OFFTOPIC_RULES, 20 | ONTOPIC_CHAT_ID, 21 | ONTOPIC_RULES, 22 | ONTOPIC_USERNAME, 23 | ) 24 | 25 | 26 | def get_dtm_str() -> str: 27 | return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:%f") 28 | 29 | 30 | async def approve_user( 31 | user: Union[int, User], chat_id: int, group_name: str, context: ContextTypes.DEFAULT_TYPE 32 | ) -> None: 33 | try: 34 | if isinstance(user, User): 35 | await user.approve_join_request(chat_id=chat_id) 36 | else: 37 | await context.bot.approve_chat_join_request(user_id=user, chat_id=chat_id) 38 | except BadRequest as exc: 39 | user_mention = f"{user.username} - {user.id}" if isinstance(user, User) else str(user) 40 | error_message = f"{exc} - {user_mention} - {group_name}" 41 | raise BadRequest(error_message) from exc 42 | except Forbidden as exc: 43 | if "user is deactivated" not in exc.message: 44 | raise exc 45 | 46 | 47 | async def decline_user( 48 | user: Union[int, User], chat_id: int, group_name: str, context: ContextTypes.DEFAULT_TYPE 49 | ) -> None: 50 | try: 51 | if isinstance(user, User): 52 | await user.decline_join_request(chat_id=chat_id) 53 | else: 54 | await context.bot.decline_chat_join_request(user_id=user, chat_id=chat_id) 55 | except BadRequest as exc: 56 | user_mention = f"{user.username} - {user.id}" if isinstance(user, User) else str(user) 57 | error_message = f"{exc} - {user_mention} - {group_name}" 58 | raise BadRequest(error_message) from exc 59 | except Forbidden as exc: 60 | if "user is deactivated" not in exc.message: 61 | raise exc 62 | 63 | 64 | async def join_request_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 65 | join_request = cast(ChatJoinRequest, update.chat_join_request) 66 | user = join_request.from_user 67 | user_chat_id = join_request.user_chat_id 68 | chat_id = join_request.chat.id 69 | jobs = cast(JobQueue, context.job_queue).get_jobs_by_name(f"JOIN_TIMEOUT {chat_id} {user.id}") 70 | if jobs: 71 | # No need to ping the user again if we already did 72 | return 73 | 74 | on_topic = join_request.chat.username == ONTOPIC_USERNAME 75 | group_mention = ONTOPIC_CHAT_ID if on_topic else OFFTOPIC_CHAT_ID 76 | 77 | text = ( 78 | f"Hi, {user.mention_html()}! I'm {context.bot.bot.mention_html()}, the " 79 | f"guardian of the group {group_mention}, that you requested to join.\n\nBefore you can " 80 | "join the group, please carefully read the following rules of the group. Confirm that you " 81 | "have read them by double-tapping the button at the bottom of the message - that's it 🙃" 82 | f"\n\n{ONTOPIC_RULES if on_topic else OFFTOPIC_RULES}\n\n" 83 | "ℹ️ If I fail to react to your confirmation within 2 hours, please contact one of the" 84 | "administrators of the group. Admins are marked as such in the list of group members." 85 | ) 86 | reply_markup = InlineKeyboardMarkup.from_button( 87 | InlineKeyboardButton( 88 | text="I have read the rules 📖", 89 | callback_data=f"JOIN 1 {chat_id}", 90 | ) 91 | ) 92 | try: 93 | message = await context.bot.send_message( 94 | chat_id=user_chat_id, text=text, reply_markup=reply_markup 95 | ) 96 | except Forbidden: 97 | # If the user blocked the bot, let's give the admins a chance to handle that 98 | # TG also notifies the user and forwards the message once the user unblocks the bot, but 99 | # forwarding it still doesn't hurt ... 100 | text = ( 101 | f"User {user.mention_html()} with id {user.id} requested to join the group " 102 | f"{join_request.chat.username} but has blocked me. Please manually handle this." 103 | ) 104 | await context.bot.send_message(chat_id=ERROR_CHANNEL_CHAT_ID, text=text) 105 | return 106 | 107 | cast(JobQueue, context.job_queue).run_once( 108 | callback=join_request_timeout_job, 109 | when=datetime.timedelta(hours=2), 110 | data=(user, message, group_mention), 111 | name=f"JOIN_TIMEOUT {chat_id} {user.id}", 112 | user_id=user.id, 113 | chat_id=chat_id, 114 | ) 115 | 116 | 117 | async def join_request_buttons(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 118 | callback_query = cast(CallbackQuery, update.callback_query) 119 | user = cast(User, update.effective_user) 120 | _, press, chat_id = cast(str, callback_query.data).split() 121 | if press == "2": 122 | jobs = cast(JobQueue, context.job_queue).get_jobs_by_name( 123 | f"JOIN_TIMEOUT {chat_id} {user.id}" 124 | ) 125 | if jobs: 126 | for job in jobs: 127 | job.schedule_removal() 128 | 129 | try: 130 | await approve_user( 131 | user=user, chat_id=int(chat_id), group_name="Unknown", context=context 132 | ) 133 | except BadRequest as exc: 134 | # If the user was already approved for some reason, we can just ignore the error 135 | if "User_already_participant" not in exc.message: 136 | raise exc 137 | 138 | reply_markup = None 139 | else: 140 | reply_markup = InlineKeyboardMarkup.from_button( 141 | InlineKeyboardButton( 142 | text="⚠️ Tap again to confirm", 143 | callback_data=f"JOIN 2 {chat_id}", 144 | ) 145 | ) 146 | 147 | try: 148 | await callback_query.edit_message_reply_markup(reply_markup=reply_markup) 149 | except BadRequest as exc: 150 | # Ignore people clicking the button too quickly 151 | if "Message is not modified" not in exc.message: 152 | raise exc 153 | 154 | 155 | async def join_request_timeout_job(context: ContextTypes.DEFAULT_TYPE) -> None: 156 | job = cast(Job, context.job) 157 | chat_id = cast(int, job.chat_id) 158 | user, message, group = cast(Tuple[User, Message, str], job.data) 159 | text = ( 160 | f"Your request to join the group {group} has timed out. Please send a new request to join." 161 | ) 162 | await decline_user(user=user, chat_id=chat_id, group_name=group, context=context) 163 | try: 164 | await message.edit_text(text=text) 165 | except Forbidden as exc: 166 | if "user is deactivated" not in exc.message: 167 | raise exc 168 | except BadRequest as exc: 169 | # These apparently happen frequently, e.g. when user clear the chat 170 | if exc.message not in [ 171 | "Message to edit not found", 172 | "Can't access the chat", 173 | "Chat not found", 174 | ]: 175 | raise exc 176 | -------------------------------------------------------------------------------- /components/rulesjobqueue.py: -------------------------------------------------------------------------------- 1 | from telegram.ext import JobQueue 2 | 3 | 4 | class RulesJobQueue(JobQueue): 5 | """Subclass of JobQueue to add custom stop behavior.""" 6 | 7 | async def stop(self, wait: bool = True) -> None: 8 | """Declines all join requests and stops the job queue. That way, users will know that 9 | they have to apply to join again.""" 10 | # We loop instead of `asyncio.gather`-ing to minimize the risk of timeouts & flood limits 11 | for job in self.jobs(): 12 | if job.name and job.name.startswith("JOIN_TIMEOUT"): 13 | await job.run(self.application) 14 | await super().stop(wait) 15 | -------------------------------------------------------------------------------- /components/search.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import datetime 3 | import heapq 4 | import itertools 5 | from io import BytesIO 6 | from typing import Any, Dict, Iterable, List, Optional, Tuple, cast 7 | from urllib.parse import urljoin 8 | 9 | import httpx 10 | from async_lru import alru_cache 11 | from bs4 import BeautifulSoup 12 | from sphinx.util.inventory import InventoryFile 13 | from telegram.ext import Application, ContextTypes, Job, JobQueue 14 | 15 | from .const import ( 16 | DEFAULT_HEADERS, 17 | DEFAULT_REPO_NAME, 18 | DEFAULT_REPO_OWNER, 19 | DOCS_URL, 20 | EXAMPLES_URL, 21 | GITHUB_PATTERN, 22 | OFFICIAL_URL, 23 | USER_AGENT, 24 | WIKI_CODE_SNIPPETS_URL, 25 | WIKI_FAQ_URL, 26 | WIKI_FRDP_URL, 27 | WIKI_URL, 28 | ) 29 | from .entrytypes import ( 30 | BaseEntry, 31 | CodeSnippet, 32 | DocEntry, 33 | FAQEntry, 34 | FRDPEntry, 35 | ParamDocEntry, 36 | ReadmeSection, 37 | WikiPage, 38 | ) 39 | from .github import GitHub 40 | from .taghints import TAG_HINTS 41 | 42 | 43 | class Search: 44 | def __init__(self, github_auth: str, github_user_agent: str = USER_AGENT) -> None: 45 | self.__lock = asyncio.Lock() 46 | self._docs: List[DocEntry] = [] 47 | self._readme: List[ReadmeSection] = [] 48 | self._official: Dict[str, str] = {} 49 | self._wiki: List[WikiPage] = [] 50 | self._snippets: List[CodeSnippet] = [] 51 | self._faq: List[FAQEntry] = [] 52 | self._design_patterns: List[FRDPEntry] = [] 53 | self.github = GitHub(auth=github_auth, user_agent=github_user_agent) 54 | self._httpx_client = httpx.AsyncClient(headers=DEFAULT_HEADERS) 55 | 56 | async def initialize( 57 | self, application: Application[Any, Any, Any, Any, Any, JobQueue] 58 | ) -> None: 59 | await self.github.initialize() 60 | job_queue = cast(JobQueue, application.job_queue) 61 | job_queue.run_once(callback=self.update_job, when=1, data=(None, None, None)) 62 | 63 | async def shutdown(self) -> None: 64 | await self.github.shutdown() 65 | await self._httpx_client.aclose() 66 | await self.search.close() # pylint:disable=no-member 67 | await self.multi_search_combinations.close() # pylint:disable=no-member 68 | 69 | async def update_job(self, context: ContextTypes.DEFAULT_TYPE) -> None: 70 | job = cast(Job, context.job) 71 | cursors = cast(Tuple[Optional[str], Optional[str], Optional[str]], job.data) 72 | restart = not any(cursors) 73 | 74 | if restart: 75 | await asyncio.gather( 76 | context.application.create_task(self.github.update_examples()), 77 | context.application.create_task(self.github.update_ptb_contribs()), 78 | ) 79 | async with self.__lock: 80 | await asyncio.gather( 81 | context.application.create_task(self.update_readme()), 82 | context.application.create_task(self.update_docs()), 83 | context.application.create_task(self.update_wiki()), 84 | context.application.create_task(self.update_wiki_code_snippets()), 85 | context.application.create_task(self.update_wiki_faq()), 86 | context.application.create_task(self.update_wiki_design_patterns()), 87 | ) 88 | 89 | issue_cursor = ( 90 | await self.github.update_issues(cursor=cursors[0]) if restart or cursors[0] else None 91 | ) 92 | pr_cursor = ( 93 | await self.github.update_pull_requests(cursor=cursors[1]) 94 | if restart or cursors[1] 95 | else None 96 | ) 97 | discussion_cursor = ( 98 | await self.github.update_discussions(cursor=cursors[2]) 99 | if restart or cursors[2] 100 | else None 101 | ) 102 | 103 | new_cursors = (issue_cursor, pr_cursor, discussion_cursor) 104 | when = datetime.timedelta(seconds=30) if any(new_cursors) else datetime.timedelta(hours=12) 105 | cast(JobQueue, context.job_queue).run_once( 106 | callback=self.update_job, when=when, data=new_cursors 107 | ) 108 | 109 | # This is important: If the docs have changed the cache is useless 110 | self.search.cache_clear() # pylint:disable=no-member 111 | self.multi_search_combinations.cache_clear() # pylint:disable=no-member 112 | 113 | async def _update_official_docs(self) -> None: 114 | response = await self._httpx_client.get(url=OFFICIAL_URL) 115 | official_soup = BeautifulSoup(response.content, "html.parser") 116 | for anchor in official_soup.select("a.anchor"): 117 | if "-" not in anchor["href"]: 118 | self._official[anchor["href"][1:]] = anchor.next_sibling 119 | 120 | async def update_docs(self) -> None: 121 | await self._update_official_docs() 122 | response = await self._httpx_client.get( 123 | url=urljoin(DOCS_URL, "objects.inv"), 124 | headers=DEFAULT_HEADERS, 125 | follow_redirects=True, 126 | ) 127 | data = InventoryFile.load(BytesIO(response.content), DOCS_URL, urljoin) 128 | self._docs = [] 129 | for entry_type, items in data.items(): 130 | for name, (_, _, url, display_name) in items.items(): 131 | if "._" in name: 132 | # For some reason both `ext._application.Application` and `ext.Application` 133 | # are present ... 134 | continue 135 | 136 | tg_url, tg_test, tg_name = "", "", "" 137 | name_bits = name.split(".") 138 | 139 | if entry_type == "py:method" and ( 140 | "telegram.Bot" in name or "telegram.ext.ExtBot" in name 141 | ): 142 | tg_test = name_bits[-1] 143 | if entry_type == "py:attribute": 144 | tg_test = name_bits[-2] 145 | if entry_type == "py:class": 146 | tg_test = name_bits[-1] 147 | elif entry_type == "py:parameter": 148 | tg_test = name_bits[-4] 149 | 150 | tg_test = tg_test.replace("_", "").lower() 151 | 152 | if tg_test in self._official: 153 | tg_name = self._official[tg_test] 154 | tg_url = urljoin(OFFICIAL_URL, "#" + tg_name.lower()) 155 | 156 | if entry_type == "py:parameter": 157 | self._docs.append( 158 | ParamDocEntry( 159 | name=name, 160 | url=url, 161 | display_name=display_name if display_name.strip() != "-" else None, 162 | entry_type=entry_type, 163 | telegram_url=tg_url, 164 | telegram_name=tg_name, 165 | ) 166 | ) 167 | else: 168 | self._docs.append( 169 | DocEntry( 170 | name=name, 171 | url=url, 172 | display_name=display_name if display_name.strip() != "-" else None, 173 | entry_type=entry_type, 174 | telegram_url=tg_url, 175 | telegram_name=tg_name, 176 | ) 177 | ) 178 | 179 | async def update_readme(self) -> None: 180 | response = await self._httpx_client.get(url=DOCS_URL, follow_redirects=True) 181 | readme_soup = BeautifulSoup(response.content, "html.parser") 182 | self._readme = [] 183 | 184 | # parse section headers from readme 185 | for tag in ["h1", "h2", "h3", "h4", "h5"]: 186 | for headline in readme_soup.select(tag): 187 | # check if element is inside a hidden div - special casing for the 188 | # "Hidden Headline" we include for furo 189 | if headline.find_parent("div", attrs={"style": "display: none"}): 190 | continue 191 | self._readme.append( 192 | ReadmeSection( 193 | name=str(headline.contents[0]).strip(), anchor=headline.find("a")["href"] 194 | ) 195 | ) 196 | 197 | async def update_wiki(self) -> None: 198 | response = await self._httpx_client.get(url=WIKI_URL) 199 | wiki_soup = BeautifulSoup(response.content, "html.parser") 200 | self._wiki = [] 201 | 202 | # Parse main pages from custom sidebar 203 | for tag in ["ol", "ul"]: 204 | for element in wiki_soup.select(f"div.wiki-custom-sidebar > {tag}"): 205 | category = element.find_previous_sibling("div").text.strip() 206 | for list_item in element.select("li"): 207 | if list_item.a["href"] != "#": 208 | self._wiki.append( 209 | WikiPage( 210 | category=category, 211 | name=list_item.a.text.strip(), 212 | url=urljoin(WIKI_URL, list_item.a["href"]), 213 | ) 214 | ) 215 | 216 | self._wiki.append(WikiPage(category="Code Resources", name="Examples", url=EXAMPLES_URL)) 217 | 218 | async def update_wiki_code_snippets(self) -> None: 219 | response = await self._httpx_client.get(url=WIKI_CODE_SNIPPETS_URL) 220 | code_snippet_soup = BeautifulSoup(response.content, "html.parser") 221 | self._snippets = [] 222 | for headline in code_snippet_soup.select( 223 | "div#wiki-body h4,div#wiki-body h3,div#wiki-body h2" 224 | ): 225 | self._snippets.append( 226 | CodeSnippet( 227 | name=headline.text.strip(), 228 | url=urljoin(WIKI_CODE_SNIPPETS_URL, headline.find_next_sibling("a")["href"]), 229 | ) 230 | ) 231 | 232 | async def update_wiki_faq(self) -> None: 233 | response = await self._httpx_client.get(url=WIKI_FAQ_URL) 234 | faq_soup = BeautifulSoup(response.content, "html.parser") 235 | self._faq = [] 236 | for headline in faq_soup.select("div#wiki-body h3"): 237 | self._faq.append( 238 | FAQEntry( 239 | name=headline.text.strip(), 240 | url=urljoin(WIKI_FAQ_URL, headline.find_next_sibling("a")["href"]), 241 | ) 242 | ) 243 | 244 | async def update_wiki_design_patterns(self) -> None: 245 | response = await self._httpx_client.get(url=WIKI_FRDP_URL) 246 | frdp_soup = BeautifulSoup(response.content, "html.parser") 247 | self._design_patterns = [] 248 | for headline in frdp_soup.select("div#wiki-body h3,div#wiki-body h2"): 249 | self._design_patterns.append( 250 | FRDPEntry( 251 | name=headline.text.strip(), 252 | url=urljoin(WIKI_FRDP_URL, headline.find_next_sibling("a")["href"]), 253 | ) 254 | ) 255 | 256 | @staticmethod 257 | def _sort_key(entry: BaseEntry, search_query: str) -> float: 258 | return entry.compare_to_query(search_query) 259 | 260 | @alru_cache(maxsize=64) # type: ignore[misc] 261 | async def search( 262 | self, search_query: Optional[str], amount: int = None 263 | ) -> Optional[List[BaseEntry]]: 264 | """Searches all available entries for appropriate results. This includes: 265 | 266 | * readme sections 267 | * wiki pages 268 | * FAQ entries 269 | * Design Pattern entries 270 | * Code snippets 271 | * examples 272 | * documentation 273 | * ptbcontrib 274 | * issues & PRs on GH 275 | 276 | If the query is in one of the following formats, the search will *only* attempt to fand 277 | one corresponding GitHub result: 278 | 279 | * ((owner)/repo)# 280 | * @ 281 | 282 | If the query is in the format `#some search query`, only the issues on 283 | python-telegram-bot/python-telegram-bot will be searched. 284 | 285 | If the query is in the format `ptbcontrib/`, only the contributions 286 | of ptbcontrib will be searched. 287 | 288 | If the query is in the format `/search query`, only the tags hints will be searched. 289 | 290 | Args: 291 | search_query: The search query. May be None, in which case all available entries 292 | will be given. 293 | amount: Optional. If passed, returns the ``amount`` elements with the highest 294 | comparison score. 295 | 296 | Returns: 297 | The results sorted by comparison score. 298 | """ 299 | search_entries: Iterable[BaseEntry] = [] 300 | 301 | match = GITHUB_PATTERN.fullmatch(search_query) if search_query else None 302 | if match: 303 | owner, repo, number, sha, gh_search_query, ptbcontrib = ( 304 | match.groupdict()[x] 305 | for x in ("owner", "repo", "number", "sha", "query", "ptbcontrib") 306 | ) 307 | owner = owner or DEFAULT_REPO_OWNER 308 | repo = repo or DEFAULT_REPO_NAME 309 | 310 | # If it's an issue 311 | if number: 312 | issue = await self.github.get_thread(int(number), owner, repo) 313 | return [issue] if issue else None 314 | # If it's a commit 315 | if sha: 316 | commit = await self.github.get_commit(sha, owner, repo) 317 | return [commit] if commit else None 318 | # If it's a search 319 | if gh_search_query: 320 | search_query = gh_search_query 321 | search_entries = itertools.chain( 322 | self.github.all_issues, 323 | self.github.all_pull_requests, 324 | self.github.all_discussions, 325 | ) 326 | elif ptbcontrib: 327 | search_entries = self.github.all_ptbcontribs 328 | 329 | if search_query and search_query.startswith("/"): 330 | search_entries = TAG_HINTS.values() 331 | 332 | async with self.__lock: 333 | if not search_entries: 334 | search_entries = itertools.chain( 335 | self._readme, 336 | self._wiki, 337 | self.github.all_examples, 338 | self._faq, 339 | self._design_patterns, 340 | self._snippets, 341 | self.github.all_ptbcontribs, 342 | self._docs, 343 | TAG_HINTS.values(), 344 | ) 345 | 346 | if not search_query: 347 | return search_entries if isinstance(search_entries, list) else list(search_entries) 348 | 349 | if not amount: 350 | return sorted( 351 | search_entries, 352 | key=lambda entry: self._sort_key(entry, search_query), 353 | reverse=True, 354 | ) 355 | return heapq.nlargest( 356 | amount, 357 | search_entries, 358 | key=lambda entry: self._sort_key(entry, search_query), 359 | ) 360 | 361 | @alru_cache(maxsize=64) # type: ignore[misc] 362 | async def multi_search_combinations( 363 | self, search_queries: Tuple[str], results_per_query: int = 3 364 | ) -> List[Dict[str, BaseEntry]]: 365 | """For each query, runs :meth:`search` and fetches the ``results_per_query`` most likely 366 | results. Then builds all possible combinations. 367 | 368 | Args: 369 | search_queries: The search queries. 370 | results_per_query: Optional. Number of results to fetch per query. Defaults to ``3``. 371 | 372 | Returns: 373 | All possible result combinations. Each list entry is a dictionary mapping each query 374 | to the corresponding :class:`BaseEntry`. 375 | 376 | """ 377 | # Don't use a page-argument here, as the number of results will usually be relatively small 378 | # so we can just build the list once and get slices from the cached result if necessary 379 | 380 | results = {} 381 | # Remove duplicates while maintaining the order 382 | effective_queries = list(dict.fromkeys(search_queries)) 383 | for query in effective_queries: 384 | if res := await self.search(search_query=query, amount=results_per_query): 385 | results[query] = res 386 | 387 | return [ 388 | dict(zip(effective_queries, query_results)) 389 | for query_results in itertools.product(*results.values()) 390 | ] 391 | -------------------------------------------------------------------------------- /components/taghints.py: -------------------------------------------------------------------------------- 1 | import re 2 | from typing import Any, Dict, List, Match, Optional 3 | 4 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Message, MessageEntity 5 | from telegram.ext.filters import MessageFilter 6 | 7 | from components import const 8 | from components.const import DOCS_URL, PTBCONTRIB_LINK 9 | from components.entrytypes import TagHint 10 | 11 | # Tag hints should be used for "meta" hints, i.e. pointing out how to use the PTB groups 12 | # Explaining functionality should be done in the wiki instead. 13 | # 14 | # Note that wiki pages are available through the search directly, but the Ask-Right and MWE pages 15 | # are needed so frequently that we provide tag hints for them ... 16 | _TAG_HINTS: Dict[str, Dict[str, Any]] = { 17 | "askright": { 18 | "message": ( 19 | '{query} Please read this short article and try again ;)' 21 | ), 22 | "help": "The wiki page about asking technical questions", 23 | "default": ( 24 | "Hey. In order for someone to be able to help you, you must ask a good " 25 | "technical question." 26 | ), 27 | }, 28 | "mwe": { 29 | "message": ( 30 | "{query} Please follow these instructions on how to write a " 31 | 'Minimal Working Example (MWE).' 33 | ), 34 | "help": "How to build an MWE for PTB.", 35 | "default": "Hey. Please provide a minimal working example (MWE).", 36 | }, 37 | "inline": { 38 | "message": ( 39 | f"Consider using me in inline-mode 😎 @{const.SELF_BOT_NAME} " + "{query}" 40 | ), 41 | "default": "Your search terms", 42 | "buttons": [[InlineKeyboardButton(text="🔎 Try it out", switch_inline_query="")]], 43 | "help": "Give a query that will be used for a switch_to_inline-button", 44 | }, 45 | "private": { 46 | "message": "Please don't spam the group with {query}, and go to a private " 47 | "chat with me instead. Thanks a lot, the other members will appreciate it 😊", 48 | "default": "searches or commands", 49 | "buttons": [ 50 | [ 51 | InlineKeyboardButton( 52 | text="🤖 Go to private chat", url=f"https://t.me/{const.SELF_BOT_NAME}" 53 | ) 54 | ] 55 | ], 56 | "help": "Tell a member to stop spamming and switch to a private chat", 57 | }, 58 | "userbot": { 59 | "message": ( 60 | '{query} Refer to this article to learn more about Userbots.' 62 | ), 63 | "help": "What are Userbots?", 64 | "default": "", 65 | }, 66 | "meta": { 67 | "message": ( 68 | 'No need for meta questions. Just ask! 🤗' 69 | '"Has anyone done .. before?" ' 70 | "Probably. Just ask your question and somebody will help!" 71 | ), 72 | "help": "Show our stance on meta-questions", 73 | }, 74 | "tutorial": { 75 | "message": ( 76 | "{query}" 77 | "We have compiled a list of learning resources just for you:\n\n" 78 | '• As Beginner' 79 | "\n" 80 | '• As Programmer' 81 | "\n" 82 | '• Official Tutorial\n' 83 | '• Dive into Python\n' 84 | '• Learn Python\n' 85 | '• Computer Science Circles\n' 86 | '• MIT ' 88 | "OpenCourse\n" 89 | '• Hitchhiker’s Guide to Python\n' 90 | "• The @PythonRes Telegram Channel.\n" 91 | '• Corey Schafer videos for beginners and in general' 94 | "\n" 95 | '• Project Python\n' 96 | ), 97 | "help": "How to find a Python tutorial", 98 | "default": ( 99 | "Oh, hey! There's someone new joining our awesome community of Python developers ❤️ " 100 | ), 101 | }, 102 | "wronglib": { 103 | "message": ( 104 | "{query} If you are using a different package/language, we are sure you can " 105 | "find some kind of community help on their homepage. Here are a few links for other " 106 | "popular libraries: " 107 | 'pyTelegramBotApi, ' 108 | 'Telepot, ' 109 | 'pyrogram, ' 110 | 'Telethon, ' 111 | 'aiogram, ' 112 | 'botogram.' 113 | ), 114 | "help": "Other Python wrappers for Telegram", 115 | "default": ( 116 | "Hey, I think you're wrong 🧐\nThis is the support group of the " 117 | "python-telegram-bot library." 118 | ), 119 | }, 120 | "pastebin": { 121 | "message": ( 122 | "{query} Please post code or tracebacks using a pastebin rather than via plain text " 123 | "or a picture. https://pastebin.com/ is quite popular, but there are " 124 | "many alternatives " 125 | "out there. Of course, for very short snippets, text is fine. Please at " 126 | "least format it as monospace in that case." 127 | ), 128 | "help": "Ask users not to post code as text or images.", 129 | "default": "Hey.", 130 | }, 131 | "doublepost": { 132 | "message": ( 133 | "{query} Please don't double post. Questions usually are on-topic only in one of the " 134 | "two groups anyway." 135 | ), 136 | "help": "Ask users not to post the same question in both on- and off-topic.", 137 | "default": "Hey.", 138 | }, 139 | "xy": { 140 | "message": ( 141 | '{query} This seems like an xy-problem to me.' 142 | ), 143 | "default": "Hey. What exactly do you want this for?", 144 | "help": "Ask users for the actual use case.", 145 | }, 146 | "dontping": { 147 | "message": ( 148 | "{query} Please only mention or reply to users directly if you're following up on a " 149 | "conversation with them. Otherwise just ask your question and wait if someone has a " 150 | "solution for you - that's how this group works 😉 Also note that the " 151 | "@admin tag is only to be used to report spam or abuse!" 152 | ), 153 | "default": "Hey.", 154 | "help": "Tell users not to ping randomly ping you.", 155 | }, 156 | "read": { 157 | "message": ( 158 | "I just pointed you to {query} and I have the strong feeling that you did not " 159 | "actually read it. Please do so. If you don't understand everything and have " 160 | "follow up questions, that's fine, but you can't expect me to repeat everything " 161 | "just for you because you didn't feel like reading on your own. 😉" 162 | ), 163 | "default": "a resource in the wiki, the docs or the examples", 164 | "help": "Tell users to actually read the resources they were linked to", 165 | }, 166 | "ptbcontrib": { 167 | "message": ( 168 | "{query} ptbcontrib is a library that provides extensions for the " 169 | "python-telegram-bot library that written and maintained by the " 170 | "community of PTB users." 171 | ), 172 | "default": "Hey.", 173 | "buttons": [[InlineKeyboardButton(text="🔗 Take me there!", url=PTBCONTRIB_LINK)]], 174 | "help": "Display a short info text about ptbcontrib", 175 | }, 176 | "botlists": { 177 | "message": ( 178 | "{query} This group is for technical questions that come up while you code your own " 179 | "Telegram bot. If you are looking for ready-to-use bots, please have a look at " 180 | "channels like @BotsArchive or @BotList/@BotlistBot. There are also a number of " 181 | "websites that list existing bots." 182 | ), 183 | "default": "Hey.", 184 | "help": "Redirect users to lists of existing bots.", 185 | }, 186 | "coc": { 187 | "message": ( 188 | f'{{query}} Please read our Code of Conduct and ' 189 | "stick to it. Note that violation of the CoC can lead to temporary or permanent " 190 | "banishment from this group." 191 | ), 192 | "default": "Hey.", 193 | "help": "Remind the users of the Code of Conduct.", 194 | }, 195 | "docs": { 196 | "message": ( 197 | f"{{query}} You can find our documentation at Read the " 198 | f"Docs. " 199 | ), 200 | "default": "Hey.", 201 | "help": "Point users to the documentation", 202 | "group_command": True, 203 | }, 204 | "wiki": { 205 | "message": f"{{query}} You can find our wiki on Github.", 206 | "default": "Hey.", 207 | "help": "Point users to the wiki", 208 | "group_command": True, 209 | }, 210 | "help": { 211 | "message": ( 212 | "{query} You can find an explanation of @roolsbot's functionality on '" 213 | '' 214 | "GitHub." 215 | ), 216 | "default": "Hey.", 217 | "help": "Point users to the bots readme", 218 | "group_command": True, 219 | }, 220 | "upgrade": { 221 | "message": ( 222 | "{query} You seem to be using a version <=13.15 of " 223 | "python-telegram-bot. " 224 | "Please note that we only provide support for the latest stable version and that the " 225 | "library has undergone significant changes in v20. Please consider upgrading to v20 " 226 | "by reading the release notes and the transition guide linked below." 227 | ), 228 | "buttons": [ 229 | [ 230 | InlineKeyboardButton( 231 | text="🔗 Release Notes", 232 | url="https://telegra.ph/Release-notes-for-python-telegram-bot-v200a0-05-06", 233 | ), 234 | InlineKeyboardButton( 235 | text="🔗 Transition Guide", 236 | url="https://github.com/python-telegram-bot/python-telegram-bot/wiki" 237 | "/Transition-guide-to-Version-20.0", 238 | ), 239 | ] 240 | ], 241 | "default": "Hey.", 242 | "help": "Ask users to upgrade to the latest version of PTB", 243 | "group_command": True, 244 | }, 245 | "compat": { 246 | "message": ( 247 | "{query} You seem to be using the new version (>=20.0) of " 248 | "python-telegram-bot but your code is written for an older and " 249 | "deprecated version (<=13.15).\nPlease update your code to the new v20 by reading" 250 | " the release notes and the transition guide linked below.\nYou can also install a " 251 | "version of PTB that is compatible with your code base, but please note that the " 252 | "library has undergone significant changes in v20 and the older version is not " 253 | "supported anymore. It may contain bugs that will not be fixed by the PTB team " 254 | "and it also doesn't support new functions added by newer Bot API releases." 255 | ), 256 | "buttons": [ 257 | [ 258 | InlineKeyboardButton( 259 | text="🔗 Release Notes", 260 | url="https://telegra.ph/Release-notes-for-python-telegram-bot-v200a0-05-06", 261 | ), 262 | InlineKeyboardButton( 263 | text="🔗 Transition Guide", 264 | url="https://github.com/python-telegram-bot/python-telegram-bot/wiki" 265 | "/Transition-guide-to-Version-20.0", 266 | ), 267 | ] 268 | ], 269 | "default": "Hey.", 270 | "help": "Point out compatibility issues of code and PTB version to users", 271 | "group_command": True, 272 | }, 273 | "llm": { 274 | "message": ( 275 | "{query} This text reads like an AI/LLM was used to generate this. We found their " 276 | "answers to be unfitting for this group. We are all about providing fine tuned help " 277 | "for technical questions. These generated texts are often long winded, very " 278 | "explanatory answers for steps which didn't need explaining, and then happen to miss " 279 | "the actual underlying question completely or are outright false in the worst case." 280 | "\n\n" 281 | "Please refrain from this in the future. If you can answer a question yourself, we " 282 | "are glad to see a precise, technical answer. If you can not answer a question, it's " 283 | "better to just not reply instead of copy-pasting an autogenerated answer 😉." 284 | ), 285 | "default": "Hey.", 286 | "help": "Tell users not to use AI/LLM generated answers", 287 | "group_command": True, 288 | }, 289 | "traceback": { 290 | "message": ( 291 | "{query} Please show the full traceback via a pastebin. Make sure to include " 292 | "everything from the first Traceback (most recent call last): until the " 293 | "last error message. https://pastebin.com/ is a popular pastebin service, but there " 294 | "are many alternatives out " 295 | "there." 296 | ), 297 | "default": "Hey.", 298 | "help": "Ask for the full traceback", 299 | "group_command": True, 300 | }, 301 | } 302 | 303 | 304 | # Sort the hints by key 305 | _TAG_HINTS = dict(sorted(_TAG_HINTS.items())) 306 | # convert into proper objects 307 | TAG_HINTS: Dict[str, TagHint] = { 308 | key: TagHint( 309 | tag=key, 310 | message=value["message"], 311 | description=value["help"], 312 | default_query=value.get("default"), 313 | inline_keyboard=InlineKeyboardMarkup(value["buttons"]) if "buttons" in value else None, 314 | group_command=value.get("group_command", False), 315 | ) 316 | for key, value in _TAG_HINTS.items() 317 | } 318 | TAG_HINTS_PATTERN = re.compile( 319 | # case insensitive 320 | r"(?i)" 321 | # join the /tags 322 | r"((?P(?P" 323 | rf'{"|".join(hint.short_name for hint in TAG_HINTS.values())})' 324 | # don't allow the tag to be followed by '/' - That could be the start of the next tag 325 | r"(?!/)" 326 | # Optionally the bots username 327 | rf"(@{re.escape(const.SELF_BOT_NAME)})?)" 328 | # match everything that comes next as long as it's separated by a whitespace - important for 329 | # inserting a custom query in inline mode 330 | r"($| (?P[^\/.]*)))" 331 | ) 332 | 333 | 334 | class TagHintFilter(MessageFilter): 335 | """Custom filter class for filtering for tag hint messages""" 336 | 337 | def __init__(self) -> None: 338 | super().__init__(name="TageHintFilter", data_filter=True) 339 | 340 | def filter(self, message: Message) -> Optional[Dict[str, List[Match]]]: 341 | """Does the filtering. Applies the regex and makes sure that only those tag hints are 342 | handled, that are also marked as bot command. 343 | """ 344 | if not message.text: 345 | return None 346 | 347 | matches = [] 348 | command_texts = message.parse_entities([MessageEntity.BOT_COMMAND]).values() 349 | for match in TAG_HINTS_PATTERN.finditer(message.text): 350 | if match.groupdict()["tag_hint_with_username"] in command_texts: 351 | matches.append(match) 352 | 353 | if not matches: 354 | return None 355 | 356 | return {"matches": matches} 357 | -------------------------------------------------------------------------------- /components/util.py: -------------------------------------------------------------------------------- 1 | # pylint:disable=cyclic-import 2 | # because we import truncate_str in entrytypes.Issue.short_description 3 | import logging 4 | import re 5 | import sys 6 | import warnings 7 | from functools import wraps 8 | from typing import Any, Callable, Coroutine, Dict, List, Optional, Pattern, Tuple, Union, cast 9 | 10 | from bs4 import MarkupResemblesLocatorWarning 11 | from telegram import Bot, Chat, InlineKeyboardButton, Message, Update, User 12 | from telegram.error import BadRequest, Forbidden, InvalidToken 13 | from telegram.ext import CallbackContext, ContextTypes, filters 14 | 15 | from .const import OFFTOPIC_CHAT_ID, ONTOPIC_CHAT_ID, RATE_LIMIT_SPACING 16 | from .taghints import TAG_HINTS 17 | 18 | # Messages may contain links that we don't care about - so let's ignore the warnings 19 | warnings.filterwarnings("ignore", category=MarkupResemblesLocatorWarning, module="bs4") 20 | 21 | 22 | def get_reply_id(update: Update) -> Optional[int]: 23 | if update.effective_message and update.effective_message.reply_to_message: 24 | return update.effective_message.reply_to_message.message_id 25 | return None 26 | 27 | 28 | async def reply_or_edit(update: Update, context: CallbackContext, text: str) -> None: 29 | chat_data = cast(Dict, context.chat_data) 30 | if update.edited_message and update.edited_message.message_id in chat_data: 31 | try: 32 | await chat_data[update.edited_message.message_id].edit_text(text) 33 | except BadRequest as exc: 34 | if "not modified" not in str(exc): 35 | raise exc 36 | else: 37 | message = cast(Message, update.effective_message) 38 | issued_reply = get_reply_id(update) 39 | if issued_reply: 40 | chat_data[message.message_id] = await context.bot.send_message( 41 | message.chat_id, 42 | text, 43 | reply_to_message_id=issued_reply, 44 | ) 45 | else: 46 | chat_data[message.message_id] = await message.reply_text(text) 47 | 48 | 49 | def get_text_not_in_entities(message: Message) -> str: 50 | if message.text is None: 51 | raise ValueError("Message has no text!") 52 | 53 | if sys.maxunicode != 0xFFFF: 54 | text: Union[str, bytes] = message.text.encode("utf-16-le") 55 | else: 56 | text = message.text 57 | 58 | removed_chars = 0 59 | for entity in message.entities: 60 | start = entity.offset - removed_chars 61 | end = entity.offset + entity.length - removed_chars 62 | removed_chars += entity.length 63 | 64 | if sys.maxunicode != 0xFFFF: 65 | start = 2 * start 66 | end = 2 * end 67 | 68 | text = text[:start] + text[end:] # type: ignore 69 | 70 | if isinstance(text, str): 71 | return text 72 | return text.decode("utf-16-le") 73 | 74 | 75 | def build_menu( 76 | buttons: List[InlineKeyboardButton], 77 | n_cols: int, 78 | header_buttons: List[InlineKeyboardButton] = None, 79 | footer_buttons: List[InlineKeyboardButton] = None, 80 | ) -> List[List[InlineKeyboardButton]]: 81 | menu = [buttons[i : i + n_cols] for i in range(0, len(buttons), n_cols)] 82 | if header_buttons: 83 | menu.insert(0, header_buttons) 84 | if footer_buttons: 85 | menu.append(footer_buttons) 86 | return menu 87 | 88 | 89 | async def try_to_delete(message: Message) -> bool: 90 | try: 91 | return await message.delete() 92 | except (BadRequest, Forbidden): 93 | return False 94 | 95 | 96 | async def rate_limit_tracker(_: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 97 | data = cast(Dict, context.chat_data).setdefault("rate_limit", {}) 98 | 99 | for key in data.keys(): 100 | data[key] += 1 101 | 102 | 103 | def rate_limit( 104 | func: Callable[[Update, ContextTypes.DEFAULT_TYPE], Coroutine[Any, Any, None]], 105 | ) -> Callable[[Update, ContextTypes.DEFAULT_TYPE], Coroutine[Any, Any, None]]: 106 | """ 107 | Rate limit command so that RATE_LIMIT_SPACING non-command messages are 108 | required between invocations. Private chats are not rate limited. 109 | """ 110 | 111 | @wraps(func) 112 | async def wrapper(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 113 | if chat := update.effective_chat: 114 | if chat.type == chat.PRIVATE: 115 | return await func(update, context) 116 | 117 | # Get rate limit data 118 | data = cast(Dict, context.chat_data).setdefault("rate_limit", {}) 119 | 120 | # If we have not seen two non-command messages since last of type `func` 121 | if data.get(func, RATE_LIMIT_SPACING) < RATE_LIMIT_SPACING: 122 | logging.debug("Ignoring due to rate limit!") 123 | context.application.create_task( 124 | try_to_delete(cast(Message, update.effective_message)), update=update 125 | ) 126 | return None 127 | 128 | data[func] = 0 129 | return await func(update, context) 130 | 131 | return wrapper 132 | 133 | 134 | def truncate_str(string: str, max_length: int) -> str: 135 | return (string[:max_length] + "…") if len(string) > max_length else string 136 | 137 | 138 | def build_command_list( 139 | private: bool = False, group_name: str = None, admins: bool = False 140 | ) -> List[Tuple[str, str]]: 141 | base_commands = [ 142 | (hint.tag, hint.description) for hint in TAG_HINTS.values() if hint.group_command 143 | ] 144 | hint_commands = [ 145 | (hint.tag, hint.description) for hint in TAG_HINTS.values() if not hint.group_command 146 | ] 147 | 148 | if private: 149 | return base_commands + hint_commands 150 | 151 | base_commands += [ 152 | ("privacy", "Show the privacy policy of this bot"), 153 | ("rules", "Show the rules for this group."), 154 | ("buy", "Tell people to not do job offers."), 155 | ("token", "Warn people if they share a token."), 156 | ] 157 | 158 | if group_name is None: 159 | return base_commands + hint_commands 160 | 161 | on_off_topic = [ 162 | { 163 | ONTOPIC_CHAT_ID: ("off_topic", "Redirect to the off-topic group"), 164 | OFFTOPIC_CHAT_ID: ("on_topic", "Redirect to the on-topic group"), 165 | }[group_name], 166 | ] 167 | 168 | if not admins: 169 | return base_commands + on_off_topic + hint_commands 170 | 171 | say_potato = [("say_potato", "Send captcha to a potential userbot")] 172 | 173 | return base_commands + on_off_topic + say_potato + hint_commands 174 | 175 | 176 | async def admin_check(chat_data: Dict, chat: Chat, who_banned: User) -> bool: 177 | # This check will fail if we add or remove admins at runtime but that is so rare that 178 | # we can just restart the bot in that case ... 179 | admins = chat_data.setdefault("admins", await chat.get_administrators()) 180 | if who_banned not in [admin.user for admin in admins]: 181 | return False 182 | return True 183 | 184 | 185 | async def get_bot_from_token(token: str) -> Optional[User]: 186 | bot = Bot(token) 187 | 188 | try: 189 | user = await bot.get_me() 190 | return user 191 | 192 | # raised when the token isn't valid 193 | except InvalidToken: 194 | return None 195 | 196 | 197 | def update_shared_token_timestamp(message: Message, context: ContextTypes.DEFAULT_TYPE) -> str: 198 | chat_data = cast(Dict, context.chat_data) 199 | key = "shared_token_timestamp" 200 | 201 | last_time = chat_data.get(key) 202 | current_time = message.date 203 | chat_data[key] = current_time 204 | 205 | if last_time is None: 206 | return ( 207 | "... Error... No time found....\n" 208 | "Oh my god. Where is the time. Has someone seen the time?" 209 | ) 210 | 211 | time_diff = current_time - last_time 212 | # We do a day counter for now 213 | return f"{time_diff.days}" 214 | 215 | 216 | class FindAllFilter(filters.MessageFilter): 217 | __slots__ = ("pattern",) 218 | 219 | def __init__(self, pattern: Union[str, Pattern]): 220 | if isinstance(pattern, str): 221 | pattern = re.compile(pattern) 222 | self.pattern: Pattern = pattern 223 | super().__init__(data_filter=True) 224 | 225 | def filter(self, message: Message) -> Optional[Dict[str, List[str]]]: 226 | if message.text: 227 | matches = re.findall(self.pattern, message.text) 228 | if matches: 229 | return {"matches": matches} 230 | return {} 231 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.black] 2 | line-length = 99 3 | target-version = ['py38', 'py39', 'py310'] 4 | 5 | [tool.isort] # black config 6 | profile = "black" 7 | line_length = 99 -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | pre-commit -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # Make sure to install those as additional_dependencies in the 2 | # pre-commit hooks for pylint & mypy 3 | beautifulsoup4~=4.11.0 4 | thefuzz~=0.19.0 5 | python-Levenshtein~=0.25.0 6 | python-telegram-bot[job-queue]==20.2 7 | Sphinx~=5.0.2 8 | httpx~=0.23.0 9 | gql[aiohttp]~=3.5.0 10 | async-lru~=1.0.3 11 | -------------------------------------------------------------------------------- /rules_bot.py: -------------------------------------------------------------------------------- 1 | import configparser 2 | import logging 3 | import os 4 | from typing import cast 5 | 6 | import httpx 7 | from telegram import ( 8 | BotCommandScopeAllGroupChats, 9 | BotCommandScopeAllPrivateChats, 10 | BotCommandScopeChat, 11 | BotCommandScopeChatAdministrators, 12 | Update, 13 | ) 14 | from telegram.constants import ParseMode 15 | from telegram.ext import ( 16 | Application, 17 | ApplicationBuilder, 18 | CallbackQueryHandler, 19 | ChatJoinRequestHandler, 20 | CommandHandler, 21 | Defaults, 22 | InlineQueryHandler, 23 | MessageHandler, 24 | TypeHandler, 25 | filters, 26 | ) 27 | 28 | from components import inlinequeries 29 | from components.callbacks import ( 30 | ban_sender_channels, 31 | buy, 32 | command_token_warning, 33 | compat_warning, 34 | delete_message, 35 | leave_chat, 36 | long_code_handling, 37 | off_on_topic, 38 | privacy, 39 | raise_app_handler_stop, 40 | regex_token_warning, 41 | reply_search, 42 | rules, 43 | sandwich, 44 | say_potato_button, 45 | say_potato_command, 46 | start, 47 | tag_hint, 48 | ) 49 | from components.const import ( 50 | COMPAT_ERRORS, 51 | DESCRIPTION, 52 | ERROR_CHANNEL_CHAT_ID, 53 | OFFTOPIC_CHAT_ID, 54 | OFFTOPIC_USERNAME, 55 | ONTOPIC_CHAT_ID, 56 | ONTOPIC_USERNAME, 57 | SHORT_DESCRIPTION, 58 | ) 59 | from components.errorhandler import error_handler 60 | from components.joinrequests import join_request_buttons, join_request_callback 61 | from components.rulesjobqueue import RulesJobQueue 62 | from components.search import Search 63 | from components.taghints import TagHintFilter 64 | from components.util import FindAllFilter, build_command_list, rate_limit_tracker 65 | 66 | if os.environ.get("ROOLSBOT_DEBUG"): 67 | logging.basicConfig( 68 | format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.DEBUG 69 | ) 70 | else: 71 | logging.basicConfig( 72 | format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO 73 | ) 74 | logging.getLogger("apscheduler").setLevel(logging.WARNING) 75 | logging.getLogger("gql").setLevel(logging.WARNING) 76 | 77 | logger = logging.getLogger(__name__) 78 | 79 | 80 | async def post_init(application: Application) -> None: 81 | bot = application.bot 82 | await cast(Search, application.bot_data["search"]).initialize(application) 83 | 84 | await bot.set_my_short_description(SHORT_DESCRIPTION) 85 | await bot.set_my_description(DESCRIPTION) 86 | 87 | # set commands 88 | await bot.set_my_commands( 89 | build_command_list(private=True), 90 | scope=BotCommandScopeAllPrivateChats(), 91 | ) 92 | await bot.set_my_commands( 93 | build_command_list(private=False), 94 | scope=BotCommandScopeAllGroupChats(), 95 | ) 96 | 97 | for group_name in [ONTOPIC_CHAT_ID, OFFTOPIC_CHAT_ID]: 98 | await bot.set_my_commands( 99 | build_command_list(private=False, group_name=group_name), 100 | scope=BotCommandScopeChat(group_name), 101 | ) 102 | await bot.set_my_commands( 103 | build_command_list(private=False, group_name=group_name, admins=True), 104 | scope=BotCommandScopeChatAdministrators(group_name), 105 | ) 106 | 107 | 108 | async def post_shutdown(application: Application) -> None: 109 | await cast(Search, application.bot_data["search"]).shutdown() 110 | 111 | 112 | def main() -> None: 113 | config = configparser.ConfigParser() 114 | config.read("bot.ini") 115 | 116 | defaults = Defaults(parse_mode=ParseMode.HTML, disable_web_page_preview=True) 117 | application = ( 118 | ApplicationBuilder() 119 | .token(config["KEYS"]["bot_api"]) 120 | .defaults(defaults) 121 | .post_init(post_init) 122 | .post_shutdown(post_shutdown) 123 | .job_queue(RulesJobQueue()) 124 | .build() 125 | ) 126 | 127 | application.bot_data["search"] = Search(github_auth=config["KEYS"]["github_auth"]) 128 | 129 | if "pastebin_auth" in config["KEYS"]: 130 | application.bot_data["pastebin_client"] = httpx.AsyncClient( 131 | auth=httpx.BasicAuth(username="Rools", password=config["KEYS"]["pastebin_auth"]) 132 | ) 133 | 134 | # Note: Order matters! 135 | 136 | # Don't handle messages that were sent in the error channel 137 | application.add_handler( 138 | MessageHandler(filters.Chat(chat_id=ERROR_CHANNEL_CHAT_ID), raise_app_handler_stop), 139 | group=-2, 140 | ) 141 | # Leave groups that are not maintained by PTB 142 | application.add_handler( 143 | TypeHandler( 144 | type=Update, 145 | callback=leave_chat, 146 | ), 147 | group=-2, 148 | ) 149 | 150 | application.add_handler(MessageHandler(~filters.COMMAND, rate_limit_tracker), group=-2) 151 | 152 | # We need several different patterns, so filters.REGEX doesn't do the trick 153 | # therefore we catch everything and do regex ourselves. In case the message contains a 154 | # long code block, we'll raise AppHandlerStop to prevent further processing. 155 | application.add_handler(MessageHandler(filters.TEXT, long_code_handling), group=-1) 156 | 157 | application.add_handler( 158 | MessageHandler( 159 | filters.SenderChat.CHANNEL & ~filters.ChatType.CHANNEL & ~filters.IS_AUTOMATIC_FORWARD, 160 | ban_sender_channels, 161 | block=False, 162 | ) 163 | ) 164 | 165 | # Simple commands 166 | # The first one also handles deep linking /start commands 167 | application.add_handler(CommandHandler("start", start)) 168 | application.add_handler(CommandHandler("rules", rules)) 169 | application.add_handler(CommandHandler("buy", buy)) 170 | application.add_handler(CommandHandler("privacy", privacy)) 171 | 172 | # Stuff that runs on every message with regex 173 | application.add_handler( 174 | MessageHandler( 175 | filters.Regex(r"(?i)[\s\S]*?((sudo )?make me a sandwich)[\s\S]*?"), sandwich 176 | ) 177 | ) 178 | application.add_handler(MessageHandler(filters.Regex("/(on|off)_topic"), off_on_topic)) 179 | 180 | # Warn user who shared a bot's token 181 | application.add_handler(CommandHandler("token", command_token_warning)) 182 | application.add_handler( 183 | MessageHandler(FindAllFilter(r"([0-9]+:[a-zA-Z0-9_-]{35})"), regex_token_warning) 184 | ) 185 | 186 | # Tag hints - works with regex 187 | application.add_handler(MessageHandler(TagHintFilter(), tag_hint)) 188 | 189 | # Compat tag hint via regex 190 | application.add_handler(MessageHandler(filters.Regex(COMPAT_ERRORS), compat_warning)) 191 | 192 | # We need several matches so filters.REGEX is basically useless 193 | # therefore we catch everything and do regex ourselves 194 | application.add_handler( 195 | MessageHandler(filters.TEXT & filters.UpdateType.MESSAGES & ~filters.COMMAND, reply_search) 196 | ) 197 | 198 | # Inline Queries 199 | application.add_handler(InlineQueryHandler(inlinequeries.inline_query)) 200 | 201 | # Captcha for userbots 202 | application.add_handler( 203 | CommandHandler( 204 | "say_potato", 205 | say_potato_command, 206 | filters=filters.Chat(username=[ONTOPIC_USERNAME, OFFTOPIC_USERNAME]), 207 | ) 208 | ) 209 | application.add_handler(CallbackQueryHandler(say_potato_button, pattern="^POTATO")) 210 | 211 | # Join requests 212 | application.add_handler(ChatJoinRequestHandler(callback=join_request_callback, block=False)) 213 | application.add_handler(CallbackQueryHandler(join_request_buttons, pattern="^JOIN")) 214 | 215 | # Delete unhandled commands - e.g. for users that like to click on blue text in other messages 216 | application.add_handler(MessageHandler(filters.COMMAND, delete_message)) 217 | 218 | # Status updates 219 | application.add_handler( 220 | MessageHandler( 221 | filters.Chat(username=[ONTOPIC_USERNAME, OFFTOPIC_USERNAME]) 222 | & filters.StatusUpdate.NEW_CHAT_MEMBERS, 223 | delete_message, 224 | block=False, 225 | ), 226 | group=1, 227 | ) 228 | 229 | # Error Handler 230 | application.add_error_handler(error_handler) 231 | 232 | application.run_polling(allowed_updates=Update.ALL_TYPES, close_loop=False) 233 | 234 | 235 | if __name__ == "__main__": 236 | main() 237 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length = 99 3 | ignore = W503, W605 4 | extend-ignore = E203 5 | exclude = setup.py, setup-raw.py docs/source/conf.py, telegram/vendor 6 | 7 | [pylint.message-control] 8 | # We're ignoring a bunch of warnings, b/c rools is no high-end product … 9 | disable = C0116,C0115,R0902,C0114,R0912,R0914,R0913,R0917 10 | 11 | 12 | [mypy] 13 | warn_unused_ignores = True 14 | warn_unused_configs = True 15 | disallow_untyped_defs = True 16 | disallow_incomplete_defs = True 17 | disallow_untyped_decorators = True 18 | show_error_codes = True 19 | implicit_optional = True 20 | --------------------------------------------------------------------------------