aiogram-webhook 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aiogram_webhook-0.0.1/.github/dependabot.yml +11 -0
- aiogram_webhook-0.0.1/.github/workflows/release.yml +54 -0
- aiogram_webhook-0.0.1/.github/workflows/tests.yml +39 -0
- aiogram_webhook-0.0.1/.gitignore +207 -0
- aiogram_webhook-0.0.1/CHANGELOG.md +7 -0
- aiogram_webhook-0.0.1/LICENSE +21 -0
- aiogram_webhook-0.0.1/PKG-INFO +143 -0
- aiogram_webhook-0.0.1/README.md +119 -0
- aiogram_webhook-0.0.1/aiogram_webhook/__init__.py +12 -0
- aiogram_webhook-0.0.1/aiogram_webhook/adapters/__init__.py +0 -0
- aiogram_webhook-0.0.1/aiogram_webhook/adapters/base.py +67 -0
- aiogram_webhook-0.0.1/aiogram_webhook/adapters/fastapi.py +41 -0
- aiogram_webhook-0.0.1/aiogram_webhook/engines/__init__.py +5 -0
- aiogram_webhook-0.0.1/aiogram_webhook/engines/base.py +129 -0
- aiogram_webhook-0.0.1/aiogram_webhook/engines/simple.py +106 -0
- aiogram_webhook-0.0.1/aiogram_webhook/engines/token.py +99 -0
- aiogram_webhook-0.0.1/aiogram_webhook/routing/__init__.py +5 -0
- aiogram_webhook-0.0.1/aiogram_webhook/routing/base.py +33 -0
- aiogram_webhook-0.0.1/aiogram_webhook/routing/path.py +28 -0
- aiogram_webhook-0.0.1/aiogram_webhook/routing/query.py +35 -0
- aiogram_webhook-0.0.1/aiogram_webhook/security/__init__.py +6 -0
- aiogram_webhook-0.0.1/aiogram_webhook/security/checks/__init__.py +0 -0
- aiogram_webhook-0.0.1/aiogram_webhook/security/checks/check.py +14 -0
- aiogram_webhook-0.0.1/aiogram_webhook/security/checks/ip.py +67 -0
- aiogram_webhook-0.0.1/aiogram_webhook/security/secret_token.py +42 -0
- aiogram_webhook-0.0.1/aiogram_webhook/security/security.py +38 -0
- aiogram_webhook-0.0.1/pyproject.toml +116 -0
- aiogram_webhook-0.0.1/uv.lock +1023 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
updates:
|
|
8
|
+
- package-ecosystem: "pip" # See documentation for possible values
|
|
9
|
+
directory: "/" # Location of package manifests
|
|
10
|
+
schedule:
|
|
11
|
+
interval: "weekly"
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
name: Release & Publish
|
|
2
|
+
|
|
3
|
+
on: workflow_dispatch
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
release:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
concurrency:
|
|
9
|
+
group: ${{ github.workflow }}-release-${{ github.ref_name }}
|
|
10
|
+
cancel-in-progress: true
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
id-token: write
|
|
14
|
+
contents: write
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- name: 🛎️ Checkout repository
|
|
18
|
+
uses: actions/checkout@v4
|
|
19
|
+
with:
|
|
20
|
+
ref: ${{ github.ref_name }}
|
|
21
|
+
fetch-depth: 0
|
|
22
|
+
|
|
23
|
+
- name: 🏷️ Semantic Version Release
|
|
24
|
+
id: release
|
|
25
|
+
uses: python-semantic-release/python-semantic-release@v10.5.3
|
|
26
|
+
with:
|
|
27
|
+
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
28
|
+
git_committer_name: "github-actions"
|
|
29
|
+
git_committer_email: "actions@users.noreply.github.com"
|
|
30
|
+
|
|
31
|
+
- name: 🐍 Setup uv & venv
|
|
32
|
+
if: steps.release.outputs.released == 'true'
|
|
33
|
+
uses: astral-sh/setup-uv@v5
|
|
34
|
+
|
|
35
|
+
- name: 📦 Update uv.lock
|
|
36
|
+
if: steps.release.outputs.released == 'true'
|
|
37
|
+
run: uv lock
|
|
38
|
+
|
|
39
|
+
- name: 🏗️ Build package (sdist & wheel)
|
|
40
|
+
if: steps.release.outputs.released == 'true'
|
|
41
|
+
run: uv build --no-sources
|
|
42
|
+
|
|
43
|
+
- name: 🚀 Publish | Upload to GitHub Release Assets
|
|
44
|
+
if: steps.release.outputs.released == 'true'
|
|
45
|
+
uses: python-semantic-release/publish-action@v10.5.3
|
|
46
|
+
with:
|
|
47
|
+
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
48
|
+
tag: ${{ steps.release.outputs.tag }}
|
|
49
|
+
|
|
50
|
+
- name: 🐍 Publish to PyPI
|
|
51
|
+
if: steps.release.outputs.released == 'true'
|
|
52
|
+
env:
|
|
53
|
+
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_TOKEN }}
|
|
54
|
+
run: uv publish
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main, develop]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main, develop]
|
|
8
|
+
|
|
9
|
+
concurrency:
|
|
10
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
11
|
+
cancel-in-progress: false
|
|
12
|
+
|
|
13
|
+
permissions:
|
|
14
|
+
contents: read
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
lint-and-test:
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
strategy:
|
|
20
|
+
matrix:
|
|
21
|
+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
|
22
|
+
fail-fast: false
|
|
23
|
+
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v4
|
|
26
|
+
|
|
27
|
+
- name: Install uv and set the python version
|
|
28
|
+
uses: astral-sh/setup-uv@v5
|
|
29
|
+
with:
|
|
30
|
+
python-version: ${{ matrix.python-version }}
|
|
31
|
+
|
|
32
|
+
- name: Install the project
|
|
33
|
+
run: uv sync --all-groups
|
|
34
|
+
|
|
35
|
+
- name: Run Ruff
|
|
36
|
+
run: uv run ruff check --output-format=github .
|
|
37
|
+
|
|
38
|
+
# - name: Run pytest
|
|
39
|
+
# run: uv run pytest
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 m-xim
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aiogram-webhook
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A python library for integrating webhook support with multiple web frameworks in aiogram. Organizes bot operation via webhooks for both single and multi-bot setups.
|
|
5
|
+
Project-URL: Homepage, https://github.com/m-xim/aiogram-webhook
|
|
6
|
+
Project-URL: Repository, https://github.com/m-xim/aiogram-webhook
|
|
7
|
+
Project-URL: Issues, https://github.com/m-xim/aiogram-webhook/issues
|
|
8
|
+
Project-URL: Documentation, https://github.com/m-xim/aiogram-webhook#readme
|
|
9
|
+
Author-email: m-xim <i@m-xim.ru>
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: aiogram,fastapi,multibot,telegram,webhook
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: aiogram>=3.23.0
|
|
22
|
+
Requires-Dist: yarl>=1.22.0
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# aiogram-webhook
|
|
26
|
+
|
|
27
|
+
[](https://pypi.org/project/aiogram-webhook)
|
|
28
|
+
[](/LICENSE)
|
|
29
|
+
[](https://github.com/m-xim/aiogram-webhook/actions)
|
|
30
|
+
[](https://github.com/m-xim/aiogram-webhook/actions)
|
|
31
|
+
[](https://deepwiki.com/m-xim/aiogram-webhook)
|
|
32
|
+
|
|
33
|
+
**aiogram-webhook** is a Python library for seamless webhook integration with multiple web frameworks in aiogram. It enables both single and multi-bot operation via webhooks, with flexible routing and security features.
|
|
34
|
+
|
|
35
|
+
<br>
|
|
36
|
+
|
|
37
|
+
## ✨ Features
|
|
38
|
+
|
|
39
|
+
- 🧱 Modular and extensible webhook engine
|
|
40
|
+
- 🔀 Flexible routing (static and token-based)
|
|
41
|
+
- 🤖 Supports single and multi-bot setups
|
|
42
|
+
- ⚡ FastAPI adapters out of the box
|
|
43
|
+
- 🔒 Security best practices: secret tokens, IP checks
|
|
44
|
+
- 🧩 Easy to extend with custom adapters and routing
|
|
45
|
+
|
|
46
|
+
## 🚀 Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
uv add aiogram-webhook
|
|
50
|
+
# or
|
|
51
|
+
pip install aiogram-webhook
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## ⚡ Quick Start
|
|
55
|
+
|
|
56
|
+
### Single Bot Example (FastAPI)
|
|
57
|
+
```python
|
|
58
|
+
import uvicorn
|
|
59
|
+
from contextlib import asynccontextmanager
|
|
60
|
+
from fastapi import FastAPI
|
|
61
|
+
from aiogram import Bot, Dispatcher, Router
|
|
62
|
+
from aiogram.filters import CommandStart
|
|
63
|
+
from aiogram.types import Message
|
|
64
|
+
from aiogram_webhook import SimpleEngine, FastApiWebAdapter
|
|
65
|
+
from aiogram_webhook.routing import PathRouting
|
|
66
|
+
|
|
67
|
+
router = Router()
|
|
68
|
+
|
|
69
|
+
@router.message(CommandStart())
|
|
70
|
+
async def start(message: Message):
|
|
71
|
+
await message.answer("OK")
|
|
72
|
+
|
|
73
|
+
dispatcher = Dispatcher()
|
|
74
|
+
dispatcher.include_router(router)
|
|
75
|
+
bot = Bot("BOT_TOKEN_HERE")
|
|
76
|
+
|
|
77
|
+
engine = SimpleEngine(
|
|
78
|
+
dispatcher,
|
|
79
|
+
bot,
|
|
80
|
+
web_adapter=FastApiWebAdapter(),
|
|
81
|
+
routing=PathRouting(url="/webhook"),
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
@asynccontextmanager
|
|
85
|
+
async def lifespan(app: FastAPI):
|
|
86
|
+
engine.register(app)
|
|
87
|
+
await engine.set_webhook(
|
|
88
|
+
drop_pending_updates=True,
|
|
89
|
+
allowed_updates=("message", "callback_query"),
|
|
90
|
+
)
|
|
91
|
+
await engine.on_startup()
|
|
92
|
+
yield
|
|
93
|
+
await engine.on_shutdown()
|
|
94
|
+
|
|
95
|
+
app = FastAPI(lifespan=lifespan)
|
|
96
|
+
|
|
97
|
+
if __name__ == "__main__":
|
|
98
|
+
uvicorn.run("main:app", host="0.0.0.0", port=8080)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Multi-Bot Example (FastAPI)
|
|
102
|
+
Each bot is configured in Telegram with its own webhook URL: `https://example.com/webhook/<BOT_TOKEN>`
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from aiogram import Dispatcher
|
|
106
|
+
from aiogram.client.default import DefaultBotProperties
|
|
107
|
+
from aiogram_webhook import TokenEngine, FastApiWebAdapter
|
|
108
|
+
from aiogram_webhook.routing import PathRouting
|
|
109
|
+
|
|
110
|
+
dispatcher = Dispatcher()
|
|
111
|
+
engine = TokenEngine(
|
|
112
|
+
dispatcher,
|
|
113
|
+
web_adapter=FastApiWebAdapter(),
|
|
114
|
+
routing=PathRouting(url="/webhook/{bot_token}", param="bot_token"),
|
|
115
|
+
bot_settings={
|
|
116
|
+
"default": DefaultBotProperties(parse_mode="HTML"),
|
|
117
|
+
},
|
|
118
|
+
)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Usage is the same:
|
|
122
|
+
```python
|
|
123
|
+
engine.register(app)
|
|
124
|
+
await engine.set_webhook(...)
|
|
125
|
+
await engine.on_startup()
|
|
126
|
+
await engine.on_shutdown()
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## 🛣️ Routing
|
|
130
|
+
|
|
131
|
+
`PathRouting` defines where Telegram sends updates:
|
|
132
|
+
|
|
133
|
+
- **Static path:**
|
|
134
|
+
```python
|
|
135
|
+
PathRouting(url="/webhook")
|
|
136
|
+
```
|
|
137
|
+
- **Token-based path:**
|
|
138
|
+
```python
|
|
139
|
+
PathRouting(url="/webhook/{bot_token}", param="bot_token")
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## 🛡️ Security
|
|
143
|
+
writings...
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# aiogram-webhook
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/aiogram-webhook)
|
|
4
|
+
[](/LICENSE)
|
|
5
|
+
[](https://github.com/m-xim/aiogram-webhook/actions)
|
|
6
|
+
[](https://github.com/m-xim/aiogram-webhook/actions)
|
|
7
|
+
[](https://deepwiki.com/m-xim/aiogram-webhook)
|
|
8
|
+
|
|
9
|
+
**aiogram-webhook** is a Python library for seamless webhook integration with multiple web frameworks in aiogram. It enables both single and multi-bot operation via webhooks, with flexible routing and security features.
|
|
10
|
+
|
|
11
|
+
<br>
|
|
12
|
+
|
|
13
|
+
## ✨ Features
|
|
14
|
+
|
|
15
|
+
- 🧱 Modular and extensible webhook engine
|
|
16
|
+
- 🔀 Flexible routing (static and token-based)
|
|
17
|
+
- 🤖 Supports single and multi-bot setups
|
|
18
|
+
- ⚡ FastAPI adapters out of the box
|
|
19
|
+
- 🔒 Security best practices: secret tokens, IP checks
|
|
20
|
+
- 🧩 Easy to extend with custom adapters and routing
|
|
21
|
+
|
|
22
|
+
## 🚀 Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
uv add aiogram-webhook
|
|
26
|
+
# or
|
|
27
|
+
pip install aiogram-webhook
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## ⚡ Quick Start
|
|
31
|
+
|
|
32
|
+
### Single Bot Example (FastAPI)
|
|
33
|
+
```python
|
|
34
|
+
import uvicorn
|
|
35
|
+
from contextlib import asynccontextmanager
|
|
36
|
+
from fastapi import FastAPI
|
|
37
|
+
from aiogram import Bot, Dispatcher, Router
|
|
38
|
+
from aiogram.filters import CommandStart
|
|
39
|
+
from aiogram.types import Message
|
|
40
|
+
from aiogram_webhook import SimpleEngine, FastApiWebAdapter
|
|
41
|
+
from aiogram_webhook.routing import PathRouting
|
|
42
|
+
|
|
43
|
+
router = Router()
|
|
44
|
+
|
|
45
|
+
@router.message(CommandStart())
|
|
46
|
+
async def start(message: Message):
|
|
47
|
+
await message.answer("OK")
|
|
48
|
+
|
|
49
|
+
dispatcher = Dispatcher()
|
|
50
|
+
dispatcher.include_router(router)
|
|
51
|
+
bot = Bot("BOT_TOKEN_HERE")
|
|
52
|
+
|
|
53
|
+
engine = SimpleEngine(
|
|
54
|
+
dispatcher,
|
|
55
|
+
bot,
|
|
56
|
+
web_adapter=FastApiWebAdapter(),
|
|
57
|
+
routing=PathRouting(url="/webhook"),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
@asynccontextmanager
|
|
61
|
+
async def lifespan(app: FastAPI):
|
|
62
|
+
engine.register(app)
|
|
63
|
+
await engine.set_webhook(
|
|
64
|
+
drop_pending_updates=True,
|
|
65
|
+
allowed_updates=("message", "callback_query"),
|
|
66
|
+
)
|
|
67
|
+
await engine.on_startup()
|
|
68
|
+
yield
|
|
69
|
+
await engine.on_shutdown()
|
|
70
|
+
|
|
71
|
+
app = FastAPI(lifespan=lifespan)
|
|
72
|
+
|
|
73
|
+
if __name__ == "__main__":
|
|
74
|
+
uvicorn.run("main:app", host="0.0.0.0", port=8080)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Multi-Bot Example (FastAPI)
|
|
78
|
+
Each bot is configured in Telegram with its own webhook URL: `https://example.com/webhook/<BOT_TOKEN>`
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from aiogram import Dispatcher
|
|
82
|
+
from aiogram.client.default import DefaultBotProperties
|
|
83
|
+
from aiogram_webhook import TokenEngine, FastApiWebAdapter
|
|
84
|
+
from aiogram_webhook.routing import PathRouting
|
|
85
|
+
|
|
86
|
+
dispatcher = Dispatcher()
|
|
87
|
+
engine = TokenEngine(
|
|
88
|
+
dispatcher,
|
|
89
|
+
web_adapter=FastApiWebAdapter(),
|
|
90
|
+
routing=PathRouting(url="/webhook/{bot_token}", param="bot_token"),
|
|
91
|
+
bot_settings={
|
|
92
|
+
"default": DefaultBotProperties(parse_mode="HTML"),
|
|
93
|
+
},
|
|
94
|
+
)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Usage is the same:
|
|
98
|
+
```python
|
|
99
|
+
engine.register(app)
|
|
100
|
+
await engine.set_webhook(...)
|
|
101
|
+
await engine.on_startup()
|
|
102
|
+
await engine.on_shutdown()
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## 🛣️ Routing
|
|
106
|
+
|
|
107
|
+
`PathRouting` defines where Telegram sends updates:
|
|
108
|
+
|
|
109
|
+
- **Static path:**
|
|
110
|
+
```python
|
|
111
|
+
PathRouting(url="/webhook")
|
|
112
|
+
```
|
|
113
|
+
- **Token-based path:**
|
|
114
|
+
```python
|
|
115
|
+
PathRouting(url="/webhook/{bot_token}", param="bot_token")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 🛡️ Security
|
|
119
|
+
writings...
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from aiogram_webhook.adapters.base import WebAdapter
|
|
2
|
+
from aiogram_webhook.engines.simple import SimpleEngine
|
|
3
|
+
from aiogram_webhook.engines.token import TokenEngine
|
|
4
|
+
|
|
5
|
+
__all__ = ["SimpleEngine", "TokenEngine", "WebAdapter"]
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from aiogram_webhook.adapters.fastapi import FastApiWebAdapter # noqa: F401
|
|
9
|
+
|
|
10
|
+
__all__.insert(0, "FastApiWebAdapter")
|
|
11
|
+
except ImportError:
|
|
12
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import TYPE_CHECKING, Any
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from collections.abc import Awaitable, Callable
|
|
9
|
+
from ipaddress import IPv4Address, IPv6Address
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(slots=True)
|
|
13
|
+
class BoundRequest(ABC):
|
|
14
|
+
"""
|
|
15
|
+
Abstract base class for a request bound to a web adapter.
|
|
16
|
+
|
|
17
|
+
Provides interface for extracting data from incoming requests and generating responses.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
request: Any
|
|
21
|
+
adapter: WebAdapter
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
async def json(self) -> dict[str, Any]:
|
|
25
|
+
raise NotImplementedError
|
|
26
|
+
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def header(self, name: str) -> Any | None:
|
|
29
|
+
raise NotImplementedError
|
|
30
|
+
|
|
31
|
+
@abstractmethod
|
|
32
|
+
def query_param(self, name: str) -> Any | None:
|
|
33
|
+
raise NotImplementedError
|
|
34
|
+
|
|
35
|
+
@abstractmethod
|
|
36
|
+
def path_param(self, name: str) -> Any | None:
|
|
37
|
+
raise NotImplementedError
|
|
38
|
+
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def ip(self) -> IPv4Address | IPv6Address | str | None:
|
|
41
|
+
raise NotImplementedError
|
|
42
|
+
|
|
43
|
+
def secret_token(self) -> str | None:
|
|
44
|
+
return self.header(self.adapter.secret_header)
|
|
45
|
+
|
|
46
|
+
@abstractmethod
|
|
47
|
+
def json_response(self, status: int, payload: dict[str, Any]) -> Any:
|
|
48
|
+
raise NotImplementedError
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass
|
|
52
|
+
class WebAdapter(ABC):
|
|
53
|
+
"""
|
|
54
|
+
Abstract base class for web framework adapters.
|
|
55
|
+
|
|
56
|
+
Provides interface for binding requests and registering webhook handlers.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
secret_header: str = "x-telegram-bot-api-secret-token" # noqa: S105
|
|
60
|
+
|
|
61
|
+
@abstractmethod
|
|
62
|
+
def bind(self, request: Any) -> BoundRequest:
|
|
63
|
+
raise NotImplementedError
|
|
64
|
+
|
|
65
|
+
@abstractmethod
|
|
66
|
+
def register(self, app: Any, path: str, handler: Callable[[BoundRequest], Awaitable[Any]]) -> None:
|
|
67
|
+
raise NotImplementedError
|