generated from sudoskys/TelegramBotTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7c2a239
Showing
18 changed files
with
1,788 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
TELEGRAM_BOT_TOKEN=xxx | ||
# TELEGRAM_BOT_PROXY_ADDRESS=socks5://127.0.0.1:7890 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: Ruff | ||
on: [ push, pull_request ] | ||
jobs: | ||
ruff: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: chartboost/ruff-action@v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
.idea/ | ||
/.pdm-python | ||
|
||
# Ignore dynaconf secret files | ||
/conf_dir/.*.toml | ||
/conf_dir/.*.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v3.2.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- id: check-yaml | ||
- id: check-added-large-files | ||
|
||
- repo: https://github.com/astral-sh/ruff-pre-commit | ||
# Ruff version. | ||
rev: v0.1.7 | ||
hooks: | ||
# Run the linter. | ||
- id: ruff | ||
args: [ --fix ] | ||
# Run the formatter. | ||
- id: ruff-format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Jasmine | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# TelegramBotTemplate | ||
|
||
![Ruff](https://github.com/sudoskys/TelegramBotTemplate/actions/workflows/ruff.yml/badge.svg) | ||
|
||
## Project Description | ||
|
||
This project is a template for creating Telegram bots using Python. | ||
|
||
## Usage | ||
|
||
Provide instructions on how to use your project. For example: | ||
|
||
```shell | ||
pip install pdm | ||
|
||
``` | ||
|
||
- Change Configuration | ||
|
||
```shell | ||
nano pyproject.toml # Fix the name and email | ||
|
||
``` | ||
|
||
### Pydantic V2 | ||
|
||
Pydantic is a library for data validation and settings management based on Python type annotations. | ||
|
||
```shell | ||
nano .env | ||
``` | ||
|
||
### PM2 | ||
|
||
pm2 is a run-time and deployment management tool. | ||
|
||
```shell | ||
pm2 start pm2.json | ||
pm2 monit | ||
pm2 restart pm2.json | ||
pm2 stop pm2.json | ||
|
||
``` | ||
|
||
### dynaconf | ||
|
||
dynaconf is a Python library that provides a flexible and dynamic configuration management solution for your | ||
applications. It allows you to easily manage and access configuration settings from various sources such as environment | ||
variables, YAML files, JSON files, INI files, and more. | ||
|
||
### Pre-commit | ||
|
||
pre-commit is a framework for managing and maintaining multi-language pre-commit hooks. | ||
|
||
```shell | ||
pre-commit install | ||
pre-commit run --all-files | ||
|
||
``` | ||
|
||
### PDM | ||
|
||
pdm is a modern Python package manager with PEP 582 support. | ||
|
||
```shell | ||
add # Add package(s) to pyproject.toml and install them | ||
build # Build artifacts for distribution | ||
cache # Control the caches of PDM | ||
completion # Generate completion scripts for the given shell | ||
config # Display the current configuration | ||
export # Export the locked packages set to other formats | ||
fix # Fix the project problems according to the latest version of PDM | ||
import # Import project metadata from other formats | ||
info # Show the project information | ||
init # Initialize a pyproject.toml for PDM | ||
install # Install dependencies from lock file | ||
list # List packages installed in the current working set | ||
lock # Resolve and lock dependencies | ||
publish # Build and publish the project to PyPI | ||
remove # Remove packages from pyproject.toml | ||
run # Run commands or scripts with local packages loaded | ||
search # Search for PyPI packages | ||
show # Show the package information | ||
sync # Synchronize the current working set with lock file | ||
update # Update package(s) in pyproject.toml | ||
use # Use the given python version or path as base interpreter | ||
venv # Virtualenv management | ||
|
||
``` | ||
|
||
## License | ||
|
||
Provide information about the license your project is under. For example, if your project is under the MIT license, you | ||
could include the following text: | ||
This project is licensed under the MIT License - see the LICENSE.md file for details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2023/11/18 上午12:47 | ||
# @Author : sudoskys | ||
# @File : __init__.py | ||
# @Software: PyCharm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2023/11/18 上午12:18 | ||
# @File : controller.py | ||
# @Software: PyCharm | ||
|
||
from asgiref.sync import sync_to_async | ||
from loguru import logger | ||
from telebot import types | ||
from telebot import util, formatting | ||
from telebot.async_telebot import AsyncTeleBot | ||
from telebot.asyncio_helper import ApiTelegramException | ||
from telebot.asyncio_storage import StateMemoryStorage | ||
|
||
from setting.telegrambot import BotSetting | ||
|
||
StepCache = StateMemoryStorage() | ||
|
||
|
||
@sync_to_async | ||
def sync_to_async_func(): | ||
pass | ||
|
||
|
||
class BotRunner(object): | ||
def __init__(self): | ||
self.bot = AsyncTeleBot(BotSetting.token, state_storage=StepCache) | ||
|
||
async def run(self): | ||
logger.info("Bot Start") | ||
bot = self.bot | ||
if BotSetting.proxy_address: | ||
from telebot import asyncio_helper | ||
|
||
asyncio_helper.proxy = BotSetting.proxy_address | ||
logger.info("Proxy tunnels are being used!") | ||
|
||
@bot.message_handler( | ||
commands="help", chat_types=["private", "supergroup", "group"] | ||
) | ||
async def listen_help_command(message: types.Message): | ||
_message = await bot.reply_to( | ||
message=message, | ||
text=formatting.format_text( | ||
formatting.mbold("🥕 Help"), | ||
formatting.mlink( | ||
"🍀 Github", "https://github.com/sudoskys/TelegramBotTemplate" | ||
), | ||
), | ||
parse_mode="MarkdownV2", | ||
) | ||
|
||
try: | ||
await bot.polling( | ||
non_stop=True, allowed_updates=util.update_types, skip_pending=True | ||
) | ||
except ApiTelegramException as e: | ||
logger.opt(exception=e).exception("ApiTelegramException") | ||
except Exception as e: | ||
logger.exception(e) |
Oops, something went wrong.