Compare commits

...

60 Commits

Author SHA1 Message Date
Hinrich Mahler 82c98b64a7 Bump Version to v20.5 2023-09-03 14:46:28 +02:00
Bibo-Joshi f9ccf560f6 Remove Functionality Deprecated Since Bot API 6.5, 6.6 or 6.7 (#3858) 2023-09-03 14:23:48 +02:00
Bibo-Joshi 40ab8aadca Documentation Improvements (#3803, #3797, #3816, #3829)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Dmitry K <58207913+lemontree210@users.noreply.github.com>
Co-authored-by: Trim21 <trim21.me@gmail.com>
Co-authored-by: aelkheir <90580077+aelkheir@users.noreply.github.com>
2023-09-03 14:13:19 +02:00
Bibo-Joshi 644d76b592 Type Hinting Fixes for WebhookInfo (#3871) 2023-09-03 13:44:44 +02:00
Harshil bd24da29cd API 6.8 (#3853)
Co-authored-by: Aditya <clot27@apx_managed.vanilla>
Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
2023-09-03 13:43:44 +02:00
dependabot[bot] caffb9d66e Bump srvaroa/labeler from 1.6.0 to 1.6.1 (#3870)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com>
2023-09-02 13:01:53 +02:00
dependabot[bot] 1efd8e80ab Bump sphinx from 7.2.3 to 7.2.5 (#3869)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-02 12:35:19 +02:00
dependabot[bot] 8cc55c3381 Update apscheduler requirement from ~=3.10.3 to ~=3.10.4 (#3862)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: dependabot[bot] <dependabot[bot]@users.noreply.github.com>
Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
2023-08-26 11:47:32 +02:00
dependabot[bot] fbe9eeeaf5 Bump sphinx from 7.2.2 to 7.2.3 (#3861)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-26 11:16:29 +02:00
Yao Kuan ebed8ec7d3 Add has_args Parameter to CommandHandler (#3854)
Co-authored-by: Yao Kuan <chan_yao_kuan@tech.gov.sg>
2023-08-23 21:28:36 +02:00
dependabot[bot] 009785f028 Bump sphinx-paramlinks from 0.5.4 to 0.6.0 (#3840) 2023-08-21 18:57:29 +02:00
dependabot[bot] 7c858473ca Update apscheduler requirement from ~=3.10.1 to ~=3.10.3 (#3851)
Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
2023-08-21 18:55:49 +02:00
Harshil 58a109aae6 Remove Version Check from Examples (#3846) 2023-08-21 18:47:05 +02:00
Harshil 71eff4731c Add Support for Python 3.12 RC (#3847) 2023-08-21 18:27:02 +02:00
dependabot[bot] c2c8c53d9c Bump furo from 2023.7.26 to 2023.8.19 (#3850)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
2023-08-19 09:20:34 +02:00
dependabot[bot] 3b78934a77 Bump sphinx from 7.1.2 to 7.2.2 (#3852)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-19 09:01:34 +02:00
Bibo-Joshi 5128748092 Add Application.stop_running() and Improve Marking Updates as Read on Updater.stop() (#3804) 2023-08-17 11:50:26 +02:00
Bibo-Joshi 03f87750d4 Provide Versions of customwebhookbot.py with Different Frameworks (#3820) 2023-08-16 21:15:32 +02:00
dependabot[bot] 4c4bf2185d Bump sphinx from 7.1.1 to 7.1.2 (#3827)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-05 22:06:54 +02:00
pre-commit-ci[bot] aaadc70fcc pre-commit autoupdate (#3824)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
2023-08-02 11:51:17 +02:00
Bibo-Joshi ed3acd4b81 Extend Allowed Values for HTTP Version (#3823) 2023-08-01 16:19:33 +02:00
dependabot[bot] a00ba52114 Bump sphinx from 7.0.1 to 7.1.1 (#3818)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-29 10:49:55 +02:00
dependabot[bot] 0ae991c141 Bump furo from 2023.5.20 to 2023.7.26 (#3817)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-29 10:29:13 +02:00
Bibo-Joshi 7226aaea04 Test and Document Exception.__cause__ on NetworkError (#3792)
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
2023-07-18 12:02:13 +02:00
dependabot[bot] 50a16edb41 Bump pytest-asyncio from 0.21.0 to 0.21.1 (#3801)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-17 22:19:47 +02:00
Hinrich Mahler 59105b240f Bump Version to v20.4 2023-07-09 12:18:32 +02:00
Bibo-Joshi db6030ea83 Documentation Improvements (#3698, #3708, #3767)
Co-authored-by: Iulian Onofrei <5748627+revolter@users.noreply.github.com>
Co-authored-by: Poolitzer <github@poolitzer.eu>
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Aditya <clot27@apx_managed.vanilla>
2023-07-09 11:22:08 +02:00
pre-commit-ci[bot] 7d52ead228 pre-commit autoupdate (#3791)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com>
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
2023-07-06 18:57:51 +02:00
Harshil 589047ddbf Add Python 3.12 Beta to the Test Matrix (#3751) 2023-07-05 21:58:57 +02:00
Bibo-Joshi 5534ddfaa0 Use Temporary Files for Testing File Downloads (#3777) 2023-07-03 10:06:16 +02:00
dependabot[bot] 1d27a0fadb Bump srvaroa/labeler from 1.5.0 to 1.6.0 (#3786)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-02 18:07:30 +02:00
dependabot[bot] 79cda7582e Bump dependabot/fetch-metadata from 1.5.1 to 1.6.0 (#3787)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-02 17:56:58 +02:00
dependabot[bot] dccf62eb1c Bump dessant/lock-threads from 4.0.0 to 4.0.1 (#3785)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-02 17:56:21 +02:00
Bibo-Joshi fb86bb3417 Drop Support for Python 3.7 (#3728, #3742, #3749, #3740, #3754, #3753, #3764, #3762, #3759)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Luca Bellanti <luca.bellanti@gmail.com>
Co-authored-by: thefunkycat <104379699+thefunkycat@users.noreply.github.com>
Co-authored-by: Aditya Yadav <69784758+clot27@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2023-06-29 18:17:47 +02:00
Harshil 58b89cf0e9 Add More ruff Rules (#3763) 2023-06-29 11:38:09 +02:00
Bibo-Joshi 4a6e0fd7a6 Add Quotes for Installation Instructions With Optional Dependencies (#3780) 2023-06-29 07:46:00 +02:00
dependabot[bot] 623d2f7f0b Bump pytest from 7.3.2 to 7.4.0 (#3774)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
2023-06-26 22:14:19 +02:00
Bibo-Joshi 62a8cfc395 Rework pytest Integration into GitHub Actions (#3776) 2023-06-26 19:48:38 +02:00
Harshil b0cff31fc1 Rename _handler.py to _basehandler.py (#3761) 2023-06-25 15:08:26 +02:00
Harshil 3c87e450fb Fix Wrong Warning Text in KeyboardButton.__eq__ (#3768) 2023-06-22 10:21:53 +02:00
dependabot[bot] 8e91a6adba Bump pytest from 7.3.1 to 7.3.2 (#3758) 2023-06-17 09:16:31 +02:00
Dmitry Kolomatskiy 63fd846233 Set httpx Logging Level to Warning in Examples (#3746) 2023-06-07 22:32:04 +02:00
pre-commit-ci[bot] 814c72052f pre-commit autoupdate (#3747)
Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com>
2023-06-07 21:51:40 +02:00
Luca Bellanti 1a7edd7a5d Explicitly set allowed_updates in Examples (#3741) 2023-06-04 17:11:58 +02:00
dependabot[bot] f0e71216fe Update cachetools requirement from ~=5.3.0 to ~=5.3.1 (#3738) 2023-06-03 08:44:14 +02:00
Bibo-Joshi ca37219a68 Fix Two Bugs in GitHub Actions Workflows (#3739) 2023-06-03 08:28:08 +02:00
dependabot[bot] c9636726f7 Bump sphinxcontrib-mermaid from 0.8.1 to 0.9.2 (#3737) 2023-06-03 07:48:17 +02:00
Bibo-Joshi 9c8d6efe7a Make Integration of APScheduler into JobQueue More Explicit (#3695) 2023-06-02 22:17:46 +02:00
Aditya Yadav bf54599618 Introduce BaseUpdateProcessor for Customized Concurrent Handling of Updates (#3654)
Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com>
2023-06-02 18:17:08 +02:00
Bibo-Joshi 4c8d7332db Auto-Update Changed Version in Other Files After Dependabot PRs (#3716) 2023-05-31 22:19:56 +02:00
Harshil c185137c9e Bump furo and sphinx 7 (#3719) 2023-05-25 22:47:05 +02:00
Bibo-Joshi 3c5a16be1c Exclude Type Hints from Stability Policy (#3712) 2023-05-24 21:42:30 +02:00
dependabot[bot] cd25964419 Update httpx requirement from ~=0.24.0 to ~=0.24.1 (#3715)
Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com>
2023-05-20 14:23:40 +02:00
Bibo-Joshi 5309cf6555 Automatically Label pre-commit-ci PRs (#3713) 2023-05-20 10:27:51 +02:00
dependabot[bot] bb8b508a22 Bump pytest-xdist from 3.3.0 to 3.3.1 (#3714)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-05-20 10:15:57 +02:00
Iulian Onofrei 4f15a7feee Fix Inconsistent Type Hints for timeout Parameter of Bot.get_updates (#3709) 2023-05-19 22:09:16 +02:00
dependabot[bot] 57c780c62f Update aiolimiter requirement from ~=1.0.0 to ~=1.1.0 (#3707)
Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com>
2023-05-18 07:59:44 +02:00
MiguelX413 99fd4432db Use Explicit Optionals (#3692)
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2023-05-18 07:57:59 +02:00
dependabot[bot] e432c296a9 Bump pytest-xdist from 3.2.1 to 3.3.0 (#3705) 2023-05-15 18:20:50 +02:00
Bibo-Joshi 1fdccd7bf9 Drop a Legacy pre-commit.ci Configuration (#3697) 2023-05-12 22:36:52 +02:00
273 changed files with 6890 additions and 8724 deletions
+1 -19
View File
@@ -167,6 +167,7 @@ Feel free to copy (parts of) the checklist to the PR description to remind you o
**If the PR contains API changes (otherwise, you can ignore this passage)**
- Checked the Bot API specific sections of the `Stability Policy <https://docs.python-telegram-bot.org/stability_policy.html>`_
- Created a PR to remove functionality deprecated in the previous Bot API release (`see here <https://docs.python-telegram-bot.org/en/stable/stability_policy.html#case-2>`_)
- New classes:
@@ -274,25 +275,6 @@ callable we prefer that the call also uses keyword arg syntax. For example:
This gives us the flexibility to re-order arguments and more importantly
to add new required arguments. It's also more explicit and easier to read.
Properly defining optional arguments
------------------------------------
It's always good to not initialize optional arguments at class creation,
instead use ``**kwargs`` to get them. It's well known Telegram API can
change without notice, in that case if a new argument is added it won't
break the API classes. For example:
.. code-block:: python
# GOOD
def __init__(self, id, name, last_name=None, **kwargs):
self.last_name = last_name
# BAD
def __init__(self, id, name, last_name=None):
self.last_name = last_name
.. _`Code of Conduct`: https://www.python.org/psf/conduct/
.. _`issue tracker`: https://github.com/python-telegram-bot/python-telegram-bot/issues
+9
View File
@@ -0,0 +1,9 @@
# Config file for workflows/labelling.yml
version: 1
labels:
- label: "dependencies"
authors: ["dependabot[bot]", "pre-commit-ci[bot]"]
- label: "code quality ✨"
authors: ["pre-commit-ci[bot]"]
+1 -1
View File
@@ -1,6 +1,6 @@
<!--
Hey! You're PRing? Cool!
Please be sure to check out our contribution guide (https://github.com/python-telegram-bot/python-telegram-bot/blob/master/.github/CONTRIBUTING.rst).
Especially, please have a look at the check list for PRs (https://github.com/python-telegram-bot/python-telegram-bot/blob/master/.github/CONTRIBUTING.rst#checklist-for-prs). Feel free to copy (parts of) the checklist to the PR description to remind you or the maintainers of open points or if you have questions on anything.
Especially, please have a look at the check list for PRs (https://github.com/python-telegram-bot/python-telegram-bot/blob/master/.github/CONTRIBUTING.rst#check-list-for-prs). Feel free to copy (parts of) the checklist to the PR description to remind you or the maintainers of open points or if you have questions on anything.
-->
+38
View File
@@ -0,0 +1,38 @@
name: Process Dependabot PRs
on:
pull_request:
types: [opened, reopened]
jobs:
process-dependabot-prs:
permissions:
pull-requests: read
contents: write
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.user.login == 'dependabot[bot]' }}
steps:
- name: Fetch Dependabot metadata
id: dependabot-metadata
uses: dependabot/fetch-metadata@v1.6.1
- uses: actions/checkout@v3.5.2
with:
ref: ${{ github.event.pull_request.head.ref }}
- name: Update Version Number in Other Files
uses: jacobtomlinson/gha-find-replace@v3
with:
find: ${{ steps.dependabot-metadata.outputs.previous-version }}
replace: ${{ steps.dependabot-metadata.outputs.new-version }}
regex: false
exclude: CHANGES.rst
- name: Commit & Push Changes to PR
uses: EndBug/add-and-commit@v9.1.3
with:
message: 'Update version number in other files'
committer_name: GitHub Actions
committer_email: 41898282+github-actions[bot]@users.noreply.github.com
+17
View File
@@ -0,0 +1,17 @@
name: PR Labeler
on:
pull_request:
types: [opened]
jobs:
pre-commit-ci:
permissions:
contents: read # for srvaroa/labeler to read config file
pull-requests: write # for srvaroa/labeler to add labels in PR
runs-on: ubuntu-latest
steps:
- uses: srvaroa/labeler@v1.6.1
# Config file at .github/labeler.yml
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
+1 -1
View File
@@ -8,7 +8,7 @@ jobs:
lock:
runs-on: ubuntu-latest
steps:
- uses: dessant/lock-threads@v4.0.0
- uses: dessant/lock-threads@v4.0.1
with:
github-token: ${{ github.token }}
issue-inactive-days: '7'
+23 -6
View File
@@ -1,4 +1,4 @@
name: GitHub Actions
name: Unit Tests
on:
pull_request:
branches:
@@ -16,7 +16,7 @@ jobs:
runs-on: ${{matrix.os}}
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12.0-rc.1']
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: False
steps:
@@ -55,7 +55,7 @@ jobs:
TO_TEST="test_no_passport.py or test_datetime.py or test_defaults.py or test_jobqueue.py or test_applicationbuilder.py or test_ratelimiter.py or test_updater.py or test_callbackdatacache.py or test_request.py"
pytest -v --cov -k "${TO_TEST}"
# Rerun only failed tests (--lf), and don't run any tests if none failed (--lfnf=none)
pytest -v --cov --cov-append -k "${TO_TEST}" --lf --lfnf=none
pytest -v --cov --cov-append -k "${TO_TEST}" --lf --lfnf=none --junit-xml=.test_report_no_optionals.xml
# No tests were selected, convert returned status code to 0
opt_dep_status=$(( $? == 5 ? 0 : $? ))
@@ -66,7 +66,7 @@ jobs:
# worker. Increasing number of workers has little effect on test duration, but it seems
# to increase flakyness, specially on python 3.7 with --dist=loadgroup.
pytest -v --cov --cov-append -n auto --dist loadfile
pytest -v --cov --cov-append -n auto --dist loadfile --lf --lfnf=none
pytest -v --cov --cov-append -n auto --dist loadfile --lf --lfnf=none --junit-xml=.test_report_optionals.xml
main_status=$(( $? == 5 ? 0 : $? ))
# exit with non-zero status if any of the two pytest runs failed
exit $(( ${opt_dep_status} || ${main_status} ))
@@ -77,18 +77,28 @@ jobs:
TEST_BUILD: "true"
shell: bash --noprofile --norc {0}
- name: Test Summary
id: test_summary
uses: test-summary/action@v2.1
if: always() # always run, even if tests fail
with:
paths: |
.test_report_no_optionals.xml
.test_report_optionals.xml
- name: Submit coverage
uses: codecov/codecov-action@v3
with:
env_vars: OS,PYTHON
name: ${{ matrix.os }}-${{ matrix.python-version }}
fail_ci_if_error: true
test_official:
name: test-official
runs-on: ${{matrix.os}}
strategy:
matrix:
python-version: [3.7]
python-version: [3.11]
os: [ubuntu-latest]
fail-fast: False
steps:
@@ -105,8 +115,15 @@ jobs:
python -W ignore -m pip install -r requirements-dev.txt
- name: Compare to official api
run: |
pytest -v tests/test_official.py
pytest -v tests/test_official.py --junit-xml=.test_report_official.xml
exit $?
env:
TEST_OFFICIAL: "true"
shell: bash --noprofile --norc {0}
- name: Test Summary
id: test_summary
uses: test-summary/action@v2.1
if: always() # always run, even if tests fail
with:
paths: .test_report_official.xml
+1 -1
View File
@@ -22,7 +22,7 @@ jobs:
cache-dependency-path: '**/requirements*.txt'
- name: Install Pyright
run: |
python -W ignore -m pip install pyright~=1.1.291
python -W ignore -m pip install pyright~=1.1.316
- name: Get PR Completeness
# Must run before base completeness, as base completeness will checkout the base branch
# And we can't go back to the PR branch after that in case the PR is coming from a fork
+21 -24
View File
@@ -3,20 +3,17 @@
ci:
autofix_prs: false
autoupdate_schedule: monthly
# We currently only need this behavior on the v13.x branch were we have the vendored urllib
# TODO: Remove once we discontinue v13
submodules: true
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
args:
- --diff
- --check
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8
- repo: https://github.com/PyCQA/pylint
@@ -31,14 +28,14 @@ repos:
- --jobs=0
additional_dependencies:
- httpx~=0.23.3
- httpx~=0.24.1
- tornado~=6.2
- APScheduler~=3.10.1
- cachetools~=5.3.0
- aiolimiter~=1.0.0
- APScheduler~=3.10.4
- cachetools~=5.3.1
- aiolimiter~=1.1.0
- . # this basically does `pip install -e .`
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.2.0
rev: v1.4.1
hooks:
- id: mypy
name: mypy-ptb
@@ -47,11 +44,11 @@ repos:
- types-pytz
- types-cryptography
- types-cachetools
- httpx~=0.23.3
- httpx~=0.24.1
- tornado~=6.2
- APScheduler~=3.10.1
- cachetools~=5.3.0
- aiolimiter~=1.0.0
- APScheduler~=3.10.4
- cachetools~=5.3.1
- aiolimiter~=1.1.0
- . # this basically does `pip install -e .`
- id: mypy
name: mypy-examples
@@ -61,16 +58,16 @@ repos:
- --follow-imports=silent
additional_dependencies:
- tornado~=6.2
- APScheduler~=3.10.1
- cachetools~=5.3.0
- APScheduler~=3.10.4
- cachetools~=5.3.1
- . # this basically does `pip install -e .`
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.2
rev: v3.10.1
hooks:
- id: pyupgrade
files: ^(telegram|examples|tests|docs)/.*\.py$
args:
- --py37-plus
- --py38-plus
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
@@ -79,15 +76,15 @@ repos:
args:
- --diff
- --check
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.263'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.0.281'
hooks:
- id: ruff
name: ruff
files: ^(telegram|examples|tests)/.*\.py$
additional_dependencies:
- httpx~=0.23.3
- httpx~=0.24.1
- tornado~=6.2
- APScheduler~=3.10.1
- cachetools~=5.3.0
- aiolimiter~=1.0.0
- APScheduler~=3.10.4
- cachetools~=5.3.1
- aiolimiter~=1.1.0
+5
View File
@@ -21,9 +21,11 @@ Contributors
The following wonderful people contributed directly or indirectly to this project:
- `Abdelrahman <https://github.com/aelkheir>`_
- `Abshar <https://github.com/abxhr>`_
- `Alateas <https://github.com/alateas>`_
- `Ales Dokshanin <https://github.com/alesdokshanin>`_
- `Alizia <https://github.com/thefunkycat>`_
- `Ambro17 <https://github.com/Ambro17>`_
- `Andrej Zhilenkov <https://github.com/Andrej730>`_
- `Anton Tagunov <https://github.com/anton-tagunov>`_
@@ -56,6 +58,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Harshil <https://github.com/harshil21>`_
- `Hugo Damer <https://github.com/HakimusGIT>`_
- `ihoru <https://github.com/ihoru>`_
- `Iulian Onofrei <https://github.com/revolter>`_
- `Jasmin Bom <https://github.com/jsmnbom>`_
- `JASON0916 <https://github.com/JASON0916>`_
- `jeffffc <https://github.com/jeffffc>`_
@@ -77,6 +80,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Matheus Lemos <https://github.com/mlemosf>`_
- `Michael Dix <https://github.com/Eisberge>`_
- `Michael Elovskikh <https://github.com/wronglink>`_
- `Miguel C. R. <https://github.com/MiguelX413>`_
- `miles <https://github.com/miles170>`_
- `Mischa Krüger <https://github.com/Makman2>`_
- `naveenvhegde <https://github.com/naveenvhegde>`_
@@ -117,6 +121,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Wagner Macedo <https://github.com/wagnerluis1982>`_
- `wjt <https://github.com/wjt>`_
- `Yaw Danso <https://github.com/dglitxh>`_
- `Yao Kuan <https://github.com/thatguylah>`_
- `zeroone2numeral2 <https://github.com/zeroone2numeral2>`_
- `zeshuaro <https://github.com/zeshuaro>`_
- `zpavloudis <https://github.com/zpavloudis>`_
+772 -1430
View File
File diff suppressed because it is too large Load Diff
+17 -17
View File
@@ -14,7 +14,7 @@
:target: https://pypi.org/project/python-telegram-bot/
:alt: Supported Python versions
.. image:: https://img.shields.io/badge/Bot%20API-6.7-blue?logo=telegram
.. image:: https://img.shields.io/badge/Bot%20API-6.8-blue?logo=telegram
:target: https://core.telegram.org/bots/api-changelog
:alt: Supported Bot API versions
@@ -30,7 +30,7 @@
:target: https://www.gnu.org/licenses/lgpl-3.0.html
:alt: LGPLv3 License
.. image:: https://github.com/python-telegram-bot/python-telegram-bot/workflows/GitHub%20Actions/badge.svg
.. image:: https://github.com/python-telegram-bot/python-telegram-bot/workflows/Unit%20Tests/badge.svg
:target: https://github.com/python-telegram-bot/python-telegram-bot/
:alt: Github Actions workflow
@@ -77,7 +77,7 @@ Introduction
This library provides a pure Python, asynchronous interface for the
`Telegram Bot API <https://core.telegram.org/bots/api>`_.
It's compatible with Python versions **3.7+**.
It's compatible with Python versions **3.8+**.
In addition to the pure API implementation, this library features a number of high-level classes to
make the development of bots easy and straightforward. These classes are contained in the
@@ -93,7 +93,7 @@ Installing both ``python-telegram-bot`` and ``python-telegram-bot-raw`` in conju
Telegram API support
====================
All types and methods of the Telegram Bot API **6.7** are supported.
All types and methods of the Telegram Bot API **6.8** are supported.
Installing
==========
@@ -135,7 +135,7 @@ As these features are *optional*, the corresponding 3rd party dependencies are n
Instead, they are listed as optional dependencies.
This allows to avoid unnecessary dependency conflicts for users who don't need the optional features.
The only required dependency is `httpx ~= 0.23.3 <https://www.python-httpx.org>`_ for
The only required dependency is `httpx ~= 0.24.1 <https://www.python-httpx.org>`_ for
``telegram.request.HTTPXRequest``, the default networking backend.
``python-telegram-bot`` is most useful when used along with additional libraries.
@@ -148,26 +148,26 @@ Optional Dependencies
PTB can be installed with optional dependencies:
* ``pip install python-telegram-bot[passport]`` installs the `cryptography>=39.0.1 <https://cryptography.io/en/stable>`_ library. Use this, if you want to use Telegram Passport related functionality.
* ``pip install python-telegram-bot[socks]`` installs `httpx[socks] <https://www.python-httpx.org/#dependencies>`_. Use this, if you want to work behind a Socks5 server.
* ``pip install python-telegram-bot[http2]`` installs `httpx[http2] <https://www.python-httpx.org/#dependencies>`_. Use this, if you want to use HTTP/2.
* ``pip install python-telegram-bot[rate-limiter]`` installs `aiolimiter~=1.0.0 <https://aiolimiter.readthedocs.io/en/stable/>`_. Use this, if you want to use ``telegram.ext.AIORateLimiter``.
* ``pip install python-telegram-bot[webhooks]`` installs the `tornado~=6.2 <https://www.tornadoweb.org/en/stable/>`_ library. Use this, if you want to use ``telegram.ext.Updater.start_webhook``/``telegram.ext.Application.run_webhook``.
* ``pip install python-telegram-bot[callback-data]`` installs the `cachetools~=5.3.0 <https://cachetools.readthedocs.io/en/latest/>`_ library. Use this, if you want to use `arbitrary callback_data <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Arbitrary-callback_data>`_.
* ``pip install python-telegram-bot[job-queue]`` installs the `APScheduler~=3.10.1 <https://apscheduler.readthedocs.io/en/3.x/>`_ library and enforces `pytz>=2018.6 <https://pypi.org/project/pytz/>`_, where ``pytz`` is a dependency of ``APScheduler``. Use this, if you want to use the ``telegram.ext.JobQueue``.
* ``pip install "python-telegram-bot[passport]"`` installs the `cryptography>=39.0.1 <https://cryptography.io/en/stable>`_ library. Use this, if you want to use Telegram Passport related functionality.
* ``pip install "python-telegram-bot[socks]"`` installs `httpx[socks] <https://www.python-httpx.org/#dependencies>`_. Use this, if you want to work behind a Socks5 server.
* ``pip install "python-telegram-bot[http2]"`` installs `httpx[http2] <https://www.python-httpx.org/#dependencies>`_. Use this, if you want to use HTTP/2.
* ``pip install "python-telegram-bot[rate-limiter]"`` installs `aiolimiter~=1.1.0 <https://aiolimiter.readthedocs.io/en/stable/>`_. Use this, if you want to use ``telegram.ext.AIORateLimiter``.
* ``pip install "python-telegram-bot[webhooks]"`` installs the `tornado~=6.2 <https://www.tornadoweb.org/en/stable/>`_ library. Use this, if you want to use ``telegram.ext.Updater.start_webhook``/``telegram.ext.Application.run_webhook``.
* ``pip install "python-telegram-bot[callback-data]"`` installs the `cachetools~=5.3.1 <https://cachetools.readthedocs.io/en/latest/>`_ library. Use this, if you want to use `arbitrary callback_data <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Arbitrary-callback_data>`_.
* ``pip install "python-telegram-bot[job-queue]"`` installs the `APScheduler~=3.10.4 <https://apscheduler.readthedocs.io/en/3.x/>`_ library and enforces `pytz>=2018.6 <https://pypi.org/project/pytz/>`_, where ``pytz`` is a dependency of ``APScheduler``. Use this, if you want to use the ``telegram.ext.JobQueue``.
To install multiple optional dependencies, separate them by commas, e.g. ``pip install python-telegram-bot[socks,webhooks]``.
To install multiple optional dependencies, separate them by commas, e.g. ``pip install "python-telegram-bot[socks,webhooks]"``.
Additionally, two shortcuts are provided:
* ``pip install python-telegram-bot[all]`` installs all optional dependencies.
* ``pip install python-telegram-bot[ext]`` installs all optional dependencies that are related to ``telegram.ext``, i.e. ``[rate-limiter, webhooks, callback-data, job-queue]``.
* ``pip install "python-telegram-bot[all]"`` installs all optional dependencies.
* ``pip install "python-telegram-bot[ext]"`` installs all optional dependencies that are related to ``telegram.ext``, i.e. ``[rate-limiter, webhooks, callback-data, job-queue]``.
Quick Start
===========
Our Wiki contains an `Introduction to the API <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Introduction-to-the-API>`_ explaining how the pure Bot API can be accessed via ``python-telegram-bot``.
Moreover, the `Tutorial: Your first Bot <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-Your-first-Bot>`_ gives an introduction on how chatbots can be easily programmed with the help of the ``telegram.ext`` module.
Moreover, the `Tutorial: Your first Bot <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions---Your-first-Bot>`_ gives an introduction on how chatbots can be easily programmed with the help of the ``telegram.ext`` module.
Resources
=========
@@ -209,7 +209,7 @@ Contributing
Contributions of all sizes are welcome.
Please review our `contribution guidelines <https://github.com/python-telegram-bot/python-telegram-bot/blob/master/.github/CONTRIBUTING.rst>`_ to get started.
You can also help by `reporting bugs or feature requests <https://github.com/python-telegram-bot/python-telegram-bot/issues/new>`_.
You can also help by `reporting bugs or feature requests <https://github.com/python-telegram-bot/python-telegram-bot/issues/new/choose>`_.
Donating
========
+11 -11
View File
@@ -14,7 +14,7 @@
:target: https://pypi.org/project/python-telegram-bot-raw/
:alt: Supported Python versions
.. image:: https://img.shields.io/badge/Bot%20API-6.7-blue?logo=telegram
.. image:: https://img.shields.io/badge/Bot%20API-6.8-blue?logo=telegram
:target: https://core.telegram.org/bots/api-changelog
:alt: Supported Bot API versions
@@ -30,7 +30,7 @@
:target: https://www.gnu.org/licenses/lgpl-3.0.html
:alt: LGPLv3 License
.. image:: https://github.com/python-telegram-bot/python-telegram-bot/workflows/GitHub%20Actions/badge.svg
.. image:: https://github.com/python-telegram-bot/python-telegram-bot/workflows/Unit%20Tests/badge.svg
:target: https://github.com/python-telegram-bot/python-telegram-bot/
:alt: Github Actions workflow
@@ -77,7 +77,7 @@ Introduction
This library provides a pure Python, asynchronous interface for the
`Telegram Bot API <https://core.telegram.org/bots/api>`_.
It's compatible with Python versions **3.7+**.
It's compatible with Python versions **3.8+**.
``python-telegram-bot-raw`` is part of the `python-telegram-bot <https://python-telegram-bot.org>`_ ecosystem and provides the pure API functionality extracted from PTB. It therefore does not have independent release schedules, changelogs or documentation.
@@ -89,7 +89,7 @@ Installing both ``python-telegram-bot`` and ``python-telegram-bot-raw`` in conju
Telegram API support
====================
All types and methods of the Telegram Bot API **6.7** are supported.
All types and methods of the Telegram Bot API **6.8** are supported.
Installing
==========
@@ -136,7 +136,7 @@ As these features are *optional*, the corresponding 3rd party dependencies are n
Instead, they are listed as optional dependencies.
This allows to avoid unnecessary dependency conflicts for users who don't need the optional features.
The only required dependency is `httpx ~= 0.23.3 <https://www.python-httpx.org>`_ for
The only required dependency is `httpx ~= 0.24.1 <https://www.python-httpx.org>`_ for
``telegram.request.HTTPXRequest``, the default networking backend.
``python-telegram-bot`` is most useful when used along with additional libraries.
@@ -149,13 +149,13 @@ Optional Dependencies
PTB can be installed with optional dependencies:
* ``pip install python-telegram-bot-raw[passport]`` installs the `cryptography>=39.0.1 <https://cryptography.io/en/stable>`_ library. Use this, if you want to use Telegram Passport related functionality.
* ``pip install python-telegram-bot-raw[socks]`` installs `httpx[socks] <https://www.python-httpx.org/#dependencies>`_. Use this, if you want to work behind a Socks5 server.
* ``pip install python-telegram-bot[http2]`` installs `httpx[http2] <https://www.python-httpx.org/#dependencies>`_. Use this, if you want to use HTTP/2.
* ``pip install "python-telegram-bot-raw[passport]"`` installs the `cryptography>=39.0.1 <https://cryptography.io/en/stable>`_ library. Use this, if you want to use Telegram Passport related functionality.
* ``pip install "python-telegram-bot-raw[socks]"`` installs `httpx[socks] <https://www.python-httpx.org/#dependencies>`_. Use this, if you want to work behind a Socks5 server.
* ``pip install "python-telegram-bot-raw[http2]"`` installs `httpx[http2] <https://www.python-httpx.org/#dependencies>`_. Use this, if you want to use HTTP/2.
To install multiple optional dependencies, separate them by commas, e.g. ``pip install python-telegram-bot-raw[passport,socks]``.
To install multiple optional dependencies, separate them by commas, e.g. ``pip install "python-telegram-bot-raw[passport,socks]"``.
Additionally, the shortcut ``pip install python-telegram-bot-raw[all]`` installs all optional dependencies.
Additionally, the shortcut ``pip install "python-telegram-bot-raw[all]"`` installs all optional dependencies.
Quick Start
===========
@@ -195,7 +195,7 @@ Contributing
Contributions of all sizes are welcome.
Please review our `contribution guidelines <https://github.com/python-telegram-bot/python-telegram-bot/blob/master/.github/CONTRIBUTING.rst>`_ to get started.
You can also help by `reporting bugs or feature requests <https://github.com/python-telegram-bot/python-telegram-bot/issues/new>`_.
You can also help by `reporting bugs or feature requests <https://github.com/python-telegram-bot/python-telegram-bot/issues/new/choose>`_.
Donating
========
+6 -3
View File
@@ -174,8 +174,7 @@ class AdmonitionInserter:
break
for line in lines_with_attrs:
line_match = attr_docstr_pattern.match(line)
if not line_match:
if not (line_match := attr_docstr_pattern.match(line)):
continue
target_attr = line_match.group("attr_name")
@@ -529,7 +528,11 @@ class AdmonitionInserter:
# For custom generics like telegram.ext._application.Application[~BT, ~CCT, ~UD...].
# This must come before the check for isinstance(type) because GenericAlias can also be
# recognized as type if it belongs to <class 'types.GenericAlias'>.
elif str(type(arg)) in ("<class 'typing._GenericAlias'>", "<class 'types.GenericAlias'>"):
elif str(type(arg)) in (
"<class 'typing._GenericAlias'>",
"<class 'types.GenericAlias'>",
"<class 'typing._LiteralGenericAlias'>",
):
if "telegram" in str(arg):
# get_origin() of telegram.ext._application.Application[~BT, ~CCT, ~UD...]
# will produce <class 'telegram.ext._application.Application'>
+1 -1
View File
@@ -42,7 +42,7 @@ keyword_args = [
]
write_timeout_sub = [":attr:`~telegram.request.BaseRequest.DEFAULT_NONE`", "``20``"]
read_timeout_sub = [
":attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.",
":attr:`~telegram.request.BaseRequest.DEFAULT_NONE`",
"``2``. :paramref:`timeout` will be added to this value",
]
read_timeout_type = [":obj:`float` | :obj:`None`", ":obj:`float`"]
+4 -2
View File
@@ -189,8 +189,10 @@ def autodoc_process_bases(app, name, obj, option, bases: list):
bases[idx] = f":class:`{base}`"
# Now convert `telegram._message.Message` to `telegram.Message` etc
match = re.search(pattern=r"(telegram(\.ext|))\.[_\w\.]+", string=base)
if not match or "_utils" in base:
if (
not (match := re.search(pattern=r"(telegram(\.ext|))\.[_\w\.]+", string=base))
or "_utils" in base
):
continue
parts = match.group(0).split(".")
+6 -5
View File
@@ -1,7 +1,8 @@
sphinx==6.1.3
sphinx==7.2.5
sphinx-pypi-upload
furo==2023.3.27
git+https://github.com/harshil21/furo-sphinx-search@01efc7be422d7dc02390aab9be68d6f5ce1a5618#egg=furo-sphinx-search
sphinx-paramlinks==0.5.4
sphinxcontrib-mermaid==0.8.1
furo==2023.8.19
git+https://github.com/harshil21/furo-sphinx-search@v0.2.0.1
sphinx-paramlinks==0.6.0
sphinxcontrib-mermaid==0.9.2
sphinx-copybutton==0.5.2
sphinx-inline-tabs==2023.4.21
+8 -3
View File
@@ -21,9 +21,9 @@ author = "Leandro Toledo"
# built documents.
#
# The short X.Y version.
version = "20.3" # telegram.__version__[:3]
version = "20.5" # telegram.__version__[:3]
# The full version, including alpha/beta/rc tags.
release = "20.3" # telegram.__version__
release = "20.5" # telegram.__version__
# If your documentation needs a minimal Sphinx version, state it here.
needs_sphinx = "6.1.3"
@@ -39,12 +39,17 @@ extensions = [
"sphinx.ext.extlinks",
"sphinx_paramlinks",
"sphinx_copybutton",
"sphinx_inline_tabs",
"sphinxcontrib.mermaid",
"sphinx_search.extension",
]
# For shorter links to Wiki in docstrings
extlinks = {"wiki": ("https://github.com/python-telegram-bot/python-telegram-bot/wiki/%s", "%s")}
extlinks = {
"wiki": ("https://github.com/python-telegram-bot/python-telegram-bot/wiki/%s", "%s"),
"pr": ("https://github.com/python-telegram-bot/python-telegram-bot/pull/%s", "#%s"),
"issue": ("https://github.com/python-telegram-bot/python-telegram-bot/issues/%s", "#%s"),
}
# Use intersphinx to reference the python builtin library docs
intersphinx_mapping = {
+39 -3
View File
@@ -1,7 +1,43 @@
``customwebhookbot.py``
=======================
.. literalinclude:: ../../examples/customwebhookbot.py
:language: python
:linenos:
This example is available for different web frameworks.
You can select your preferred framework by opening one of the tabs above the code example.
.. hint::
The following examples show how different Python web frameworks can be used alongside PTB.
This can be useful for two use cases:
1. For extending the functionality of your existing bot to handling updates of external services
2. For extending the functionality of your exisiting web application to also include chat bot functionality
How the PTB and web framework components of the examples below are viewed surely depends on which use case one has in mind.
We are fully aware that a combination of PTB with web frameworks will always mean finding a tradeoff between usability and best practices for both PTB and the web framework and these examples are certainly far from optimal solutions.
Please understand them as starting points and use your expertise of the web framework of your choosing to build up on them.
You are of course also very welcome to help improve these examples!
.. tab:: ``starlette``
.. literalinclude:: ../../examples/customwebhookbot/starlettebot.py
:language: python
:linenos:
.. tab:: ``flask``
.. literalinclude:: ../../examples/customwebhookbot/flaskbot.py
:language: python
:linenos:
.. tab:: ``quart``
.. literalinclude:: ../../examples/customwebhookbot/quartbot.py
:language: python
:linenos:
.. tab:: ``Django``
.. literalinclude:: ../../examples/customwebhookbot/djangobot.py
:language: python
:linenos:
+4 -5
View File
@@ -38,9 +38,8 @@ class to send timed messages. The user sets a timer by using ``/set``
command with a specific time, for example ``/set 30``. The bot then sets
up a job to send a message to that user after 30 seconds. The user can
also cancel the timer by sending ``/unset``. To learn more about the
``JobQueue``, read `this wiki
article <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-JobQueue>`__.
Note: To use ``JobQueue``, you must install PTB via ``pip install python-telegram-bot[job-queue]``
``JobQueue``, read `this wiki article <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions---JobQueue>`__.
Note: To use ``JobQueue``, you must install PTB via ``pip install "python-telegram-bot[job-queue]"``
:any:`examples.conversationbot`
-------------------------------
@@ -116,7 +115,7 @@ Dont forget to enable and configure payments with
`@BotFather <https://telegram.me/BotFather>`_. Check out this
`guide <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Telegram-Passport>`__
on Telegram passports in PTB.
Note: To use Telegram Passport, you must install PTB via ``pip install python-telegram-bot[passport]``
Note: To use Telegram Passport, you must install PTB via ``pip install "python-telegram-bot[passport]"``
:any:`examples.paymentbot`
--------------------------
@@ -164,7 +163,7 @@ combination with ``telegram.ext.Application``.
This example showcases how PTBs “arbitrary callback data” feature can be
used.
Note: To use arbitrary callback data, you must install PTB via ``pip install python-telegram-bot[callback-data]``
Note: To use arbitrary callback data, you must install PTB via ``pip install "python-telegram-bot[callback-data]"``
Pure API
--------
@@ -1,7 +1,9 @@
.. tip::
When combining ``python-telegram-bot`` with other :mod:`asyncio` based frameworks, using this
method is likely not the best choice, as it blocks the event loop until it receives a stop
signal as described above.
Instead, you can manually call the methods listed below to start and shut down the application
and the :attr:`~telegram.ext.Application.updater`.
Keeping the event loop running and listening for a stop signal is then up to you.
* When combining ``python-telegram-bot`` with other :mod:`asyncio` based frameworks, using this
method is likely not the best choice, as it blocks the event loop until it receives a stop
signal as described above.
Instead, you can manually call the methods listed below to start and shut down the application
and the :attr:`~telegram.ext.Application.updater`.
Keeping the event loop running and listening for a stop signal is then up to you.
* To gracefully stop the execution of this method from within a handler, job or error callback,
use :meth:`~telegram.ext.Application.stop_running`.
+3 -1
View File
@@ -237,7 +237,7 @@
- Used for setting the keywords of a sticker
* - :meth:`~telegram.Bot.set_sticker_mask_position`
- Used for setting the mask position of a mask sticker
* - :meth:`~telegram.Bot.set_sticker_set_thumb`
* - :meth:`~telegram.Bot.set_sticker_set_thumbnail`
- Used for setting the thumbnail of a sticker set
* - :meth:`~telegram.Bot.set_custom_emoji_sticker_set_thumbnail`
- Used for setting the thumbnail of a custom emoji sticker set
@@ -328,6 +328,8 @@
- Used to reopen the general topic
* - :meth:`~telegram.Bot.unpin_all_forum_topic_messages`
- Used to unpin all messages in a forum topic
* - :meth:`~telegram.Bot.unpin_all_general_forum_topic_messages`
- Used to unpin all messages in the general forum topic
.. raw:: html
+4 -1
View File
@@ -29,7 +29,7 @@ Objects are in general not guaranteed to be pickleable (unless stated otherwise)
We may provide a way to convert pickled objects from one version to another, but this is not guaranteed.
Functionality that is part of PTBs API but is explicitly documented as not being intended to be used directly by users (e.g. :meth:`telegram.request.BaseRequest.do_request`) may change.
This also applies to functions or attributes marked as final in the sense of `PEP 591 <https://www.python.org/dev/peps/pep-0591/>`__.
This also applies to functions or attributes marked as final in the sense of `PEP 591 <https://peps.python.org/pep-0591/>`__.
PTB has dependencies to third-party packages.
The versions that PTB uses of these third-party packages may change if that does not affect PTBs public API.
@@ -37,6 +37,9 @@ The versions that PTB uses of these third-party packages may change if that does
PTB does not give guarantees about which Python versions are supported.
In general, we will try to support all Python versions that have not yet reached their end of life, but we reserve ourselves the option to drop support for Python versions earlier if that benefits the advancement of the library.
PTB provides static type hints for all public attributes, parameters, return values and generic classes.
These type hints are not covered by this policy and may change at any time under the condition that these changes have no impact on the runtime behavior of PTB.
.. _bot-api-functionality-1:
Bot API Functionality
+1
View File
@@ -79,6 +79,7 @@ Available Types
telegram.replykeyboardmarkup
telegram.replykeyboardremove
telegram.sentwebappmessage
telegram.story
telegram.switchinlinequerychosenchat
telegram.telegramobject
telegram.update
@@ -0,0 +1,6 @@
BaseUpdateProcessor
===================
.. autoclass:: telegram.ext.BaseUpdateProcessor
:members:
:show-inheritance:
+2
View File
@@ -9,12 +9,14 @@ telegram.ext package
telegram.ext.application
telegram.ext.applicationbuilder
telegram.ext.applicationhandlerstop
telegram.ext.baseupdateprocessor
telegram.ext.callbackcontext
telegram.ext.contexttypes
telegram.ext.defaults
telegram.ext.extbot
telegram.ext.job
telegram.ext.jobqueue
telegram.ext.simpleupdateprocessor
telegram.ext.updater
telegram.ext.handlers-tree.rst
telegram.ext.persistence-tree.rst
@@ -0,0 +1,6 @@
SimpleUpdateProcessor
=====================
.. autoclass:: telegram.ext.SimpleUpdateProcessor
:members:
:show-inheritance:
+6
View File
@@ -0,0 +1,6 @@
Story
=====
.. autoclass:: telegram.Story
:members:
:show-inheritance:
+8 -4
View File
@@ -14,10 +14,6 @@
.. |thumbdocstringnopath| replace:: |thumbdocstringbase| |uploadinputnopath|
.. |thumbargumentdeprecation| replace:: As of Bot API 6.6 this argument is deprecated in favor of
.. |thumbattributedeprecation| replace:: As of Bot API 6.6 this attribute is deprecated in favor of
.. |editreplymarkup| replace:: It is currently only possible to edit messages without :attr:`telegram.Message.reply_markup` or with inline keyboards.
.. |toapikwargsbase| replace:: These arguments are also considered by :meth:`~telegram.TelegramObject.to_dict` and :meth:`~telegram.TelegramObject.to_json`, i.e. when passing objects to Telegram. Passing them to Telegram is however not guaranteed to work for all kinds of objects, e.g. this will fail for objects that can not directly be JSON serialized.
@@ -57,3 +53,11 @@
.. |captionentitiesattr| replace:: Tuple of special entities that appear in the caption, which can be specified instead of ``parse_mode``.
.. |datetime_localization| replace:: The default timezone of the bot is used for localization, which is UTC unless :attr:`telegram.ext.Defaults.tzinfo` is used.
.. |post_methods_note| replace:: If you implement custom logic that implies that you will **not** be using :class:`~telegram.ext.Application`'s methods :meth:`~telegram.ext.Application.run_polling` or :meth:`~telegram.ext.Application.run_webhook` to run your application (like it's done in `Custom Webhook Bot Example <https://docs.python-telegram-bot.org/en/stable/examples.customwebhookbot.html>`__), the callback you set in this method **will not be called automatically**. So instead of setting a callback with this method, you have to explicitly ``await`` the function that you want to run at this stage of your application's life (in the `example mentioned above <https://docs.python-telegram-bot.org/en/stable/examples.customwebhookbot.html>`__, that would be in ``async with application`` context manager).
.. |removed_thumb_note| replace:: Removed the deprecated argument and attribute ``thumb``.
.. |removed_thumb_url_note| replace:: Removed the deprecated argument and attribute ``thumb_url``.
.. |removed_thumb_wildcard_note| replace:: Removed the deprecated arguments and attributes ``thumb_*``.
+6 -16
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""This example showcases how PTBs "arbitrary callback data" feature can be used.
@@ -9,24 +9,11 @@ https://github.com/python-telegram-bot/python-telegram-bot/wiki/Arbitrary-callba
Note:
To use arbitrary callback data, you must install PTB via
`pip install python-telegram-bot[callback-data]`
`pip install "python-telegram-bot[callback-data]"`
"""
import logging
from typing import List, Tuple, cast
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import (
Application,
@@ -41,6 +28,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -122,7 +112,7 @@ def main() -> None:
application.add_handler(CallbackQueryHandler(list_button))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+4 -14
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -14,19 +14,6 @@ bot.
import logging
from typing import Optional, Tuple
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import Chat, ChatMember, ChatMemberUpdated, Update
from telegram.constants import ParseMode
from telegram.ext import (
@@ -44,6 +31,9 @@ logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
+11 -16
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -14,19 +14,6 @@ import logging
from collections import defaultdict
from typing import DefaultDict, Optional, Set
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.constants import ParseMode
from telegram.ext import (
@@ -43,6 +30,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -57,7 +47,12 @@ class ChatData:
class CustomContext(CallbackContext[ExtBot, dict, ChatData, dict]):
"""Custom class for context."""
def __init__(self, application: Application, chat_id: int = None, user_id: int = None):
def __init__(
self,
application: Application,
chat_id: Optional[int] = None,
user_id: Optional[int] = None,
):
super().__init__(application=application, chat_id=chat_id, user_id=user_id)
self._message_id: Optional[int] = None
@@ -142,7 +137,7 @@ def main() -> None:
application.add_handler(CallbackQueryHandler(count_click))
application.add_handler(CommandHandler("print_users", print_users))
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -16,19 +16,6 @@ bot.
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 5):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,
@@ -43,6 +30,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
GENDER, PHOTO, LOCATION, BIO = range(4)
@@ -169,7 +159,7 @@ def main() -> None:
application.add_handler(conv_handler)
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -17,19 +17,6 @@ bot.
import logging
from typing import Dict
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,
@@ -44,6 +31,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
CHOOSING, TYPING_REPLY, TYPING_CHOICE = range(3)
@@ -157,7 +147,7 @@ def main() -> None:
application.add_handler(conv_handler)
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+179
View File
@@ -0,0 +1,179 @@
#!/usr/bin/env python
# This program is dedicated to the public domain under the CC0 license.
# pylint: disable=import-error,unused-argument
"""
Simple example of a bot that uses a custom webhook setup and handles custom updates.
For the custom webhook setup, the libraries `Django` and `uvicorn` are used. Please
install them as `pip install Django~=4.2.4 uvicorn~=0.23.2`.
Note that any other `asyncio` based web server framework can be used for a custom webhook setup
just as well.
Usage:
Set bot Token, URL, admin CHAT_ID and PORT after the imports.
You may also need to change the `listen` value in the uvicorn configuration to match your setup.
Press Ctrl-C on the command line or send a signal to the process to stop the bot.
"""
import asyncio
import html
import json
import logging
from dataclasses import dataclass
from uuid import uuid4
import uvicorn
from django.conf import settings
from django.core.asgi import get_asgi_application
from django.http import HttpRequest, HttpResponse, HttpResponseBadRequest
from django.urls import path
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import (
Application,
CallbackContext,
CommandHandler,
ContextTypes,
ExtBot,
TypeHandler,
)
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Define configuration constants
URL = "https://domain.tld"
ADMIN_CHAT_ID = 123456
PORT = 8000
TOKEN = "123:ABC" # nosec B105
@dataclass
class WebhookUpdate:
"""Simple dataclass to wrap a custom update type"""
user_id: int
payload: str
class CustomContext(CallbackContext[ExtBot, dict, dict, dict]):
"""
Custom CallbackContext class that makes `user_data` available for updates of type
`WebhookUpdate`.
"""
@classmethod
def from_update(
cls,
update: object,
application: "Application",
) -> "CustomContext":
if isinstance(update, WebhookUpdate):
return cls(application=application, user_id=update.user_id)
return super().from_update(update, application)
async def start(update: Update, context: CustomContext) -> None:
"""Display a message with instructions on how to use this bot."""
payload_url = html.escape(f"{URL}/submitpayload?user_id=<your user id>&payload=<payload>")
text = (
f"To check if the bot is still running, call <code>{URL}/healthcheck</code>.\n\n"
f"To post a custom update, call <code>{payload_url}</code>."
)
await update.message.reply_html(text=text)
async def webhook_update(update: WebhookUpdate, context: CustomContext) -> None:
"""Handle custom updates."""
chat_member = await context.bot.get_chat_member(chat_id=update.user_id, user_id=update.user_id)
payloads = context.user_data.setdefault("payloads", [])
payloads.append(update.payload)
combined_payloads = "</code>\n• <code>".join(payloads)
text = (
f"The user {chat_member.user.mention_html()} has sent a new payload. "
f"So far they have sent the following payloads: \n\n• <code>{combined_payloads}</code>"
)
await context.bot.send_message(chat_id=ADMIN_CHAT_ID, text=text, parse_mode=ParseMode.HTML)
async def telegram(request: HttpRequest) -> HttpResponse:
"""Handle incoming Telegram updates by putting them into the `update_queue`"""
await ptb_application.update_queue.put(
Update.de_json(data=json.loads(request.body), bot=ptb_application.bot)
)
return HttpResponse()
async def custom_updates(request: HttpRequest) -> HttpResponse:
"""
Handle incoming webhook updates by also putting them into the `update_queue` if
the required parameters were passed correctly.
"""
try:
user_id = int(request.GET["user_id"])
payload = request.GET["payload"]
except KeyError:
return HttpResponseBadRequest(
"Please pass both `user_id` and `payload` as query parameters.",
)
except ValueError:
return HttpResponseBadRequest("The `user_id` must be a string!")
await ptb_application.update_queue.put(WebhookUpdate(user_id=user_id, payload=payload))
return HttpResponse()
async def health(_: HttpRequest) -> HttpResponse:
"""For the health endpoint, reply with a simple plain text message."""
return HttpResponse("The bot is still running fine :)")
# Set up PTB application and a web application for handling the incoming requests.
context_types = ContextTypes(context=CustomContext)
# Here we set updater to None because we want our custom webhook server to handle the updates
# and hence we don't need an Updater instance
ptb_application = (
Application.builder().token(TOKEN).updater(None).context_types(context_types).build()
)
# register handlers
ptb_application.add_handler(CommandHandler("start", start))
ptb_application.add_handler(TypeHandler(type=WebhookUpdate, callback=webhook_update))
urlpatterns = [
path("telegram", telegram, name="Telegram updates"),
path("submitpayload", custom_updates, name="custom updates"),
path("healthcheck", health, name="health check"),
]
settings.configure(ROOT_URLCONF=__name__, SECRET_KEY=uuid4().hex)
async def main() -> None:
"""Finalize configuration and run the applications."""
webserver = uvicorn.Server(
config=uvicorn.Config(
app=get_asgi_application(),
port=PORT,
use_colors=False,
host="127.0.0.1",
)
)
# Pass webhook settings to telegram
await ptb_application.bot.set_webhook(url=f"{URL}/telegram", allowed_updates=Update.ALL_TYPES)
# Run application and webserver together
async with ptb_application:
await ptb_application.start()
await webserver.serve()
await ptb_application.stop()
if __name__ == "__main__":
asyncio.run(main())
+170
View File
@@ -0,0 +1,170 @@
#!/usr/bin/env python
# This program is dedicated to the public domain under the CC0 license.
# pylint: disable=import-error,unused-argument
"""
Simple example of a bot that uses a custom webhook setup and handles custom updates.
For the custom webhook setup, the libraries `flask`, `asgiref` and `uvicorn` are used. Please
install them as `pip install flask[async]~=2.3.2 uvicorn~=0.23.2 asgiref~=3.7.2`.
Note that any other `asyncio` based web server framework can be used for a custom webhook setup
just as well.
Usage:
Set bot Token, URL, admin CHAT_ID and PORT after the imports.
You may also need to change the `listen` value in the uvicorn configuration to match your setup.
Press Ctrl-C on the command line or send a signal to the process to stop the bot.
"""
import asyncio
import html
import logging
from dataclasses import dataclass
from http import HTTPStatus
import uvicorn
from asgiref.wsgi import WsgiToAsgi
from flask import Flask, Response, abort, make_response, request
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import (
Application,
CallbackContext,
CommandHandler,
ContextTypes,
ExtBot,
TypeHandler,
)
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Define configuration constants
URL = "https://domain.tld"
ADMIN_CHAT_ID = 123456
PORT = 8000
TOKEN = "123:ABC" # nosec B105
@dataclass
class WebhookUpdate:
"""Simple dataclass to wrap a custom update type"""
user_id: int
payload: str
class CustomContext(CallbackContext[ExtBot, dict, dict, dict]):
"""
Custom CallbackContext class that makes `user_data` available for updates of type
`WebhookUpdate`.
"""
@classmethod
def from_update(
cls,
update: object,
application: "Application",
) -> "CustomContext":
if isinstance(update, WebhookUpdate):
return cls(application=application, user_id=update.user_id)
return super().from_update(update, application)
async def start(update: Update, context: CustomContext) -> None:
"""Display a message with instructions on how to use this bot."""
payload_url = html.escape(f"{URL}/submitpayload?user_id=<your user id>&payload=<payload>")
text = (
f"To check if the bot is still running, call <code>{URL}/healthcheck</code>.\n\n"
f"To post a custom update, call <code>{payload_url}</code>."
)
await update.message.reply_html(text=text)
async def webhook_update(update: WebhookUpdate, context: CustomContext) -> None:
"""Handle custom updates."""
chat_member = await context.bot.get_chat_member(chat_id=update.user_id, user_id=update.user_id)
payloads = context.user_data.setdefault("payloads", [])
payloads.append(update.payload)
combined_payloads = "</code>\n• <code>".join(payloads)
text = (
f"The user {chat_member.user.mention_html()} has sent a new payload. "
f"So far they have sent the following payloads: \n\n• <code>{combined_payloads}</code>"
)
await context.bot.send_message(chat_id=ADMIN_CHAT_ID, text=text, parse_mode=ParseMode.HTML)
async def main() -> None:
"""Set up PTB application and a web application for handling the incoming requests."""
context_types = ContextTypes(context=CustomContext)
# Here we set updater to None because we want our custom webhook server to handle the updates
# and hence we don't need an Updater instance
application = (
Application.builder().token(TOKEN).updater(None).context_types(context_types).build()
)
# register handlers
application.add_handler(CommandHandler("start", start))
application.add_handler(TypeHandler(type=WebhookUpdate, callback=webhook_update))
# Pass webhook settings to telegram
await application.bot.set_webhook(url=f"{URL}/telegram", allowed_updates=Update.ALL_TYPES)
# Set up webserver
flask_app = Flask(__name__)
@flask_app.post("/telegram") # type: ignore[misc]
async def telegram() -> Response:
"""Handle incoming Telegram updates by putting them into the `update_queue`"""
await application.update_queue.put(Update.de_json(data=request.json, bot=application.bot))
return Response(status=HTTPStatus.OK)
@flask_app.route("/submitpayload", methods=["GET", "POST"]) # type: ignore[misc]
async def custom_updates() -> Response:
"""
Handle incoming webhook updates by also putting them into the `update_queue` if
the required parameters were passed correctly.
"""
try:
user_id = int(request.args["user_id"])
payload = request.args["payload"]
except KeyError:
abort(
HTTPStatus.BAD_REQUEST,
"Please pass both `user_id` and `payload` as query parameters.",
)
except ValueError:
abort(HTTPStatus.BAD_REQUEST, "The `user_id` must be a string!")
await application.update_queue.put(WebhookUpdate(user_id=user_id, payload=payload))
return Response(status=HTTPStatus.OK)
@flask_app.get("/healthcheck") # type: ignore[misc]
async def health() -> Response:
"""For the health endpoint, reply with a simple plain text message."""
response = make_response("The bot is still running fine :)", HTTPStatus.OK)
response.mimetype = "text/plain"
return response
webserver = uvicorn.Server(
config=uvicorn.Config(
app=WsgiToAsgi(flask_app),
port=PORT,
use_colors=False,
host="127.0.0.1",
)
)
# Run application and webserver together
async with application:
await application.start()
await webserver.serve()
await application.stop()
if __name__ == "__main__":
asyncio.run(main())
+171
View File
@@ -0,0 +1,171 @@
#!/usr/bin/env python
# This program is dedicated to the public domain under the CC0 license.
# pylint: disable=import-error,unused-argument
"""
Simple example of a bot that uses a custom webhook setup and handles custom updates.
For the custom webhook setup, the libraries `quart` and `uvicorn` are used. Please
install them as `pip install quart~=0.18.4 uvicorn~=0.23.2`.
Note that any other `asyncio` based web server framework can be used for a custom webhook setup
just as well.
Usage:
Set bot Token, URL, admin CHAT_ID and PORT after the imports.
You may also need to change the `listen` value in the uvicorn configuration to match your setup.
Press Ctrl-C on the command line or send a signal to the process to stop the bot.
"""
import asyncio
import html
import logging
from dataclasses import dataclass
from http import HTTPStatus
import uvicorn
from quart import Quart, Response, abort, make_response, request
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import (
Application,
CallbackContext,
CommandHandler,
ContextTypes,
ExtBot,
TypeHandler,
)
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Define configuration constants
URL = "https://domain.tld"
ADMIN_CHAT_ID = 123456
PORT = 8000
TOKEN = "123:ABC" # nosec B105
@dataclass
class WebhookUpdate:
"""Simple dataclass to wrap a custom update type"""
user_id: int
payload: str
class CustomContext(CallbackContext[ExtBot, dict, dict, dict]):
"""
Custom CallbackContext class that makes `user_data` available for updates of type
`WebhookUpdate`.
"""
@classmethod
def from_update(
cls,
update: object,
application: "Application",
) -> "CustomContext":
if isinstance(update, WebhookUpdate):
return cls(application=application, user_id=update.user_id)
return super().from_update(update, application)
async def start(update: Update, context: CustomContext) -> None:
"""Display a message with instructions on how to use this bot."""
payload_url = html.escape(f"{URL}/submitpayload?user_id=<your user id>&payload=<payload>")
text = (
f"To check if the bot is still running, call <code>{URL}/healthcheck</code>.\n\n"
f"To post a custom update, call <code>{payload_url}</code>."
)
await update.message.reply_html(text=text)
async def webhook_update(update: WebhookUpdate, context: CustomContext) -> None:
"""Handle custom updates."""
chat_member = await context.bot.get_chat_member(chat_id=update.user_id, user_id=update.user_id)
payloads = context.user_data.setdefault("payloads", [])
payloads.append(update.payload)
combined_payloads = "</code>\n• <code>".join(payloads)
text = (
f"The user {chat_member.user.mention_html()} has sent a new payload. "
f"So far they have sent the following payloads: \n\n• <code>{combined_payloads}</code>"
)
await context.bot.send_message(chat_id=ADMIN_CHAT_ID, text=text, parse_mode=ParseMode.HTML)
async def main() -> None:
"""Set up PTB application and a web application for handling the incoming requests."""
context_types = ContextTypes(context=CustomContext)
# Here we set updater to None because we want our custom webhook server to handle the updates
# and hence we don't need an Updater instance
application = (
Application.builder().token(TOKEN).updater(None).context_types(context_types).build()
)
# register handlers
application.add_handler(CommandHandler("start", start))
application.add_handler(TypeHandler(type=WebhookUpdate, callback=webhook_update))
# Pass webhook settings to telegram
await application.bot.set_webhook(url=f"{URL}/telegram", allowed_updates=Update.ALL_TYPES)
# Set up webserver
quart_app = Quart(__name__)
@quart_app.post("/telegram") # type: ignore[misc]
async def telegram() -> Response:
"""Handle incoming Telegram updates by putting them into the `update_queue`"""
await application.update_queue.put(
Update.de_json(data=await request.get_json(), bot=application.bot)
)
return Response(status=HTTPStatus.OK)
@quart_app.route("/submitpayload", methods=["GET", "POST"]) # type: ignore[misc]
async def custom_updates() -> Response:
"""
Handle incoming webhook updates by also putting them into the `update_queue` if
the required parameters were passed correctly.
"""
try:
user_id = int(request.args["user_id"])
payload = request.args["payload"]
except KeyError:
abort(
HTTPStatus.BAD_REQUEST,
"Please pass both `user_id` and `payload` as query parameters.",
)
except ValueError:
abort(HTTPStatus.BAD_REQUEST, "The `user_id` must be a string!")
await application.update_queue.put(WebhookUpdate(user_id=user_id, payload=payload))
return Response(status=HTTPStatus.OK)
@quart_app.get("/healthcheck") # type: ignore[misc]
async def health() -> Response:
"""For the health endpoint, reply with a simple plain text message."""
response = await make_response("The bot is still running fine :)", HTTPStatus.OK)
response.mimetype = "text/plain"
return response
webserver = uvicorn.Server(
config=uvicorn.Config(
app=quart_app,
port=PORT,
use_colors=False,
host="127.0.0.1",
)
)
# Run application and webserver together
async with application:
await application.start()
await webserver.serve()
await application.stop()
if __name__ == "__main__":
asyncio.run(main())
@@ -1,15 +1,15 @@
#!/usr/bin/env python
# This program is dedicated to the public domain under the CC0 license.
# pylint: disable=import-error,wrong-import-position
# pylint: disable=import-error,unused-argument
"""
Simple example of a bot that uses a custom webhook setup and handles custom updates.
For the custom webhook setup, the libraries `starlette` and `uvicorn` are used. Please install
them as `pip install starlette~=0.20.0 uvicorn~=0.17.0`.
them as `pip install starlette~=0.20.0 uvicorn~=0.23.2`.
Note that any other `asyncio` based web server framework can be used for a custom webhook setup
just as well.
Usage:
Set bot token, url, admin chat_id and port at the start of the `main` function.
Set bot Token, URL, admin CHAT_ID and PORT after the imports.
You may also need to change the `listen` value in the uvicorn configuration to match your setup.
Press Ctrl-C on the command line or send a signal to the process to stop the bot.
"""
@@ -25,20 +25,6 @@ from starlette.requests import Request
from starlette.responses import PlainTextResponse, Response
from starlette.routing import Route
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import (
@@ -54,8 +40,17 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Define configuration constants
URL = "https://domain.tld"
ADMIN_CHAT_ID = 123456
PORT = 8000
TOKEN = "123:ABC" # nosec B105
@dataclass
class WebhookUpdate:
@@ -84,17 +79,16 @@ class CustomContext(CallbackContext[ExtBot, dict, dict, dict]):
async def start(update: Update, context: CustomContext) -> None:
"""Display a message with instructions on how to use this bot."""
url = context.bot_data["url"]
payload_url = html.escape(f"{url}/submitpayload?user_id=<your user id>&payload=<payload>")
payload_url = html.escape(f"{URL}/submitpayload?user_id=<your user id>&payload=<payload>")
text = (
f"To check if the bot is still running, call <code>{url}/healthcheck</code>.\n\n"
f"To check if the bot is still running, call <code>{URL}/healthcheck</code>.\n\n"
f"To post a custom update, call <code>{payload_url}</code>."
)
await update.message.reply_html(text=text)
async def webhook_update(update: WebhookUpdate, context: CustomContext) -> None:
"""Callback that handles the custom updates."""
"""Handle custom updates."""
chat_member = await context.bot.get_chat_member(chat_id=update.user_id, user_id=update.user_id)
payloads = context.user_data.setdefault("payloads", [])
payloads.append(update.payload)
@@ -103,33 +97,24 @@ async def webhook_update(update: WebhookUpdate, context: CustomContext) -> None:
f"The user {chat_member.user.mention_html()} has sent a new payload. "
f"So far they have sent the following payloads: \n\n• <code>{combined_payloads}</code>"
)
await context.bot.send_message(
chat_id=context.bot_data["admin_chat_id"], text=text, parse_mode=ParseMode.HTML
)
await context.bot.send_message(chat_id=ADMIN_CHAT_ID, text=text, parse_mode=ParseMode.HTML)
async def main() -> None:
"""Set up the application and a custom webserver."""
url = "https://domain.tld"
admin_chat_id = 123456
port = 8000
"""Set up PTB application and a web application for handling the incoming requests."""
context_types = ContextTypes(context=CustomContext)
# Here we set updater to None because we want our custom webhook server to handle the updates
# and hence we don't need an Updater instance
application = (
Application.builder().token("TOKEN").updater(None).context_types(context_types).build()
Application.builder().token(TOKEN).updater(None).context_types(context_types).build()
)
# save the values in `bot_data` such that we may easily access them in the callbacks
application.bot_data["url"] = url
application.bot_data["admin_chat_id"] = admin_chat_id
# register handlers
application.add_handler(CommandHandler("start", start))
application.add_handler(TypeHandler(type=WebhookUpdate, callback=webhook_update))
# Pass webhook settings to telegram
await application.bot.set_webhook(url=f"{url}/telegram")
await application.bot.set_webhook(url=f"{URL}/telegram", allowed_updates=Update.ALL_TYPES)
# Set up webserver
async def telegram(request: Request) -> Response:
@@ -175,7 +160,7 @@ async def main() -> None:
webserver = uvicorn.Server(
config=uvicorn.Config(
app=starlette_app,
port=port,
port=PORT,
use_colors=False,
host="127.0.0.1",
)
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""Bot that explains Telegram's "Deep Linking Parameters" functionality.
@@ -20,19 +20,6 @@ bot.
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, helpers
from telegram.constants import ParseMode
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, filters
@@ -42,6 +29,9 @@ logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Define constants that will allow us to reuse the deep-linking parameters.
@@ -144,7 +134,7 @@ def main() -> None:
application.add_handler(CommandHandler("start", start))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -17,19 +17,6 @@ bot.
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
@@ -37,6 +24,9 @@ from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandl
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -74,7 +64,7 @@ def main() -> None:
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""This is a very simple example on how one could implement a custom error handler."""
@@ -8,19 +8,6 @@ import json
import logging
import traceback
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import Application, CommandHandler, ContextTypes
@@ -29,6 +16,9 @@ from telegram.ext import Application, CommandHandler, ContextTypes
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# This can be your own ID, or one for a developer group/channel.
@@ -90,7 +80,7 @@ def main() -> None:
application.add_error_handler(error_handler)
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+6 -16
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -18,19 +18,6 @@ import logging
from html import escape
from uuid import uuid4
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import InlineQueryResultArticle, InputTextMessageContent, Update
from telegram.constants import ParseMode
from telegram.ext import Application, CommandHandler, ContextTypes, InlineQueryHandler
@@ -39,6 +26,9 @@ from telegram.ext import Application, CommandHandler, ContextTypes, InlineQueryH
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -95,11 +85,11 @@ def main() -> None:
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_command))
# on non command i.e message - echo the message on Telegram
# on inline queries - show corresponding inline results
application.add_handler(InlineQueryHandler(inline_query))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -8,19 +8,6 @@ Basic example for a bot that uses inline keyboards. For an in-depth explanation,
"""
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes
@@ -28,6 +15,9 @@ from telegram.ext import Application, CallbackQueryHandler, CommandHandler, Cont
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -72,7 +62,7 @@ def main() -> None:
application.add_handler(CommandHandler("help", help_command))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""Simple inline keyboard bot with multiple CallbackQueryHandlers.
@@ -16,19 +16,6 @@ Press Ctrl-C on the command line to stop the bot.
"""
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import (
Application,
@@ -42,6 +29,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Stages
@@ -204,7 +194,7 @@ def main() -> None:
application.add_handler(conv_handler)
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -17,19 +17,6 @@ bot.
import logging
from typing import Any, Dict, Tuple
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import (
Application,
@@ -45,6 +32,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# State definitions for top level conversation
@@ -392,7 +382,7 @@ def main() -> None:
application.add_handler(conv_handler)
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+6 -16
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -12,24 +12,11 @@ See https://github.com/python-telegram-bot/python-telegram-bot/wiki/Telegram-Pas
Note:
To use Telegram Passport, you must install PTB via
`pip install python-telegram-bot[passport]`
`pip install "python-telegram-bot[passport]"`
"""
import logging
from pathlib import Path
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 5):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import Update
from telegram.ext import Application, ContextTypes, MessageHandler, filters
@@ -39,6 +26,9 @@ logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -129,7 +119,7 @@ def main() -> None:
application.add_handler(MessageHandler(filters.PASSPORT_DATA, msg))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,24 +1,11 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""Basic example for a bot that can receive payment from user."""
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import LabeledPrice, ShippingOption, Update
from telegram.ext import (
Application,
@@ -34,6 +21,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
PAYMENT_PROVIDER_TOKEN = "PAYMENT_PROVIDER_TOKEN"
@@ -165,7 +155,7 @@ def main() -> None:
)
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -17,19 +17,6 @@ bot.
import logging
from typing import Dict
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,
@@ -45,6 +32,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
CHOOSING, TYPING_REPLY, TYPING_CHOICE = range(3)
@@ -180,7 +170,7 @@ def main() -> None:
application.add_handler(show_data_handler)
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -9,19 +9,6 @@ one the user sends the bot
"""
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import (
KeyboardButton,
KeyboardButtonPollType,
@@ -45,6 +32,9 @@ from telegram.ext import (
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -179,7 +169,7 @@ def main() -> None:
application.add_handler(PollHandler(receive_quiz_answer))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+6 -16
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=wrong-import-position
# pylint: disable=import-error
"""Simple Bot to reply to Telegram messages.
This is built on the API wrapper, see echobot.py to see the same example built
@@ -11,25 +11,15 @@ import contextlib
import logging
from typing import NoReturn
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import Bot
from telegram import Bot, Update
from telegram.error import Forbidden, NetworkError
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -58,7 +48,7 @@ async def main() -> NoReturn:
async def echo(bot: Bot, update_id: int) -> int:
"""Echo the message the user sent."""
# Request updates after the last update_id
updates = await bot.get_updates(offset=update_id, timeout=10)
updates = await bot.get_updates(offset=update_id, timeout=10, allowed_updates=Update.ALL_TYPES)
for update in updates:
next_update_id = update.update_id + 1
+3 -16
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# pylint: disable=unused-argument, import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -19,24 +19,11 @@ bot.
Note:
To use the JobQueue, you must install PTB via
`pip install python-telegram-bot[job-queue]`
`pip install "python-telegram-bot[job-queue]"`
"""
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
@@ -114,7 +101,7 @@ def main() -> None:
application.add_handler(CommandHandler("unset", unset))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+5 -15
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument,wrong-import-position
# pylint: disable=unused-argument,import-error
# This program is dedicated to the public domain under the CC0 license.
"""
@@ -11,19 +11,6 @@ require a bot token.
import json
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove, Update, WebAppInfo
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
@@ -31,6 +18,9 @@ from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandl
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
@@ -70,7 +60,7 @@ def main() -> None:
application.add_handler(MessageHandler(filters.StatusUpdate.WEB_APP_DATA, web_app_data))
# Run the bot until the user presses Ctrl-C
application.run_polling()
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
+7 -4
View File
@@ -1,6 +1,6 @@
[tool.black]
line-length = 99
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']
target-version = ['py38', 'py39', 'py310', 'py311']
[tool.isort] # black config
profile = "black"
@@ -8,11 +8,14 @@ line_length = 99
[tool.ruff]
line-length = 99
target-version = "py37"
target-version = "py38"
show-fixes = true
ignore = ["PLR2004", "PLR0911", "PLR0912", "PLR0913", "PLR0915"]
ignore = ["PLR2004", "PLR0911", "PLR0912", "PLR0913", "PLR0915", "PERF203"]
select = ["E", "F", "I", "PL", "UP", "RUF", "PTH", "C4", "B", "PIE", "SIM", "RET", "RSE",
"G", "ISC", "PT"]
"G", "ISC", "PT", "ASYNC", "TCH", "CPY", "SLOT", "PERF", "PYI"]
[tool.ruff.per-file-ignores]
"tests/*.py" = ["B018"]
"**/__init__.py" = ["CPY001"]
"examples/**.py" = ["CPY001"]
"tests/**.py" = ["RUF012"]
+3 -3
View File
@@ -1,9 +1,9 @@
pre-commit # needed for pre-commit hooks in the git commit command
# For the test suite
pytest==7.3.1
pytest-asyncio==0.21.0 # needed because pytest doesn't come with native support for coroutines as tests
pytest-xdist==3.2.1 # xdist runs tests in parallel
pytest==7.4.0
pytest-asyncio==0.21.1 # needed because pytest doesn't come with native support for coroutines as tests
pytest-xdist==3.3.1 # xdist runs tests in parallel
flaky # Used for flaky tests (flaky decorator)
beautifulsoup4 # used in test_official for parsing tg docs
+3 -3
View File
@@ -13,15 +13,15 @@
httpx[socks] # socks
httpx[http2] # http2
cryptography!=3.4,!=3.4.1,!=3.4.2,!=3.4.3,>=39.0.1 # passport
aiolimiter~=1.0.0 # rate-limiter!ext
aiolimiter~=1.1.0 # rate-limiter!ext
# tornado is rather stable, but let's not allow the next mayor release without prior testing
tornado~=6.2 # webhooks!ext
# Cachetools and APS don't have a strict stability policy.
# Let's be cautious for now.
cachetools~=5.3.0 # callback-data!ext
APScheduler~=3.10.1 # job-queue!ext
cachetools~=5.3.1 # callback-data!ext
APScheduler~=3.10.4 # job-queue!ext
# pytz is required by APS and just needs the lower bound due to #2120
pytz>=2018.6 # job-queue!ext
+1 -1
View File
@@ -6,4 +6,4 @@
# versions and only increase the lower bound if necessary
# httpx has no stable release yet, so let's be cautious for now
httpx ~= 0.24.0
httpx ~= 0.24.1
+9 -2
View File
@@ -64,7 +64,6 @@ disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
show_error_codes = True
implicit_optional = True
# For some files, it's easier to just disable strict-optional all together instead of
# cluttering the code with `# type: ignore`s or stuff like
@@ -79,9 +78,17 @@ warn_unused_ignores = False
[mypy-apscheduler.*]
ignore_missing_imports = True
# uvicorn and starlette are only used for the `customwebhookbot.py` example
# The libs listed below are only used for the `customwebhookbot_*.py` examples
# let's just ignore type checking for them for now
[mypy-uvicorn.*]
ignore_missing_imports = True
[mypy-starlette.*]
ignore_missing_imports = True
[mypy-asgiref.*]
ignore_missing_imports = True
[mypy-flask.*]
ignore_missing_imports = True
[mypy-quart.*]
ignore_missing_imports = True
[mypy-django.*]
ignore_missing_imports = True
+2 -2
View File
@@ -105,13 +105,13 @@ def get_setup_kwargs(raw=False):
"Topic :: Internet",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
python_requires=">=3.7",
python_requires=">=3.8",
)
return kwargs
+2
View File
@@ -170,6 +170,7 @@ __all__ = ( # Keep this alphabetically ordered
"ShippingQuery",
"Sticker",
"StickerSet",
"Story",
"SuccessfulPayment",
"SwitchInlineQueryChosenChat",
"TelegramObject",
@@ -341,6 +342,7 @@ from ._replykeyboardmarkup import ReplyKeyboardMarkup
from ._replykeyboardremove import ReplyKeyboardRemove
from ._sentwebappmessage import SentWebAppMessage
from ._shared import ChatShared, UserShared
from ._story import Story
from ._switchinlinequerychosenchat import SwitchInlineQueryChosenChat
from ._telegramobject import TelegramObject
from ._update import Update
+621 -1049
View File
File diff suppressed because it is too large Load Diff
+6 -6
View File
@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Bot Command."""
from typing import ClassVar
from typing import Final, Optional
from telegram import constants
from telegram._telegramobject import TelegramObject
@@ -52,7 +52,7 @@ class BotCommand(TelegramObject):
__slots__ = ("description", "command")
def __init__(self, command: str, description: str, *, api_kwargs: JSONDict = None):
def __init__(self, command: str, description: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.command: str = command
self.description: str = description
@@ -61,22 +61,22 @@ class BotCommand(TelegramObject):
self._freeze()
MIN_COMMAND: ClassVar[int] = constants.BotCommandLimit.MIN_COMMAND
MIN_COMMAND: Final[int] = constants.BotCommandLimit.MIN_COMMAND
""":const:`telegram.constants.BotCommandLimit.MIN_COMMAND`
.. versionadded:: 20.0
"""
MAX_COMMAND: ClassVar[int] = constants.BotCommandLimit.MAX_COMMAND
MAX_COMMAND: Final[int] = constants.BotCommandLimit.MAX_COMMAND
""":const:`telegram.constants.BotCommandLimit.MAX_COMMAND`
.. versionadded:: 20.0
"""
MIN_DESCRIPTION: ClassVar[int] = constants.BotCommandLimit.MIN_DESCRIPTION
MIN_DESCRIPTION: Final[int] = constants.BotCommandLimit.MIN_DESCRIPTION
""":const:`telegram.constants.BotCommandLimit.MIN_DESCRIPTION`
.. versionadded:: 20.0
"""
MAX_DESCRIPTION: ClassVar[int] = constants.BotCommandLimit.MAX_DESCRIPTION
MAX_DESCRIPTION: Final[int] = constants.BotCommandLimit.MAX_DESCRIPTION
""":const:`telegram.constants.BotCommandLimit.MAX_DESCRIPTION`
.. versionadded:: 20.0
+19 -17
View File
@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
# pylint: disable=redefined-builtin
"""This module contains objects representing Telegram bot command scopes."""
from typing import TYPE_CHECKING, ClassVar, Dict, Optional, Type, Union
from typing import TYPE_CHECKING, Dict, Final, Optional, Type, Union
from telegram import constants
from telegram._telegramobject import TelegramObject
@@ -60,22 +60,22 @@ class BotCommandScope(TelegramObject):
__slots__ = ("type",)
DEFAULT: ClassVar[str] = constants.BotCommandScopeType.DEFAULT
DEFAULT: Final[str] = constants.BotCommandScopeType.DEFAULT
""":const:`telegram.constants.BotCommandScopeType.DEFAULT`"""
ALL_PRIVATE_CHATS: ClassVar[str] = constants.BotCommandScopeType.ALL_PRIVATE_CHATS
ALL_PRIVATE_CHATS: Final[str] = constants.BotCommandScopeType.ALL_PRIVATE_CHATS
""":const:`telegram.constants.BotCommandScopeType.ALL_PRIVATE_CHATS`"""
ALL_GROUP_CHATS: ClassVar[str] = constants.BotCommandScopeType.ALL_GROUP_CHATS
ALL_GROUP_CHATS: Final[str] = constants.BotCommandScopeType.ALL_GROUP_CHATS
""":const:`telegram.constants.BotCommandScopeType.ALL_GROUP_CHATS`"""
ALL_CHAT_ADMINISTRATORS: ClassVar[str] = constants.BotCommandScopeType.ALL_CHAT_ADMINISTRATORS
ALL_CHAT_ADMINISTRATORS: Final[str] = constants.BotCommandScopeType.ALL_CHAT_ADMINISTRATORS
""":const:`telegram.constants.BotCommandScopeType.ALL_CHAT_ADMINISTRATORS`"""
CHAT: ClassVar[str] = constants.BotCommandScopeType.CHAT
CHAT: Final[str] = constants.BotCommandScopeType.CHAT
""":const:`telegram.constants.BotCommandScopeType.CHAT`"""
CHAT_ADMINISTRATORS: ClassVar[str] = constants.BotCommandScopeType.CHAT_ADMINISTRATORS
CHAT_ADMINISTRATORS: Final[str] = constants.BotCommandScopeType.CHAT_ADMINISTRATORS
""":const:`telegram.constants.BotCommandScopeType.CHAT_ADMINISTRATORS`"""
CHAT_MEMBER: ClassVar[str] = constants.BotCommandScopeType.CHAT_MEMBER
CHAT_MEMBER: Final[str] = constants.BotCommandScopeType.CHAT_MEMBER
""":const:`telegram.constants.BotCommandScopeType.CHAT_MEMBER`"""
def __init__(self, type: str, *, api_kwargs: JSONDict = None):
def __init__(self, type: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.type: str = type
self._id_attrs = (self.type,)
@@ -100,7 +100,7 @@ class BotCommandScope(TelegramObject):
if not data:
return None
_class_mapping: Dict[str, Type["BotCommandScope"]] = {
_class_mapping: Dict[str, Type[BotCommandScope]] = {
cls.DEFAULT: BotCommandScopeDefault,
cls.ALL_PRIVATE_CHATS: BotCommandScopeAllPrivateChats,
cls.ALL_GROUP_CHATS: BotCommandScopeAllGroupChats,
@@ -128,7 +128,7 @@ class BotCommandScopeDefault(BotCommandScope):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.DEFAULT, api_kwargs=api_kwargs)
self._freeze()
@@ -144,7 +144,7 @@ class BotCommandScopeAllPrivateChats(BotCommandScope):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.ALL_PRIVATE_CHATS, api_kwargs=api_kwargs)
self._freeze()
@@ -159,7 +159,7 @@ class BotCommandScopeAllGroupChats(BotCommandScope):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.ALL_GROUP_CHATS, api_kwargs=api_kwargs)
self._freeze()
@@ -174,7 +174,7 @@ class BotCommandScopeAllChatAdministrators(BotCommandScope):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.ALL_CHAT_ADMINISTRATORS, api_kwargs=api_kwargs)
self._freeze()
@@ -197,7 +197,7 @@ class BotCommandScopeChat(BotCommandScope):
__slots__ = ("chat_id",)
def __init__(self, chat_id: Union[str, int], *, api_kwargs: JSONDict = None):
def __init__(self, chat_id: Union[str, int], *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.CHAT, api_kwargs=api_kwargs)
with self._unfrozen():
self.chat_id: Union[str, int] = (
@@ -224,7 +224,7 @@ class BotCommandScopeChatAdministrators(BotCommandScope):
__slots__ = ("chat_id",)
def __init__(self, chat_id: Union[str, int], *, api_kwargs: JSONDict = None):
def __init__(self, chat_id: Union[str, int], *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.CHAT_ADMINISTRATORS, api_kwargs=api_kwargs)
with self._unfrozen():
self.chat_id: Union[str, int] = (
@@ -254,7 +254,9 @@ class BotCommandScopeChatMember(BotCommandScope):
__slots__ = ("chat_id", "user_id")
def __init__(self, chat_id: Union[str, int], user_id: int, *, api_kwargs: JSONDict = None):
def __init__(
self, chat_id: Union[str, int], user_id: int, *, api_kwargs: Optional[JSONDict] = None
):
super().__init__(type=BotCommandScope.CHAT_MEMBER, api_kwargs=api_kwargs)
with self._unfrozen():
self.chat_id: Union[str, int] = (
+4 -2
View File
@@ -17,6 +17,8 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains two objects that represent a Telegram bots (short) description."""
from typing import Optional
from telegram._telegramobject import TelegramObject
from telegram._utils.types import JSONDict
@@ -39,7 +41,7 @@ class BotDescription(TelegramObject):
__slots__ = ("description",)
def __init__(self, description: str, *, api_kwargs: JSONDict = None):
def __init__(self, description: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.description: str = description
@@ -66,7 +68,7 @@ class BotShortDescription(TelegramObject):
__slots__ = ("short_description",)
def __init__(self, short_description: str, *, api_kwargs: JSONDict = None):
def __init__(self, short_description: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.short_description: str = short_description
+3 -3
View File
@@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represent a Telegram bots name."""
from typing import ClassVar
from typing import Final, Optional
from telegram import constants
from telegram._telegramobject import TelegramObject
@@ -42,7 +42,7 @@ class BotName(TelegramObject):
__slots__ = ("name",)
def __init__(self, name: str, *, api_kwargs: JSONDict = None):
def __init__(self, name: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.name: str = name
@@ -50,5 +50,5 @@ class BotName(TelegramObject):
self._freeze()
MAX_LENGTH: ClassVar[int] = constants.BotNameLimit.MAX_NAME_LENGTH
MAX_LENGTH: Final[int] = constants.BotNameLimit.MAX_NAME_LENGTH
""":const:`telegram.constants.BotNameLimit.MAX_NAME_LENGTH`"""
+45 -45
View File
@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
# pylint: disable=redefined-builtin
"""This module contains an object that represents a Telegram CallbackQuery"""
from typing import TYPE_CHECKING, ClassVar, Optional, Sequence, Tuple, Union
from typing import TYPE_CHECKING, Final, Optional, Sequence, Tuple, Union
from telegram import constants
from telegram._files.location import Location
@@ -118,12 +118,12 @@ class CallbackQuery(TelegramObject):
id: str,
from_user: User,
chat_instance: str,
message: Message = None,
data: str = None,
inline_message_id: str = None,
game_short_name: str = None,
message: Optional[Message] = None,
data: Optional[str] = None,
inline_message_id: Optional[str] = None,
game_short_name: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
@@ -155,16 +155,16 @@ class CallbackQuery(TelegramObject):
async def answer(
self,
text: str = None,
show_alert: bool = None,
url: str = None,
cache_time: int = None,
text: Optional[str] = None,
show_alert: Optional[bool] = None,
url: Optional[str] = None,
cache_time: Optional[int] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
@@ -195,14 +195,14 @@ class CallbackQuery(TelegramObject):
text: str,
parse_mode: ODVInput[str] = DEFAULT_NONE,
disable_web_page_preview: ODVInput[bool] = DEFAULT_NONE,
reply_markup: "InlineKeyboardMarkup" = None,
entities: Sequence["MessageEntity"] = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
entities: Optional[Sequence["MessageEntity"]] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
@@ -253,16 +253,16 @@ class CallbackQuery(TelegramObject):
async def edit_message_caption(
self,
caption: str = None,
reply_markup: "InlineKeyboardMarkup" = None,
caption: Optional[str] = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Sequence["MessageEntity"] = None,
caption_entities: Optional[Sequence["MessageEntity"]] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
@@ -317,7 +317,7 @@ class CallbackQuery(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
@@ -362,13 +362,13 @@ class CallbackQuery(TelegramObject):
async def edit_message_media(
self,
media: "InputMedia",
reply_markup: "InlineKeyboardMarkup" = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
@@ -413,19 +413,19 @@ class CallbackQuery(TelegramObject):
async def edit_message_live_location(
self,
latitude: float = None,
longitude: float = None,
reply_markup: "InlineKeyboardMarkup" = None,
horizontal_accuracy: float = None,
heading: int = None,
proximity_alert_radius: int = None,
latitude: Optional[float] = None,
longitude: Optional[float] = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
horizontal_accuracy: Optional[float] = None,
heading: Optional[int] = None,
proximity_alert_radius: Optional[int] = None,
*,
location: Location = None,
location: Optional[Location] = None,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
@@ -481,13 +481,13 @@ class CallbackQuery(TelegramObject):
async def stop_message_live_location(
self,
reply_markup: "InlineKeyboardMarkup" = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
@@ -533,14 +533,14 @@ class CallbackQuery(TelegramObject):
self,
user_id: Union[int, str],
score: int,
force: bool = None,
disable_edit_message: bool = None,
force: Optional[bool] = None,
disable_edit_message: Optional[bool] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
@@ -595,7 +595,7 @@ class CallbackQuery(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Tuple["GameHighScore", ...]:
"""Shortcut for either::
@@ -643,7 +643,7 @@ class CallbackQuery(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
@@ -671,7 +671,7 @@ class CallbackQuery(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
@@ -699,7 +699,7 @@ class CallbackQuery(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
@@ -722,21 +722,21 @@ class CallbackQuery(TelegramObject):
async def copy_message(
self,
chat_id: Union[int, str],
caption: str = None,
caption: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Sequence["MessageEntity"] = None,
caption_entities: Optional[Sequence["MessageEntity"]] = None,
disable_notification: DVInput[bool] = DEFAULT_NONE,
reply_to_message_id: int = None,
reply_to_message_id: Optional[int] = None,
allow_sending_without_reply: DVInput[bool] = DEFAULT_NONE,
reply_markup: ReplyMarkup = None,
reply_markup: Optional[ReplyMarkup] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
message_thread_id: Optional[int] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> "MessageId":
"""Shortcut for::
@@ -771,7 +771,7 @@ class CallbackQuery(TelegramObject):
message_thread_id=message_thread_id,
)
MAX_ANSWER_TEXT_LENGTH: ClassVar[
MAX_ANSWER_TEXT_LENGTH: Final[
int
] = constants.CallbackQueryLimit.ANSWER_CALLBACK_QUERY_TEXT_LENGTH
"""
+346 -295
View File
File diff suppressed because it is too large Load Diff
+5 -5
View File
@@ -127,12 +127,12 @@ class ChatAdministratorRights(TelegramObject):
can_promote_members: bool,
can_change_info: bool,
can_invite_users: bool,
can_post_messages: bool = None,
can_edit_messages: bool = None,
can_pin_messages: bool = None,
can_manage_topics: bool = None,
can_post_messages: Optional[bool] = None,
can_edit_messages: Optional[bool] = None,
can_pin_messages: Optional[bool] = None,
can_manage_topics: Optional[bool] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> None:
super().__init__(api_kwargs=api_kwargs)
# Required
+5 -5
View File
@@ -118,12 +118,12 @@ class ChatInviteLink(TelegramObject):
creates_join_request: bool,
is_primary: bool,
is_revoked: bool,
expire_date: datetime.datetime = None,
member_limit: int = None,
name: str = None,
pending_join_request_count: int = None,
expire_date: Optional[datetime.datetime] = None,
member_limit: Optional[int] = None,
name: Optional[str] = None,
pending_join_request_count: Optional[int] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
+11 -5
View File
@@ -92,6 +92,12 @@ class ChatJoinRequest(TelegramObject):
invite_link (:class:`telegram.ChatInviteLink`): Optional. Chat invite link that was used
by the user to send the join request.
Note:
When a user joins a *public* group via an invite link, this attribute may not
be present. However, this behavior is undocument and may be subject to change.
See `this GitHub thread <https://github.com/tdlib/telegram-bot-api/issues/428>`_
for some discussion.
"""
__slots__ = ("chat", "from_user", "date", "bio", "invite_link", "user_chat_id")
@@ -102,10 +108,10 @@ class ChatJoinRequest(TelegramObject):
from_user: User,
date: datetime.datetime,
user_chat_id: int,
bio: str = None,
invite_link: ChatInviteLink = None,
bio: Optional[str] = None,
invite_link: Optional[ChatInviteLink] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
@@ -147,7 +153,7 @@ class ChatJoinRequest(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
@@ -179,7 +185,7 @@ class ChatJoinRequest(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
+4 -4
View File
@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a location to which a chat is connected."""
from typing import TYPE_CHECKING, ClassVar, Optional
from typing import TYPE_CHECKING, Final, Optional
from telegram import constants
from telegram._files.location import Location
@@ -57,7 +57,7 @@ class ChatLocation(TelegramObject):
location: Location,
address: str,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.location: Location = location
@@ -79,12 +79,12 @@ class ChatLocation(TelegramObject):
return super().de_json(data=data, bot=bot)
MIN_ADDRESS: ClassVar[int] = constants.LocationLimit.MIN_CHAT_LOCATION_ADDRESS
MIN_ADDRESS: Final[int] = constants.LocationLimit.MIN_CHAT_LOCATION_ADDRESS
""":const:`telegram.constants.LocationLimit.MIN_CHAT_LOCATION_ADDRESS`
.. versionadded:: 20.0
"""
MAX_ADDRESS: ClassVar[int] = constants.LocationLimit.MAX_CHAT_LOCATION_ADDRESS
MAX_ADDRESS: Final[int] = constants.LocationLimit.MAX_CHAT_LOCATION_ADDRESS
""":const:`telegram.constants.LocationLimit.MAX_CHAT_LOCATION_ADDRESS`
.. versionadded:: 20.0
+30 -34
View File
@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram ChatMember."""
import datetime
from typing import TYPE_CHECKING, ClassVar, Dict, Optional, Type
from typing import TYPE_CHECKING, Dict, Final, Optional, Type
from telegram import constants
from telegram._telegramobject import TelegramObject
@@ -74,17 +74,17 @@ class ChatMember(TelegramObject):
__slots__ = ("user", "status")
ADMINISTRATOR: ClassVar[str] = constants.ChatMemberStatus.ADMINISTRATOR
ADMINISTRATOR: Final[str] = constants.ChatMemberStatus.ADMINISTRATOR
""":const:`telegram.constants.ChatMemberStatus.ADMINISTRATOR`"""
OWNER: ClassVar[str] = constants.ChatMemberStatus.OWNER
OWNER: Final[str] = constants.ChatMemberStatus.OWNER
""":const:`telegram.constants.ChatMemberStatus.OWNER`"""
BANNED: ClassVar[str] = constants.ChatMemberStatus.BANNED
BANNED: Final[str] = constants.ChatMemberStatus.BANNED
""":const:`telegram.constants.ChatMemberStatus.BANNED`"""
LEFT: ClassVar[str] = constants.ChatMemberStatus.LEFT
LEFT: Final[str] = constants.ChatMemberStatus.LEFT
""":const:`telegram.constants.ChatMemberStatus.LEFT`"""
MEMBER: ClassVar[str] = constants.ChatMemberStatus.MEMBER
MEMBER: Final[str] = constants.ChatMemberStatus.MEMBER
""":const:`telegram.constants.ChatMemberStatus.MEMBER`"""
RESTRICTED: ClassVar[str] = constants.ChatMemberStatus.RESTRICTED
RESTRICTED: Final[str] = constants.ChatMemberStatus.RESTRICTED
""":const:`telegram.constants.ChatMemberStatus.RESTRICTED`"""
def __init__(
@@ -92,7 +92,7 @@ class ChatMember(TelegramObject):
user: User,
status: str,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required by all subclasses
@@ -111,7 +111,7 @@ class ChatMember(TelegramObject):
if not data:
return None
_class_mapping: Dict[str, Type["ChatMember"]] = {
_class_mapping: Dict[str, Type[ChatMember]] = {
cls.OWNER: ChatMemberOwner,
cls.ADMINISTRATOR: ChatMemberAdministrator,
cls.MEMBER: ChatMemberMember,
@@ -130,6 +130,12 @@ class ChatMember(TelegramObject):
data["until_date"] = from_timestamp(data["until_date"], tzinfo=loc_tzinfo)
# This is a deprecated field that TG still returns for backwards compatibility
# Let's filter it out to speed up the de-json process
if cls is ChatMemberRestricted and data.get("can_send_media_messages") is not None:
api_kwargs = {"can_send_media_messages": data.pop("can_send_media_messages")}
return super()._de_json(data=data, bot=bot, api_kwargs=api_kwargs)
return super().de_json(data=data, bot=bot)
@@ -162,9 +168,9 @@ class ChatMemberOwner(ChatMember):
self,
user: User,
is_anonymous: bool,
custom_title: str = None,
custom_title: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(status=ChatMember.OWNER, user=user, api_kwargs=api_kwargs)
with self._unfrozen():
@@ -295,13 +301,13 @@ class ChatMemberAdministrator(ChatMember):
can_promote_members: bool,
can_change_info: bool,
can_invite_users: bool,
can_post_messages: bool = None,
can_edit_messages: bool = None,
can_pin_messages: bool = None,
can_manage_topics: bool = None,
custom_title: str = None,
can_post_messages: Optional[bool] = None,
can_edit_messages: Optional[bool] = None,
can_pin_messages: Optional[bool] = None,
can_manage_topics: Optional[bool] = None,
custom_title: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(status=ChatMember.ADMINISTRATOR, user=user, api_kwargs=api_kwargs)
with self._unfrozen():
@@ -344,7 +350,7 @@ class ChatMemberMember(ChatMember):
self,
user: User,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(status=ChatMember.MEMBER, user=user, api_kwargs=api_kwargs)
self._freeze()
@@ -360,6 +366,9 @@ class ChatMemberRestricted(ChatMember):
All arguments were made positional and their order was changed.
The argument can_manage_topics was added.
.. versionchanged:: 20.5
Removed deprecated argument and attribute ``can_send_media_messages``.
Args:
user (:class:`telegram.User`): Information about the user.
is_member (:obj:`bool`): :obj:`True`, if the user is a
@@ -372,11 +381,6 @@ class ChatMemberRestricted(ChatMember):
to pin messages; groups and supergroups only.
can_send_messages (:obj:`bool`): :obj:`True`, if the user is allowed
to send text messages, contacts, invoices, locations and venues.
can_send_media_messages (:obj:`bool`): :obj:`True`, if the user is allowed
to send audios, documents, photos, videos, video notes and voice notes.
.. deprecated:: 20.1
Bot API 6.5 replaced this argument with granular media settings.
can_send_polls (:obj:`bool`): :obj:`True`, if the user is allowed
to send polls.
can_send_other_messages (:obj:`bool`): :obj:`True`, if the user is allowed
@@ -427,11 +431,6 @@ class ChatMemberRestricted(ChatMember):
to pin messages; groups and supergroups only.
can_send_messages (:obj:`bool`): :obj:`True`, if the user is allowed
to send text messages, contacts, locations and venues.
can_send_media_messages (:obj:`bool`): :obj:`True`, if the user is allowed
to send audios, documents, photos, videos, video notes and voice notes.
.. deprecated:: 20.1
Bot API 6.5 replaced this attribute with granular media settings.
can_send_polls (:obj:`bool`): :obj:`True`, if the user is allowed
to send polls.
can_send_other_messages (:obj:`bool`): :obj:`True`, if the user is allowed
@@ -476,7 +475,6 @@ class ChatMemberRestricted(ChatMember):
"can_invite_users",
"can_pin_messages",
"can_send_messages",
"can_send_media_messages",
"can_send_polls",
"can_send_other_messages",
"can_add_web_page_previews",
@@ -498,7 +496,6 @@ class ChatMemberRestricted(ChatMember):
can_invite_users: bool,
can_pin_messages: bool,
can_send_messages: bool,
can_send_media_messages: bool,
can_send_polls: bool,
can_send_other_messages: bool,
can_add_web_page_previews: bool,
@@ -511,7 +508,7 @@ class ChatMemberRestricted(ChatMember):
can_send_video_notes: bool,
can_send_voice_notes: bool,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(status=ChatMember.RESTRICTED, user=user, api_kwargs=api_kwargs)
with self._unfrozen():
@@ -520,7 +517,6 @@ class ChatMemberRestricted(ChatMember):
self.can_invite_users: bool = can_invite_users
self.can_pin_messages: bool = can_pin_messages
self.can_send_messages: bool = can_send_messages
self.can_send_media_messages: bool = can_send_media_messages
self.can_send_polls: bool = can_send_polls
self.can_send_other_messages: bool = can_send_other_messages
self.can_add_web_page_previews: bool = can_add_web_page_previews
@@ -556,7 +552,7 @@ class ChatMemberLeft(ChatMember):
self,
user: User,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(status=ChatMember.LEFT, user=user, api_kwargs=api_kwargs)
self._freeze()
@@ -596,7 +592,7 @@ class ChatMemberBanned(ChatMember):
user: User,
until_date: datetime.datetime,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(status=ChatMember.BANNED, user=user, api_kwargs=api_kwargs)
with self._unfrozen():
+3 -3
View File
@@ -100,10 +100,10 @@ class ChatMemberUpdated(TelegramObject):
date: datetime.datetime,
old_chat_member: ChatMember,
new_chat_member: ChatMember,
invite_link: ChatInviteLink = None,
via_chat_folder_invite_link: bool = None,
invite_link: Optional[ChatInviteLink] = None,
via_chat_folder_invite_link: Optional[bool] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
+59 -64
View File
@@ -17,31 +17,36 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram ChatPermission."""
from typing import Optional
from typing import TYPE_CHECKING, Optional
from telegram._telegramobject import TelegramObject
from telegram._utils.types import JSONDict
from telegram._utils.warnings import warn
from telegram.warnings import PTBDeprecationWarning
if TYPE_CHECKING:
from telegram import Bot
class ChatPermissions(TelegramObject):
"""Describes actions that a non-administrator user is allowed to take in a chat.
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`can_send_messages`, :attr:`can_send_media_messages`,
considered equal, if their :attr:`can_send_messages`,
:attr:`can_send_polls`, :attr:`can_send_other_messages`, :attr:`can_add_web_page_previews`,
:attr:`can_change_info`, :attr:`can_invite_users`, :attr:`can_pin_messages`, and
:attr:`can_change_info`, :attr:`can_invite_users`, :attr:`can_pin_messages`,
:attr:`can_send_audios`, :attr:`can_send_documents`, :attr:`can_send_photos`,
:attr:`can_send_videos`, :attr:`can_send_video_notes`, :attr:`can_send_voice_notes`, and
:attr:`can_manage_topics` are equal.
.. versionchanged:: 20.0
:attr:`can_manage_topics` is considered as well when comparing objects of
this type in terms of equality.
.. deprecated:: 20.1
:attr:`can_send_audios`, :attr:`can_send_documents`, :attr:`can_send_photos`,
:attr:`can_send_videos`, :attr:`can_send_video_notes` and :attr:`can_send_voice_notes`
will be considered as well when comparing objects of this type in terms of equality in
V21.
.. versionchanged:: 20.5
* :attr:`can_send_audios`, :attr:`can_send_documents`, :attr:`can_send_photos`,
:attr:`can_send_videos`, :attr:`can_send_video_notes` and :attr:`can_send_voice_notes`
are considered as well when comparing objects of this type in terms of equality.
* Removed deprecated argument and attribute ``can_send_media_messages``.
Note:
Though not stated explicitly in the official docs, Telegram changes not only the
@@ -51,19 +56,11 @@ class ChatPermissions(TelegramObject):
Args:
can_send_messages (:obj:`bool`, optional): :obj:`True`, if the user is allowed to send text
messages, contacts, locations and venues.
can_send_media_messages (:obj:`bool`, optional): :obj:`True`, if the user is allowed to
send audios, documents, photos, videos, video notes and voice notes, implies
:attr:`can_send_messages`.
.. deprecated:: 20.1
Bot API 6.5 replaced this argument with granular media settings.
can_send_polls (:obj:`bool`, optional): :obj:`True`, if the user is allowed to send polls,
implies :attr:`can_send_messages`.
can_send_polls (:obj:`bool`, optional): :obj:`True`, if the user is allowed to send polls.
can_send_other_messages (:obj:`bool`, optional): :obj:`True`, if the user is allowed to
send animations, games, stickers and use inline bots, implies
:attr:`can_send_media_messages`.
send animations, games, stickers and use inline bots.
can_add_web_page_previews (:obj:`bool`, optional): :obj:`True`, if the user is allowed to
add web page previews to their messages, implies :attr:`can_send_media_messages`.
add web page previews to their messages.
can_change_info (:obj:`bool`, optional): :obj:`True`, if the user is allowed to change the
chat title, photo and other settings. Ignored in public supergroups.
can_invite_users (:obj:`bool`, optional): :obj:`True`, if the user is allowed to invite new
@@ -99,19 +96,12 @@ class ChatPermissions(TelegramObject):
Attributes:
can_send_messages (:obj:`bool`): Optional. :obj:`True`, if the user is allowed to send text
messages, contacts, locations and venues.
can_send_media_messages (:obj:`bool`): Optional. :obj:`True`, if the user is allowed to
send audios, documents, photos, videos, video notes and voice notes, implies
:attr:`can_send_messages`.
.. deprecated:: 20.1
Bot API 6.5 replaced this attribute with granular media settings.
can_send_polls (:obj:`bool`): Optional. :obj:`True`, if the user is allowed to send polls,
implies :attr:`can_send_messages`.
can_send_other_messages (:obj:`bool`): Optional. :obj:`True`, if the user is allowed to
send animations, games, stickers and use inline bots, implies
:attr:`can_send_media_messages`.
send animations, games, stickers and use inline bots.
can_add_web_page_previews (:obj:`bool`): Optional. :obj:`True`, if the user is allowed to
add web page previews to their messages, implies :attr:`can_send_media_messages`.
add web page previews to their messages.
can_change_info (:obj:`bool`): Optional. :obj:`True`, if the user is allowed to change the
chat title, photo and other settings. Ignored in public supergroups.
can_invite_users (:obj:`bool`): Optional. :obj:`True`, if the user is allowed to invite
@@ -151,7 +141,6 @@ class ChatPermissions(TelegramObject):
"can_invite_users",
"can_send_polls",
"can_send_messages",
"can_send_media_messages",
"can_change_info",
"can_pin_messages",
"can_add_web_page_previews",
@@ -166,28 +155,26 @@ class ChatPermissions(TelegramObject):
def __init__(
self,
can_send_messages: bool = None,
can_send_media_messages: bool = None,
can_send_polls: bool = None,
can_send_other_messages: bool = None,
can_add_web_page_previews: bool = None,
can_change_info: bool = None,
can_invite_users: bool = None,
can_pin_messages: bool = None,
can_manage_topics: bool = None,
can_send_audios: bool = None,
can_send_documents: bool = None,
can_send_photos: bool = None,
can_send_videos: bool = None,
can_send_video_notes: bool = None,
can_send_voice_notes: bool = None,
can_send_messages: Optional[bool] = None,
can_send_polls: Optional[bool] = None,
can_send_other_messages: Optional[bool] = None,
can_add_web_page_previews: Optional[bool] = None,
can_change_info: Optional[bool] = None,
can_invite_users: Optional[bool] = None,
can_pin_messages: Optional[bool] = None,
can_manage_topics: Optional[bool] = None,
can_send_audios: Optional[bool] = None,
can_send_documents: Optional[bool] = None,
can_send_photos: Optional[bool] = None,
can_send_videos: Optional[bool] = None,
can_send_video_notes: Optional[bool] = None,
can_send_voice_notes: Optional[bool] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
self.can_send_messages: Optional[bool] = can_send_messages
self.can_send_media_messages: Optional[bool] = can_send_media_messages
self.can_send_polls: Optional[bool] = can_send_polls
self.can_send_other_messages: Optional[bool] = can_send_other_messages
self.can_add_web_page_previews: Optional[bool] = can_add_web_page_previews
@@ -204,7 +191,6 @@ class ChatPermissions(TelegramObject):
self._id_attrs = (
self.can_send_messages,
self.can_send_media_messages,
self.can_send_polls,
self.can_send_other_messages,
self.can_add_web_page_previews,
@@ -212,23 +198,16 @@ class ChatPermissions(TelegramObject):
self.can_invite_users,
self.can_pin_messages,
self.can_manage_topics,
self.can_send_audios,
self.can_send_documents,
self.can_send_photos,
self.can_send_videos,
self.can_send_video_notes,
self.can_send_voice_notes,
)
self._freeze()
def __eq__(self, other: object) -> bool:
warn(
"In v21, granular media settings will be considered as well when comparing"
" ChatPermissions instances.",
PTBDeprecationWarning,
stacklevel=2,
)
return super().__eq__(other)
def __hash__(self) -> int:
# Intend: Added so support the own __eq__ function (which otherwise breaks hashing)
return super().__hash__()
@classmethod
def all_permissions(cls) -> "ChatPermissions":
"""
@@ -239,7 +218,7 @@ class ChatPermissions(TelegramObject):
.. versionadded:: 20.0
"""
return cls(*(15 * (True,)))
return cls(*(14 * (True,)))
@classmethod
def no_permissions(cls) -> "ChatPermissions":
@@ -249,4 +228,20 @@ class ChatPermissions(TelegramObject):
.. versionadded:: 20.0
"""
return cls(*(15 * (False,)))
return cls(*(14 * (False,)))
@classmethod
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["ChatPermissions"]:
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)
if not data:
return None
api_kwargs = {}
# This is a deprecated field that TG still returns for backwards compatibility
# Let's filter it out to speed up the de-json process
if data.get("can_send_media_messages") is not None:
api_kwargs["can_send_media_messages"] = data.pop("can_send_media_messages")
return super()._de_json(data=data, bot=bot, api_kwargs=api_kwargs)
+3 -3
View File
@@ -72,10 +72,10 @@ class ChosenInlineResult(TelegramObject):
result_id: str,
from_user: User,
query: str,
location: Location = None,
inline_message_id: str = None,
location: Optional[Location] = None,
inline_message_id: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
+16 -16
View File
@@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Dice."""
from typing import ClassVar, List
from typing import Final, List, Optional
from telegram import constants
from telegram._telegramobject import TelegramObject
@@ -89,7 +89,7 @@ class Dice(TelegramObject):
__slots__ = ("emoji", "value")
def __init__(self, value: int, emoji: str, *, api_kwargs: JSONDict = None):
def __init__(self, value: int, emoji: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.value: int = value
self.emoji: str = emoji
@@ -98,62 +98,62 @@ class Dice(TelegramObject):
self._freeze()
DICE: ClassVar[str] = constants.DiceEmoji.DICE # skipcq: PTC-W0052
DICE: Final[str] = constants.DiceEmoji.DICE # skipcq: PTC-W0052
""":const:`telegram.constants.DiceEmoji.DICE`"""
DARTS: ClassVar[str] = constants.DiceEmoji.DARTS
DARTS: Final[str] = constants.DiceEmoji.DARTS
""":const:`telegram.constants.DiceEmoji.DARTS`"""
BASKETBALL: ClassVar[str] = constants.DiceEmoji.BASKETBALL
BASKETBALL: Final[str] = constants.DiceEmoji.BASKETBALL
""":const:`telegram.constants.DiceEmoji.BASKETBALL`"""
FOOTBALL: ClassVar[str] = constants.DiceEmoji.FOOTBALL
FOOTBALL: Final[str] = constants.DiceEmoji.FOOTBALL
""":const:`telegram.constants.DiceEmoji.FOOTBALL`"""
SLOT_MACHINE: ClassVar[str] = constants.DiceEmoji.SLOT_MACHINE
SLOT_MACHINE: Final[str] = constants.DiceEmoji.SLOT_MACHINE
""":const:`telegram.constants.DiceEmoji.SLOT_MACHINE`"""
BOWLING: ClassVar[str] = constants.DiceEmoji.BOWLING
BOWLING: Final[str] = constants.DiceEmoji.BOWLING
"""
:const:`telegram.constants.DiceEmoji.BOWLING`
.. versionadded:: 13.4
"""
ALL_EMOJI: ClassVar[List[str]] = list(constants.DiceEmoji)
ALL_EMOJI: Final[List[str]] = list(constants.DiceEmoji)
"""List[:obj:`str`]: A list of all available dice emoji."""
MIN_VALUE: ClassVar[int] = constants.DiceLimit.MIN_VALUE
MIN_VALUE: Final[int] = constants.DiceLimit.MIN_VALUE
""":const:`telegram.constants.DiceLimit.MIN_VALUE`
.. versionadded:: 20.0
"""
MAX_VALUE_BOWLING: ClassVar[int] = constants.DiceLimit.MAX_VALUE_BOWLING
MAX_VALUE_BOWLING: Final[int] = constants.DiceLimit.MAX_VALUE_BOWLING
""":const:`telegram.constants.DiceLimit.MAX_VALUE_BOWLING`
.. versionadded:: 20.0
"""
MAX_VALUE_DARTS: ClassVar[int] = constants.DiceLimit.MAX_VALUE_DARTS
MAX_VALUE_DARTS: Final[int] = constants.DiceLimit.MAX_VALUE_DARTS
""":const:`telegram.constants.DiceLimit.MAX_VALUE_DARTS`
.. versionadded:: 20.0
"""
MAX_VALUE_DICE: ClassVar[int] = constants.DiceLimit.MAX_VALUE_DICE
MAX_VALUE_DICE: Final[int] = constants.DiceLimit.MAX_VALUE_DICE
""":const:`telegram.constants.DiceLimit.MAX_VALUE_DICE`
.. versionadded:: 20.0
"""
MAX_VALUE_BASKETBALL: ClassVar[int] = constants.DiceLimit.MAX_VALUE_BASKETBALL
MAX_VALUE_BASKETBALL: Final[int] = constants.DiceLimit.MAX_VALUE_BASKETBALL
""":const:`telegram.constants.DiceLimit.MAX_VALUE_BASKETBALL`
.. versionadded:: 20.0
"""
MAX_VALUE_FOOTBALL: ClassVar[int] = constants.DiceLimit.MAX_VALUE_FOOTBALL
MAX_VALUE_FOOTBALL: Final[int] = constants.DiceLimit.MAX_VALUE_FOOTBALL
""":const:`telegram.constants.DiceLimit.MAX_VALUE_FOOTBALL`
.. versionadded:: 20.0
"""
MAX_VALUE_SLOT_MACHINE: ClassVar[int] = constants.DiceLimit.MAX_VALUE_SLOT_MACHINE
MAX_VALUE_SLOT_MACHINE: Final[int] = constants.DiceLimit.MAX_VALUE_SLOT_MACHINE
""":const:`telegram.constants.DiceLimit.MAX_VALUE_SLOT_MACHINE`
.. versionadded:: 20.0
+3 -3
View File
@@ -56,9 +56,9 @@ class _BaseMedium(TelegramObject):
self,
file_id: str,
file_unique_id: str,
file_size: int = None,
file_size: Optional[int] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
@@ -77,7 +77,7 @@ class _BaseMedium(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> "File":
"""Convenience wrapper over :meth:`telegram.Bot.get_file`
+4 -32
View File
@@ -22,10 +22,6 @@ from typing import TYPE_CHECKING, Optional, Type, TypeVar
from telegram._files._basemedium import _BaseMedium
from telegram._files.photosize import PhotoSize
from telegram._utils.types import JSONDict
from telegram._utils.warnings_transition import (
warn_about_deprecated_arg_return_new_arg,
warn_about_deprecated_attr_in_property,
)
if TYPE_CHECKING:
from telegram import Bot
@@ -48,10 +44,6 @@ class _BaseThumbedMedium(_BaseMedium):
is supposed to be the same over time and for different bots.
Can't be used to download or reuse the file.
file_size (:obj:`int`, optional): File size.
thumb (:class:`telegram.PhotoSize`, optional): Thumbnail as defined by sender.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
thumbnail (:class:`telegram.PhotoSize`, optional): Thumbnail as defined by sender.
.. versionadded:: 20.2
@@ -74,11 +66,10 @@ class _BaseThumbedMedium(_BaseMedium):
self,
file_id: str,
file_unique_id: str,
file_size: int = None,
thumb: PhotoSize = None,
thumbnail: PhotoSize = None,
file_size: Optional[int] = None,
thumbnail: Optional[PhotoSize] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
@@ -87,26 +78,7 @@ class _BaseThumbedMedium(_BaseMedium):
api_kwargs=api_kwargs,
)
self.thumbnail: Optional[PhotoSize] = warn_about_deprecated_arg_return_new_arg(
deprecated_arg=thumb,
new_arg=thumbnail,
deprecated_arg_name="thumb",
new_arg_name="thumbnail",
bot_api_version="6.6",
stacklevel=3,
)
@property
def thumb(self) -> Optional[PhotoSize]:
""":class:`telegram.PhotoSize`: Optional. Thumbnail as defined by sender.
.. deprecated:: 20.2
|thumbattributedeprecation| :attr:`thumbnail`.
"""
warn_about_deprecated_attr_in_property(
deprecated_attr_name="thumb", new_attr_name="thumbnail", bot_api_version="6.6"
)
return self.thumbnail
self.thumbnail: Optional[PhotoSize] = thumbnail
@classmethod
def de_json(
+8 -11
View File
@@ -30,6 +30,9 @@ class Animation(_BaseThumbedMedium):
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`file_unique_id` is equal.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
file_id (:obj:`str`): Identifier for this file, which can be used to download
or reuse the file.
@@ -39,10 +42,6 @@ class Animation(_BaseThumbedMedium):
width (:obj:`int`): Video width as defined by sender.
height (:obj:`int`): Video height as defined by sender.
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
thumb (:class:`telegram.PhotoSize`, optional): Animation thumbnail as defined by sender.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
file_name (:obj:`str`, optional): Original animation filename as defined by sender.
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
file_size (:obj:`int`, optional): File size in bytes.
@@ -79,19 +78,17 @@ class Animation(_BaseThumbedMedium):
width: int,
height: int,
duration: int,
thumb: PhotoSize = None,
file_name: str = None,
mime_type: str = None,
file_size: int = None,
thumbnail: PhotoSize = None,
file_name: Optional[str] = None,
mime_type: Optional[str] = None,
file_size: Optional[int] = None,
thumbnail: Optional[PhotoSize] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
file_unique_id=file_unique_id,
file_size=file_size,
thumb=thumb,
api_kwargs=api_kwargs,
thumbnail=thumbnail,
)
+10 -14
View File
@@ -30,6 +30,9 @@ class Audio(_BaseThumbedMedium):
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`file_unique_id` is equal.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
file_id (:obj:`str`): Identifier for this file, which can be used to download
@@ -43,11 +46,6 @@ class Audio(_BaseThumbedMedium):
file_name (:obj:`str`, optional): Original filename as defined by sender.
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
file_size (:obj:`int`, optional): File size in bytes.
thumb (:class:`telegram.PhotoSize`, optional): Thumbnail of the album cover to
which the music file belongs.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
thumbnail (:class:`telegram.PhotoSize`, optional): Thumbnail of the album cover to
which the music file belongs.
@@ -80,21 +78,19 @@ class Audio(_BaseThumbedMedium):
file_id: str,
file_unique_id: str,
duration: int,
performer: str = None,
title: str = None,
mime_type: str = None,
file_size: int = None,
thumb: PhotoSize = None,
file_name: str = None,
thumbnail: PhotoSize = None,
performer: Optional[str] = None,
title: Optional[str] = None,
mime_type: Optional[str] = None,
file_size: Optional[int] = None,
file_name: Optional[str] = None,
thumbnail: Optional[PhotoSize] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
file_unique_id=file_unique_id,
file_size=file_size,
thumb=thumb,
thumbnail=thumbnail,
api_kwargs=api_kwargs,
)
+6 -6
View File
@@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram ChatPhoto."""
from typing import TYPE_CHECKING, ClassVar
from typing import TYPE_CHECKING, Final, Optional
from telegram import constants
from telegram._telegramobject import TelegramObject
@@ -87,7 +87,7 @@ class ChatPhoto(TelegramObject):
big_file_id: str,
big_file_unique_id: str,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.small_file_id: str = small_file_id
@@ -109,7 +109,7 @@ class ChatPhoto(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> "File":
"""Convenience wrapper over :meth:`telegram.Bot.get_file` for getting the small
(:tg-const:`telegram.ChatPhoto.SIZE_SMALL` x :tg-const:`telegram.ChatPhoto.SIZE_SMALL`)
@@ -140,7 +140,7 @@ class ChatPhoto(TelegramObject):
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> "File":
"""Convenience wrapper over :meth:`telegram.Bot.get_file` for getting the
big (:tg-const:`telegram.ChatPhoto.SIZE_BIG` x :tg-const:`telegram.ChatPhoto.SIZE_BIG`)
@@ -164,12 +164,12 @@ class ChatPhoto(TelegramObject):
api_kwargs=api_kwargs,
)
SIZE_SMALL: ClassVar[int] = constants.ChatPhotoSize.SMALL
SIZE_SMALL: Final[int] = constants.ChatPhotoSize.SMALL
""":const:`telegram.constants.ChatPhotoSize.SMALL`
.. versionadded:: 20.0
"""
SIZE_BIG: ClassVar[int] = constants.ChatPhotoSize.BIG
SIZE_BIG: Final[int] = constants.ChatPhotoSize.BIG
""":const:`telegram.constants.ChatPhotoSize.BIG`
.. versionadded:: 20.0
+4 -4
View File
@@ -51,11 +51,11 @@ class Contact(TelegramObject):
self,
phone_number: str,
first_name: str,
last_name: str = None,
user_id: int = None,
vcard: str = None,
last_name: Optional[str] = None,
user_id: Optional[int] = None,
vcard: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
+8 -11
View File
@@ -31,15 +31,14 @@ class Document(_BaseThumbedMedium):
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`file_unique_id` is equal.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
file_id (:obj:`str`): Identifier for this file, which can be used to download
or reuse the file.
file_unique_id (:obj:`str`): Unique identifier for this file, which is supposed to be
the same over time and for different bots. Can't be used to download or reuse the file.
thumb (:class:`telegram.PhotoSize`, optional): Document thumbnail as defined by sender.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
file_name (:obj:`str`, optional): Original filename as defined by sender.
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
file_size (:obj:`int`, optional): File size in bytes.
@@ -67,19 +66,17 @@ class Document(_BaseThumbedMedium):
self,
file_id: str,
file_unique_id: str,
thumb: PhotoSize = None,
file_name: str = None,
mime_type: str = None,
file_size: int = None,
thumbnail: PhotoSize = None,
file_name: Optional[str] = None,
mime_type: Optional[str] = None,
file_size: Optional[int] = None,
thumbnail: Optional[PhotoSize] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
file_unique_id=file_unique_id,
file_size=file_size,
thumb=thumb,
thumbnail=thumbnail,
api_kwargs=api_kwargs,
)
+6 -6
View File
@@ -85,10 +85,10 @@ class File(TelegramObject):
self,
file_id: str,
file_unique_id: str,
file_size: int = None,
file_path: str = None,
file_size: Optional[int] = None,
file_path: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
@@ -99,7 +99,7 @@ class File(TelegramObject):
self.file_size: Optional[int] = file_size
self.file_path: Optional[str] = file_path
self._credentials: Optional["FileCredentials"] = None
self._credentials: Optional[FileCredentials] = None
self._id_attrs = (self.file_unique_id,)
@@ -119,7 +119,7 @@ class File(TelegramObject):
async def download_to_drive(
self,
custom_path: FilePathInput = None,
custom_path: Optional[FilePathInput] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
@@ -270,7 +270,7 @@ class File(TelegramObject):
async def download_as_bytearray(
self,
buf: bytearray = None,
buf: Optional[bytearray] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
+4 -1
View File
@@ -66,7 +66,10 @@ class InputFile:
__slots__ = ("filename", "attach_name", "input_file_content", "mimetype")
def __init__(
self, obj: Union[IO[bytes], bytes, str], filename: str = None, attach: bool = False
self,
obj: Union[IO[bytes], bytes, str],
filename: Optional[str] = None,
attach: bool = False,
):
if isinstance(obj, bytes):
self.input_file_content: bytes = obj
+72 -150
View File
@@ -31,10 +31,6 @@ from telegram._utils.argumentparsing import parse_sequence_arg
from telegram._utils.defaultvalue import DEFAULT_NONE
from telegram._utils.files import parse_file_input
from telegram._utils.types import FileInput, JSONDict, ODVInput
from telegram._utils.warnings_transition import (
warn_about_deprecated_attr_in_property,
warn_about_thumb_return_thumbnail,
)
from telegram.constants import InputMediaType
MediaType = Union[Animation, Audio, Document, PhotoSize, Video]
@@ -91,11 +87,11 @@ class InputMedia(TelegramObject):
self,
media_type: str,
media: Union[str, InputFile, MediaType],
caption: str = None,
caption_entities: Sequence[MessageEntity] = None,
caption: Optional[str] = None,
caption_entities: Optional[Sequence[MessageEntity]] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.type: str = media_type
@@ -107,11 +103,13 @@ class InputMedia(TelegramObject):
self._freeze()
@staticmethod
def _parse_thumb_input(thumb: Optional[FileInput]) -> Optional[Union[str, InputFile]]:
def _parse_thumbnail_input(thumbnail: Optional[FileInput]) -> Optional[Union[str, InputFile]]:
# We use local_mode=True because we don't have access to the actual setting and want
# things to work in local mode.
return (
parse_file_input(thumb, attach=True, local_mode=True) if thumb is not None else thumb
parse_file_input(thumbnail, attach=True, local_mode=True)
if thumbnail is not None
else thumbnail
)
@@ -125,6 +123,9 @@ class InputMediaAnimation(InputMedia):
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
media (:obj:`str` | :term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | \
:class:`telegram.Animation`): File to send. |fileinputnopath|
@@ -137,14 +138,6 @@ class InputMediaAnimation(InputMedia):
:obj:`tempfile` module.
.. versionadded:: 13.1
thumb (:term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | :obj:`str`, \
optional): |thumbdocstringnopath|
.. versionchanged:: 13.2
Accept :obj:`bytes` as input.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
caption (:obj:`str`, optional): Caption of the animation to be sent,
0-:tg-const:`telegram.constants.MessageLimit.CAPTION_LENGTH` characters
after entities parsing.
@@ -196,18 +189,17 @@ class InputMediaAnimation(InputMedia):
def __init__(
self,
media: Union[FileInput, Animation],
thumb: FileInput = None,
caption: str = None,
caption: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
width: int = None,
height: int = None,
duration: int = None,
caption_entities: Sequence[MessageEntity] = None,
filename: str = None,
has_spoiler: bool = None,
thumbnail: FileInput = None,
width: Optional[int] = None,
height: Optional[int] = None,
duration: Optional[int] = None,
caption_entities: Optional[Sequence[MessageEntity]] = None,
filename: Optional[str] = None,
has_spoiler: Optional[bool] = None,
thumbnail: Optional[FileInput] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
if isinstance(media, Animation):
width = media.width if width is None else width
@@ -219,7 +211,6 @@ class InputMediaAnimation(InputMedia):
# things to work in local mode.
media = parse_file_input(media, filename=filename, attach=True, local_mode=True)
thumbnail = warn_about_thumb_return_thumbnail(deprecated_arg=thumb, new_arg=thumbnail)
super().__init__(
InputMediaType.ANIMATION,
media,
@@ -229,26 +220,14 @@ class InputMediaAnimation(InputMedia):
api_kwargs=api_kwargs,
)
with self._unfrozen():
self.thumbnail: Optional[Union[str, InputFile]] = self._parse_thumb_input(thumbnail)
self.thumbnail: Optional[Union[str, InputFile]] = self._parse_thumbnail_input(
thumbnail
)
self.width: Optional[int] = width
self.height: Optional[int] = height
self.duration: Optional[int] = duration
self.has_spoiler: Optional[bool] = has_spoiler
@property
def thumb(self) -> Optional[Union[str, InputFile]]:
""":class:`telegram.InputFile`: Optional. |thumbdocstringbase|
.. deprecated:: 20.2
|thumbattributedeprecation| :attr:`thumbnail`.
"""
warn_about_deprecated_attr_in_property(
deprecated_attr_name="thumb",
new_attr_name="thumbnail",
bot_api_version="6.6",
)
return self.thumbnail
class InputMediaPhoto(InputMedia):
"""Represents a photo to be sent.
@@ -304,13 +283,13 @@ class InputMediaPhoto(InputMedia):
def __init__(
self,
media: Union[FileInput, PhotoSize],
caption: str = None,
caption: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Sequence[MessageEntity] = None,
filename: str = None,
has_spoiler: bool = None,
caption_entities: Optional[Sequence[MessageEntity]] = None,
filename: Optional[str] = None,
has_spoiler: Optional[bool] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
# We use local_mode=True because we don't have access to the actual setting and want
# things to work in local mode.
@@ -337,9 +316,12 @@ class InputMediaVideo(InputMedia):
* When using a :class:`telegram.Video` for the :attr:`media` attribute, it will take the
width, height and duration from that video, unless otherwise specified with the optional
arguments.
* :paramref:`thumb` will be ignored for small video files, for which Telegram can easily
generate thumbnails. However, this behaviour is undocumented and might be changed
by Telegram.
* :paramref:`thumbnail` will be ignored for small video files, for which Telegram can
easily generate thumbnails. However, this behaviour is undocumented and might be
changed by Telegram.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
media (:obj:`str` | :term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | \
@@ -367,14 +349,6 @@ class InputMediaVideo(InputMedia):
duration (:obj:`int`, optional): Video duration in seconds.
supports_streaming (:obj:`bool`, optional): Pass :obj:`True`, if the uploaded video is
suitable for streaming.
thumb (:term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | :obj:`str`, \
optional): |thumbdocstringnopath|
.. versionchanged:: 13.2
Accept :obj:`bytes` as input.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
has_spoiler (:obj:`bool`, optional): Pass :obj:`True`, if the video needs to be covered
with a spoiler animation.
@@ -423,19 +397,18 @@ class InputMediaVideo(InputMedia):
def __init__(
self,
media: Union[FileInput, Video],
caption: str = None,
width: int = None,
height: int = None,
duration: int = None,
supports_streaming: bool = None,
caption: Optional[str] = None,
width: Optional[int] = None,
height: Optional[int] = None,
duration: Optional[int] = None,
supports_streaming: Optional[bool] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
thumb: FileInput = None,
caption_entities: Sequence[MessageEntity] = None,
filename: str = None,
has_spoiler: bool = None,
thumbnail: FileInput = None,
caption_entities: Optional[Sequence[MessageEntity]] = None,
filename: Optional[str] = None,
has_spoiler: Optional[bool] = None,
thumbnail: Optional[FileInput] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
if isinstance(media, Video):
width = width if width is not None else media.width
@@ -447,7 +420,6 @@ class InputMediaVideo(InputMedia):
# things to work in local mode.
media = parse_file_input(media, filename=filename, attach=True, local_mode=True)
thumbnail = warn_about_thumb_return_thumbnail(deprecated_arg=thumb, new_arg=thumbnail)
super().__init__(
InputMediaType.VIDEO,
media,
@@ -460,24 +432,12 @@ class InputMediaVideo(InputMedia):
self.width: Optional[int] = width
self.height: Optional[int] = height
self.duration: Optional[int] = duration
self.thumbnail: Optional[Union[str, InputFile]] = self._parse_thumb_input(thumbnail)
self.thumbnail: Optional[Union[str, InputFile]] = self._parse_thumbnail_input(
thumbnail
)
self.supports_streaming: Optional[bool] = supports_streaming
self.has_spoiler: Optional[bool] = has_spoiler
@property
def thumb(self) -> Optional[Union[str, InputFile]]:
""":class:`telegram.InputFile`: Optional. |thumbdocstringbase|
.. deprecated:: 20.2
|thumbattributedeprecation| :attr:`thumbnail`.
"""
warn_about_deprecated_attr_in_property(
deprecated_attr_name="thumb",
new_attr_name="thumbnail",
bot_api_version="6.6",
)
return self.thumbnail
class InputMediaAudio(InputMedia):
"""Represents an audio file to be treated as music to be sent.
@@ -489,6 +449,9 @@ class InputMediaAudio(InputMedia):
duration, performer and title from that video, unless otherwise specified with the
optional arguments.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
media (:obj:`str` | :term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | \
:class:`telegram.Audio`): File to send. |fileinputnopath|
@@ -514,14 +477,6 @@ class InputMediaAudio(InputMedia):
performer (:obj:`str`, optional): Performer of the audio as defined by sender or by audio
tags.
title (:obj:`str`, optional): Title of the audio as defined by sender or by audio tags.
thumb (:term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | :obj:`str`, \
optional): |thumbdocstringnopath|
.. versionchanged:: 13.2
Accept :obj:`bytes` as input.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
thumbnail (:term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | :obj:`str`, \
optional): |thumbdocstringnopath|
@@ -555,17 +510,16 @@ class InputMediaAudio(InputMedia):
def __init__(
self,
media: Union[FileInput, Audio],
thumb: FileInput = None,
caption: str = None,
caption: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
duration: int = None,
performer: str = None,
title: str = None,
caption_entities: Sequence[MessageEntity] = None,
filename: str = None,
thumbnail: FileInput = None,
duration: Optional[int] = None,
performer: Optional[str] = None,
title: Optional[str] = None,
caption_entities: Optional[Sequence[MessageEntity]] = None,
filename: Optional[str] = None,
thumbnail: Optional[FileInput] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
if isinstance(media, Audio):
duration = media.duration if duration is None else duration
@@ -577,7 +531,6 @@ class InputMediaAudio(InputMedia):
# things to work in local mode.
media = parse_file_input(media, filename=filename, attach=True, local_mode=True)
thumbnail = warn_about_thumb_return_thumbnail(deprecated_arg=thumb, new_arg=thumbnail)
super().__init__(
InputMediaType.AUDIO,
media,
@@ -587,31 +540,22 @@ class InputMediaAudio(InputMedia):
api_kwargs=api_kwargs,
)
with self._unfrozen():
self.thumbnail: Optional[Union[str, InputFile]] = self._parse_thumb_input(thumbnail)
self.thumbnail: Optional[Union[str, InputFile]] = self._parse_thumbnail_input(
thumbnail
)
self.duration: Optional[int] = duration
self.title: Optional[str] = title
self.performer: Optional[str] = performer
@property
def thumb(self) -> Optional[Union[str, InputFile]]:
""":class:`telegram.InputFile`: Optional. |thumbdocstringbase|
.. deprecated:: 20.2
|thumbattributedeprecation| :attr:`thumbnail`.
"""
warn_about_deprecated_attr_in_property(
deprecated_attr_name="thumb",
new_attr_name="thumbnail",
bot_api_version="6.6",
)
return self.thumbnail
class InputMediaDocument(InputMedia):
"""Represents a general file to be sent.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
media (:obj:`str` | :term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | \
:class:`telegram.Document`): File to send. |fileinputnopath|
@@ -633,14 +577,6 @@ class InputMediaDocument(InputMedia):
.. versionchanged:: 20.0
|sequenceclassargs|
thumb (:term:`file object` | :obj:`bytes` | :class:`pathlib.Path` | :obj:`str`, \
optional): |thumbdocstringnopath|
.. versionchanged:: 13.2
Accept :obj:`bytes` as input.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
disable_content_type_detection (:obj:`bool`, optional): Disables automatic server-side
content type detection for files uploaded using multipart/form-data. Always
:obj:`True`, if the document is sent as part of an album.
@@ -675,21 +611,19 @@ class InputMediaDocument(InputMedia):
def __init__(
self,
media: Union[FileInput, Document],
thumb: FileInput = None,
caption: str = None,
caption: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
disable_content_type_detection: bool = None,
caption_entities: Sequence[MessageEntity] = None,
filename: str = None,
thumbnail: FileInput = None,
disable_content_type_detection: Optional[bool] = None,
caption_entities: Optional[Sequence[MessageEntity]] = None,
filename: Optional[str] = None,
thumbnail: Optional[FileInput] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
# We use local_mode=True because we don't have access to the actual setting and want
# things to work in local mode.
media = parse_file_input(media, Document, filename=filename, attach=True, local_mode=True)
thumbnail = warn_about_thumb_return_thumbnail(deprecated_arg=thumb, new_arg=thumbnail)
super().__init__(
InputMediaType.DOCUMENT,
media,
@@ -699,19 +633,7 @@ class InputMediaDocument(InputMedia):
api_kwargs=api_kwargs,
)
with self._unfrozen():
self.thumbnail: Optional[Union[str, InputFile]] = self._parse_thumb_input(thumbnail)
self.thumbnail: Optional[Union[str, InputFile]] = self._parse_thumbnail_input(
thumbnail
)
self.disable_content_type_detection: Optional[bool] = disable_content_type_detection
@property
def thumb(self) -> Optional[Union[str, InputFile]]:
""":class:`telegram.InputFile`: Optional. |thumbdocstringbase|
.. deprecated:: 20.2
|thumbattributedeprecation| :attr:`thumbnail`.
"""
warn_about_deprecated_attr_in_property(
deprecated_attr_name="thumb",
new_attr_name="thumbnail",
bot_api_version="6.6",
)
return self.thumbnail
+7 -5
View File
@@ -18,15 +18,17 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram InputSticker."""
from typing import Optional, Sequence, Tuple, Union
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Union
from telegram._files.inputfile import InputFile
from telegram._files.sticker import MaskPosition
from telegram._telegramobject import TelegramObject
from telegram._utils.argumentparsing import parse_sequence_arg
from telegram._utils.files import parse_file_input
from telegram._utils.types import FileInput, JSONDict
if TYPE_CHECKING:
from telegram._files.inputfile import InputFile
class InputSticker(TelegramObject):
"""
@@ -74,10 +76,10 @@ class InputSticker(TelegramObject):
self,
sticker: FileInput,
emoji_list: Sequence[str],
mask_position: MaskPosition = None,
keywords: Sequence[str] = None,
mask_position: Optional[MaskPosition] = None,
keywords: Optional[Sequence[str]] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
+9 -9
View File
@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Location."""
from typing import ClassVar, Optional
from typing import Final, Optional
from telegram import constants
from telegram._telegramobject import TelegramObject
@@ -72,12 +72,12 @@ class Location(TelegramObject):
self,
longitude: float,
latitude: float,
horizontal_accuracy: float = None,
live_period: int = None,
heading: int = None,
proximity_alert_radius: int = None,
horizontal_accuracy: Optional[float] = None,
live_period: Optional[int] = None,
heading: Optional[int] = None,
proximity_alert_radius: Optional[int] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
@@ -96,17 +96,17 @@ class Location(TelegramObject):
self._freeze()
HORIZONTAL_ACCURACY: ClassVar[int] = constants.LocationLimit.HORIZONTAL_ACCURACY
HORIZONTAL_ACCURACY: Final[int] = constants.LocationLimit.HORIZONTAL_ACCURACY
""":const:`telegram.constants.LocationLimit.HORIZONTAL_ACCURACY`
.. versionadded:: 20.0
"""
MIN_HEADING: ClassVar[int] = constants.LocationLimit.MIN_HEADING
MIN_HEADING: Final[int] = constants.LocationLimit.MIN_HEADING
""":const:`telegram.constants.LocationLimit.MIN_HEADING`
.. versionadded:: 20.0
"""
MAX_HEADING: ClassVar[int] = constants.LocationLimit.MAX_HEADING
MAX_HEADING: Final[int] = constants.LocationLimit.MAX_HEADING
""":const:`telegram.constants.LocationLimit.MAX_HEADING`
.. versionadded:: 20.0
+4 -2
View File
@@ -18,6 +18,8 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram PhotoSize."""
from typing import Optional
from telegram._files._basemedium import _BaseMedium
from telegram._utils.types import JSONDict
@@ -59,9 +61,9 @@ class PhotoSize(_BaseMedium):
file_unique_id: str,
width: int,
height: int,
file_size: int = None,
file_size: Optional[int] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
+27 -55
View File
@@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains objects that represent stickers."""
from typing import TYPE_CHECKING, ClassVar, Optional, Sequence, Tuple
from typing import TYPE_CHECKING, Final, Optional, Sequence, Tuple
from telegram import constants
from telegram._files._basethumbedmedium import _BaseThumbedMedium
@@ -26,10 +26,6 @@ from telegram._files.photosize import PhotoSize
from telegram._telegramobject import TelegramObject
from telegram._utils.argumentparsing import parse_sequence_arg
from telegram._utils.types import JSONDict
from telegram._utils.warnings_transition import (
warn_about_deprecated_attr_in_property,
warn_about_thumb_return_thumbnail,
)
if TYPE_CHECKING:
from telegram import Bot
@@ -46,6 +42,9 @@ class Sticker(_BaseThumbedMedium):
arguments had to be changed. Use keyword arguments to make sure that the arguments are
passed correctly.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
file_id (:obj:`str`): Identifier for this file, which can be used to download
or reuse the file.
@@ -63,11 +62,6 @@ class Sticker(_BaseThumbedMedium):
format, which is determined by the fields :attr:`is_animated` and :attr:`is_video`.
.. versionadded:: 20.0
thumb (:class:`telegram.PhotoSize`, optional): Sticker thumbnail in the ``.WEBP`` or
``.JPG`` format.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
emoji (:obj:`str`, optional): Emoji associated with the sticker
set_name (:obj:`str`, optional): Name of the sticker set to which the sticker belongs.
mask_position (:class:`telegram.MaskPosition`, optional): For mask stickers, the position
@@ -157,23 +151,21 @@ class Sticker(_BaseThumbedMedium):
is_animated: bool,
is_video: bool,
type: str, # pylint: disable=redefined-builtin
thumb: PhotoSize = None,
emoji: str = None,
file_size: int = None,
set_name: str = None,
mask_position: "MaskPosition" = None,
premium_animation: "File" = None,
custom_emoji_id: str = None,
thumbnail: PhotoSize = None,
needs_repainting: bool = None,
emoji: Optional[str] = None,
file_size: Optional[int] = None,
set_name: Optional[str] = None,
mask_position: Optional["MaskPosition"] = None,
premium_animation: Optional["File"] = None,
custom_emoji_id: Optional[str] = None,
thumbnail: Optional[PhotoSize] = None,
needs_repainting: Optional[bool] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
file_unique_id=file_unique_id,
file_size=file_size,
thumb=thumb,
thumbnail=thumbnail,
api_kwargs=api_kwargs,
)
@@ -192,11 +184,11 @@ class Sticker(_BaseThumbedMedium):
self.custom_emoji_id: Optional[str] = custom_emoji_id
self.needs_repainting: Optional[bool] = needs_repainting
REGULAR: ClassVar[str] = constants.StickerType.REGULAR
REGULAR: Final[str] = constants.StickerType.REGULAR
""":const:`telegram.constants.StickerType.REGULAR`"""
MASK: ClassVar[str] = constants.StickerType.MASK
MASK: Final[str] = constants.StickerType.MASK
""":const:`telegram.constants.StickerType.MASK`"""
CUSTOM_EMOJI: ClassVar[str] = constants.StickerType.CUSTOM_EMOJI
CUSTOM_EMOJI: Final[str] = constants.StickerType.CUSTOM_EMOJI
""":const:`telegram.constants.StickerType.CUSTOM_EMOJI`"""
@classmethod
@@ -234,6 +226,9 @@ class StickerSet(TelegramObject):
.. versionchanged:: 20.0
The parameter ``contains_masks`` has been removed. Use :paramref:`sticker_type` instead.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
name (:obj:`str`): Sticker set name.
title (:obj:`str`): Sticker set title.
@@ -251,11 +246,6 @@ class StickerSet(TelegramObject):
:attr:`telegram.Sticker.CUSTOM_EMOJI`.
.. versionadded:: 20.0
thumb (:class:`telegram.PhotoSize`, optional): Sticker set thumbnail in the ``.WEBP``,
``.TGS``, or ``.WEBM`` format.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
thumbnail (:class:`telegram.PhotoSize`, optional): Sticker set thumbnail in the ``.WEBP``,
``.TGS``, or ``.WEBM`` format.
@@ -302,10 +292,9 @@ class StickerSet(TelegramObject):
stickers: Sequence[Sticker],
is_video: bool,
sticker_type: str,
thumb: PhotoSize = None,
thumbnail: PhotoSize = None,
thumbnail: Optional[PhotoSize] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.name: str = name
@@ -316,28 +305,11 @@ class StickerSet(TelegramObject):
self.sticker_type: str = sticker_type
# Optional
self.thumbnail: Optional[PhotoSize] = warn_about_thumb_return_thumbnail(
deprecated_arg=thumb, new_arg=thumbnail
)
self.thumbnail: Optional[PhotoSize] = thumbnail
self._id_attrs = (self.name,)
self._freeze()
@property
def thumb(self) -> Optional[PhotoSize]:
""":class:`telegram.PhotoSize`: Optional. Sticker set thumbnail in the ``.WEBP``,
``.TGS``, or ``.WEBM`` format.
.. deprecated:: 20.2
|thumbattributedeprecation| :attr:`thumbnail`.
"""
warn_about_deprecated_attr_in_property(
deprecated_attr_name="thumb",
new_attr_name="thumbnail",
bot_api_version="6.6",
)
return self.thumbnail
@classmethod
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["StickerSet"]:
"""See :meth:`telegram.TelegramObject.de_json`."""
@@ -390,13 +362,13 @@ class MaskPosition(TelegramObject):
__slots__ = ("point", "scale", "x_shift", "y_shift")
FOREHEAD: ClassVar[str] = constants.MaskPosition.FOREHEAD
FOREHEAD: Final[str] = constants.MaskPosition.FOREHEAD
""":const:`telegram.constants.MaskPosition.FOREHEAD`"""
EYES: ClassVar[str] = constants.MaskPosition.EYES
EYES: Final[str] = constants.MaskPosition.EYES
""":const:`telegram.constants.MaskPosition.EYES`"""
MOUTH: ClassVar[str] = constants.MaskPosition.MOUTH
MOUTH: Final[str] = constants.MaskPosition.MOUTH
""":const:`telegram.constants.MaskPosition.MOUTH`"""
CHIN: ClassVar[str] = constants.MaskPosition.CHIN
CHIN: Final[str] = constants.MaskPosition.CHIN
""":const:`telegram.constants.MaskPosition.CHIN`"""
def __init__(
@@ -406,7 +378,7 @@ class MaskPosition(TelegramObject):
y_shift: float,
scale: float,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.point: str = point
+5 -5
View File
@@ -79,12 +79,12 @@ class Venue(TelegramObject):
location: Location,
title: str,
address: str,
foursquare_id: str = None,
foursquare_type: str = None,
google_place_id: str = None,
google_place_type: str = None,
foursquare_id: Optional[str] = None,
foursquare_type: Optional[str] = None,
google_place_id: Optional[str] = None,
google_place_type: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
+8 -11
View File
@@ -30,6 +30,9 @@ class Video(_BaseThumbedMedium):
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`file_unique_id` is equal.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
file_id (:obj:`str`): Identifier for this file, which can be used to download
or reuse the file.
@@ -39,10 +42,6 @@ class Video(_BaseThumbedMedium):
width (:obj:`int`): Video width as defined by sender.
height (:obj:`int`): Video height as defined by sender.
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
thumb (:class:`telegram.PhotoSize`, optional): Video thumbnail.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
file_name (:obj:`str`, optional): Original filename as defined by sender.
mime_type (:obj:`str`, optional): MIME type of a file as defined by sender.
file_size (:obj:`int`, optional): File size in bytes.
@@ -76,19 +75,17 @@ class Video(_BaseThumbedMedium):
width: int,
height: int,
duration: int,
thumb: PhotoSize = None,
mime_type: str = None,
file_size: int = None,
file_name: str = None,
thumbnail: PhotoSize = None,
mime_type: Optional[str] = None,
file_size: Optional[int] = None,
file_name: Optional[str] = None,
thumbnail: Optional[PhotoSize] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
file_unique_id=file_unique_id,
file_size=file_size,
thumb=thumb,
thumbnail=thumbnail,
api_kwargs=api_kwargs,
)
+8 -9
View File
@@ -18,6 +18,8 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram VideoNote."""
from typing import Optional
from telegram._files._basethumbedmedium import _BaseThumbedMedium
from telegram._files.photosize import PhotoSize
from telegram._utils.types import JSONDict
@@ -29,6 +31,9 @@ class VideoNote(_BaseThumbedMedium):
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`file_unique_id` is equal.
.. versionchanged:: 20.5
|removed_thumb_note|
Args:
file_id (:obj:`str`): Identifier for this file, which can be used to download
or reuse the file.
@@ -38,10 +43,6 @@ class VideoNote(_BaseThumbedMedium):
length (:obj:`int`): Video width and height (diameter of the video message) as defined
by sender.
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
thumb (:class:`telegram.PhotoSize`, optional): Video thumbnail.
.. deprecated:: 20.2
|thumbargumentdeprecation| :paramref:`thumbnail`.
file_size (:obj:`int`, optional): File size in bytes.
thumbnail (:class:`telegram.PhotoSize`, optional): Video thumbnail.
@@ -71,17 +72,15 @@ class VideoNote(_BaseThumbedMedium):
file_unique_id: str,
length: int,
duration: int,
thumb: PhotoSize = None,
file_size: int = None,
thumbnail: PhotoSize = None,
file_size: Optional[int] = None,
thumbnail: Optional[PhotoSize] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
file_unique_id=file_unique_id,
file_size=file_size,
thumb=thumb,
thumbnail=thumbnail,
api_kwargs=api_kwargs,
)
+3 -3
View File
@@ -58,10 +58,10 @@ class Voice(_BaseMedium):
file_id: str,
file_unique_id: str,
duration: int,
mime_type: str = None,
file_size: int = None,
mime_type: Optional[str] = None,
file_size: Optional[int] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(
file_id=file_id,
+6 -6
View File
@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram ForceReply."""
from typing import ClassVar, Optional
from typing import Final, Optional
from telegram import constants
from telegram._telegramobject import TelegramObject
@@ -79,10 +79,10 @@ class ForceReply(TelegramObject):
def __init__(
self,
selective: bool = None,
input_field_placeholder: str = None,
selective: Optional[bool] = None,
input_field_placeholder: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.force_reply: bool = True
@@ -93,12 +93,12 @@ class ForceReply(TelegramObject):
self._freeze()
MIN_INPUT_FIELD_PLACEHOLDER: ClassVar[int] = constants.ReplyLimit.MIN_INPUT_FIELD_PLACEHOLDER
MIN_INPUT_FIELD_PLACEHOLDER: Final[int] = constants.ReplyLimit.MIN_INPUT_FIELD_PLACEHOLDER
""":const:`telegram.constants.ReplyLimit.MIN_INPUT_FIELD_PLACEHOLDER`
.. versionadded:: 20.0
"""
MAX_INPUT_FIELD_PLACEHOLDER: ClassVar[int] = constants.ReplyLimit.MAX_INPUT_FIELD_PLACEHOLDER
MAX_INPUT_FIELD_PLACEHOLDER: Final[int] = constants.ReplyLimit.MAX_INPUT_FIELD_PLACEHOLDER
""":const:`telegram.constants.ReplyLimit.MAX_INPUT_FIELD_PLACEHOLDER`
.. versionadded:: 20.0
+11 -11
View File
@@ -55,9 +55,9 @@ class ForumTopic(TelegramObject):
message_thread_id: int,
name: str,
icon_color: int,
icon_custom_emoji_id: str = None,
icon_custom_emoji_id: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.message_thread_id: int = message_thread_id
@@ -99,9 +99,9 @@ class ForumTopicCreated(TelegramObject):
self,
name: str,
icon_color: int,
icon_custom_emoji_id: str = None,
icon_custom_emoji_id: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.name: str = name
@@ -123,7 +123,7 @@ class ForumTopicClosed(TelegramObject):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None) -> None:
def __init__(self, *, api_kwargs: Optional[JSONDict] = None) -> None:
super().__init__(api_kwargs=api_kwargs)
self._freeze()
@@ -139,7 +139,7 @@ class ForumTopicReopened(TelegramObject):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None) -> None:
def __init__(self, *, api_kwargs: Optional[JSONDict] = None) -> None:
super().__init__(api_kwargs=api_kwargs)
self._freeze()
@@ -169,10 +169,10 @@ class ForumTopicEdited(TelegramObject):
def __init__(
self,
name: str = None,
icon_custom_emoji_id: str = None,
name: Optional[str] = None,
icon_custom_emoji_id: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.name: Optional[str] = name
@@ -193,7 +193,7 @@ class GeneralForumTopicHidden(TelegramObject):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self._freeze()
@@ -209,7 +209,7 @@ class GeneralForumTopicUnhidden(TelegramObject):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self._freeze()
+3 -1
View File
@@ -18,6 +18,8 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram CallbackGame."""
from typing import Optional
from telegram._telegramobject import TelegramObject
from telegram._utils.types import JSONDict
@@ -27,7 +29,7 @@ class CallbackGame(TelegramObject):
__slots__ = ()
def __init__(self, *, api_kwargs: JSONDict = None) -> None:
def __init__(self, *, api_kwargs: Optional[JSONDict] = None) -> None:
super().__init__(api_kwargs=api_kwargs)
self._freeze()
+5 -5
View File
@@ -101,11 +101,11 @@ class Game(TelegramObject):
title: str,
description: str,
photo: Sequence[PhotoSize],
text: str = None,
text_entities: Sequence[MessageEntity] = None,
animation: Animation = None,
text: Optional[str] = None,
text_entities: Optional[Sequence[MessageEntity]] = None,
animation: Optional[Animation] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
@@ -162,7 +162,7 @@ class Game(TelegramObject):
return entity_text.decode("utf-16-le")
def parse_text_entities(self, types: List[str] = None) -> Dict[MessageEntity, str]:
def parse_text_entities(self, types: Optional[List[str]] = None) -> Dict[MessageEntity, str]:
"""
Returns a :obj:`dict` that maps :class:`telegram.MessageEntity` to :obj:`str`.
It contains entities from this message filtered by their
+3 -1
View File
@@ -48,7 +48,9 @@ class GameHighScore(TelegramObject):
__slots__ = ("position", "user", "score")
def __init__(self, position: int, user: User, score: int, *, api_kwargs: JSONDict = None):
def __init__(
self, position: int, user: User, score: int, *, api_kwargs: Optional[JSONDict] = None
):
super().__init__(api_kwargs=api_kwargs)
self.position: int = position
self.user: User = user
+13 -13
View File
@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram InlineKeyboardButton."""
from typing import TYPE_CHECKING, ClassVar, Optional, Union
from typing import TYPE_CHECKING, Final, Optional, Union
from telegram import constants
from telegram._games.callbackgame import CallbackGame
@@ -220,17 +220,17 @@ class InlineKeyboardButton(TelegramObject):
def __init__(
self,
text: str,
url: str = None,
callback_data: Union[str, object] = None,
switch_inline_query: str = None,
switch_inline_query_current_chat: str = None,
callback_game: CallbackGame = None,
pay: bool = None,
login_url: LoginUrl = None,
web_app: WebAppInfo = None,
switch_inline_query_chosen_chat: SwitchInlineQueryChosenChat = None,
url: Optional[str] = None,
callback_data: Optional[Union[str, object]] = None,
switch_inline_query: Optional[str] = None,
switch_inline_query_current_chat: Optional[str] = None,
callback_game: Optional[CallbackGame] = None,
pay: Optional[bool] = None,
login_url: Optional[LoginUrl] = None,
web_app: Optional[WebAppInfo] = None,
switch_inline_query_chosen_chat: Optional[SwitchInlineQueryChosenChat] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
@@ -297,12 +297,12 @@ class InlineKeyboardButton(TelegramObject):
self.callback_data = callback_data
self._set_id_attrs()
MIN_CALLBACK_DATA: ClassVar[int] = constants.InlineKeyboardButtonLimit.MIN_CALLBACK_DATA
MIN_CALLBACK_DATA: Final[int] = constants.InlineKeyboardButtonLimit.MIN_CALLBACK_DATA
""":const:`telegram.constants.InlineKeyboardButtonLimit.MIN_CALLBACK_DATA`
.. versionadded:: 20.0
"""
MAX_CALLBACK_DATA: ClassVar[int] = constants.InlineKeyboardButtonLimit.MAX_CALLBACK_DATA
MAX_CALLBACK_DATA: Final[int] = constants.InlineKeyboardButtonLimit.MAX_CALLBACK_DATA
""":const:`telegram.constants.InlineKeyboardButtonLimit.MAX_CALLBACK_DATA`
.. versionadded:: 20.0
+1 -1
View File
@@ -72,7 +72,7 @@ class InlineKeyboardMarkup(TelegramObject):
self,
inline_keyboard: Sequence[Sequence[InlineKeyboardButton]],
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
if not check_keyboard_type(inline_keyboard):

Some files were not shown because too many files have changed in this diff Show More