Skip to content

Commit 6c2e429

Browse files
committed
simple echo bot
0 parents  commit 6c2e429

12 files changed

+309
-0
lines changed

.bandit

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bandit]
2+
exclude: /venv/

.ci/Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3.10-slim
2+
ENV PYTHONUNBUFFERED 0
3+
4+
RUN apt-get update \
5+
&& pip install --upgrade --no-cache-dir pip \
6+
&& pip install --upgrade --no-cache-dir wheel \
7+
&& pip install --upgrade --no-cache-dir setuptools
8+
9+
COPY .. /src
10+
WORKDIR /src
11+
12+
COPY ../.env.default ./.env
13+
14+
RUN pip install -r requirements.txt
15+
16+
CMD [ "python", "main.py" ]

.dockerignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
venv*
2+
htmlcov
3+
.venv
4+
.git
5+
.gitignore
6+
.dockerignore
7+
.editorconfig
8+
.bandit
9+
.flake8
10+
**/*.pyc
11+
**/*.log
12+
.idea
13+
.coverage
14+
.coverage.*
15+
.cache
16+
docs
17+
build
18+
dist
19+
reports
20+
.bandit.baseline
21+
.coverage
22+
.coverage.*
23+
*.cover
24+
.hypothesis/
25+
.pytest_cache/
26+
# Byte-compiled / optimized / DLL files
27+
__pycache__/
28+
**/*.py[cod]
29+
**/*$py.class

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; https://EditorConfig.org
2+
; https://b2btech.atlassian.net/wiki/spaces/B2BP2/pages/637042710/editorconfig
3+
root = true
4+
5+
[*.{py, css, scss, sass, less, js, ts, html, xhtml, json, txt, ini, yml, env, sh, cfg}]
6+
charset = utf-8
7+
max_line_length = 140
8+
9+
[*.py]
10+
indent_style = space
11+
indent_size = 4
12+
trim_trailing_whitespace = true
13+
insert_final_newline = true
14+
; for future
15+
quote_type = single
16+
17+
[Makefile]
18+
indent_style = tab
19+
20+
[*.{css, scss, sass, less, js, ts, html, xhtml, json}]
21+
indent_style = space
22+
indent_size = 2

.env.default

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LOG_LEVEL=INFO
2+
TELEGRAM_API_KEY=

.flake8

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[flake8]
2+
max-line-length = 140
3+
exclude =
4+
venv*/*,
5+
dev-venv/*,
6+
inline-quotes = '
7+
docstring-quotes = """

.gitignore

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Python template
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports / sast
41+
htmlcov/
42+
.tox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
.hypothesis/
50+
.pytest_cache/
51+
reports/
52+
.bandit.baseline
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
local_settings.py
60+
db.sqlite3
61+
62+
# Flask stuff:
63+
instance/
64+
.webassets-cache
65+
66+
# Scrapy stuff:
67+
.scrapy
68+
69+
# Sphinx documentation
70+
docs/_build/
71+
72+
# PyBuilder
73+
target/
74+
75+
# Jupyter Notebook
76+
.ipynb_checkpoints
77+
78+
# pyenv
79+
.python-version
80+
81+
# celery beat schedule file
82+
celerybeat-schedule
83+
84+
# SageMath parsed files
85+
*.sage.py
86+
87+
# Environments
88+
.env
89+
.venv
90+
env/
91+
venv/
92+
ENV/
93+
env.bak/
94+
venv.bak/
95+
96+
# Spyder project settings
97+
.spyderproject
98+
.spyproject
99+
100+
# Rope project settings
101+
.ropeproject
102+
103+
# mkdocs documentation
104+
/site
105+
106+
# mypy
107+
.mypy_cache/
108+
109+
.idea
110+
111+
logs
112+
*.log
113+
*conf.ini
114+
!default.*conf.ini
115+
116+
*.pytest.ini
117+
118+
# tmp
119+
tmp/
120+
temp/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Ilia
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.

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# aiogram bot template (skeleton)
2+
3+
[![\[Telegram\] aiogram live](https://img.shields.io/badge/telegram-aiogram-blue.svg?style=flat-square)](https://t.me/aiogram_live)
4+
[![Supported python versions](https://img.shields.io/pypi/pyversions/aiogram.svg?style=flat-square)](https://pypi.python.org/pypi/aiogram)
5+
[![Telegram Bot API](https://img.shields.io/badge/Telegram%20Bot%20API-6.4-blue.svg?style=flat-square&logo=telegram)](https://core.telegram.org/bots/api)
6+
[![MIT License](https://img.shields.io/pypi/l/aiogram.svg?style=flat-square)](https://opensource.org/licenses/MIT)
7+
8+
This is just a project template for writing telegram bots. The project has linter, logger, docker, dot-env configured.
9+
10+
## How to run
11+
12+
### Without Docker:
13+
14+
- Make virtual environment
15+
- Install package requirements
16+
- Create `.env` or set env-variables as you like (example: [.env.default](.env.default))
17+
- Run it! :)
18+
19+
### With Docker
20+
21+
- Create `.env` or set env-variables as you like (example: [.env.default](.env.default)
22+
and see [docker-compose.yml](docker-compose.yml))
23+
- Run it! :)

docker-compose.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.7'
2+
3+
services:
4+
aiogram_template:
5+
build:
6+
context: .
7+
dockerfile: .ci/Dockerfile
8+
container_name: aiogram_template
9+
command: python main.py
10+
environment:
11+
- LOG_LEVEL=INFO
12+
env_file:
13+
- .env

main.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import logging
2+
import os
3+
import time
4+
from datetime import timedelta
5+
6+
from aiogram import Bot, Dispatcher, executor, types
7+
from dotenv import load_dotenv
8+
9+
load_dotenv()
10+
11+
bot = Bot(token=os.getenv('TELEGRAM_API_KEY'))
12+
dp = Dispatcher(bot)
13+
SLEEP_AFTER_EXCEPTION = timedelta(minutes=1).seconds
14+
15+
16+
@dp.message_handler(commands=['start'])
17+
async def send_welcome(message: types.Message):
18+
chat_id: int = message.chat.id
19+
user_id: str = message.from_user.id
20+
username: str = message.from_user.username
21+
request_message: str = message.text
22+
23+
await message.answer_chat_action('typing')
24+
logging.info('>>> User[%s|%s:@%s]: %r', chat_id, user_id, username, request_message)
25+
await message.reply('Well, hello!')
26+
27+
28+
@dp.message_handler()
29+
async def echo(message: types.Message):
30+
chat_id: int = message.chat.id
31+
user_id: str = message.from_user.id
32+
username: str = message.from_user.username
33+
request_message: str = message.text
34+
35+
await message.answer_chat_action('typing')
36+
logging.info('>>> User[%s|%s:@%s]: %r', chat_id, user_id, username, request_message)
37+
logging.info('<<< User[%s|%s:@%s]: %r', chat_id, user_id, username, request_message)
38+
await message.answer(request_message)
39+
40+
41+
if __name__ == '__main__':
42+
logging.basicConfig(
43+
level=logging.getLevelName(os.getenv('LOG_LEVEL')),
44+
format='%(levelname)9s | %(asctime)s | %(name)30s | %(filename)20s | %(lineno)6s | %(message)s',
45+
)
46+
47+
while True:
48+
try:
49+
executor.start_polling(dp, skip_updates=True)
50+
except Exception as ex:
51+
logging.error('Error found: %r. Restarting...', ex)
52+
time.sleep(SLEEP_AFTER_EXCEPTION)

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-dotenv~=1.0.0
2+
aiogram~=2.25.1

0 commit comments

Comments
 (0)