AntaresBot
A Telegram bot framework wrapping many things.
How to use
pip install antares_bot. If you want to use pika for logging,pip install antares_bot[pika]- Run
antares_botonce in working directory to generate thebot_cfg.py - Complete the
bot_cfg.py - Write your module in
modulesdirectory under working directory - Run
antares_botto start your bot
Examples
The documentation is far from completed, so here we only introduce a small part of features. We assume that you are already familiar with python-telegram-bot.
Creating a bot with command /echo. First create a file modules/echo.py.
from typing import TYPE_CHECKING
from antares_bot.module_base import TelegramBotModuleBase
from antares_bot.framework import command_callback_wrapper
if TYPE_CHECKING:
from telegram import Update
from antares_bot.context import RichCallbackContext
class Echo(TelegramBotModuleBase):
def mark_handlers(self):
return [self.echo]
@command_callback_wrapper
async def echo(self, update: "Update", context: "RichCallbackContext") -> bool:
assert update.message is not None
assert update.message.text is not None
text = update.message.text.strip()
if text.startswith("/echo"):
text = text[len("/echo"):].strip()
if text:
await self.reply(text)
antares_bot will automatically scan the files under modules directory and find the class derived from TelegramBotModuleBase, with the same name pattern as the file (CamelCase for class name, snake_case for file name, for example: my_example.py corresponds to class MyExample(TelegramBotModuleBase):)
Module interfaces:
- Override
mark_handlersto define the handlers. Handler wrappers can be found inantares_bot.framework. - Override
do_initto init.__init__is not recommanded. - Override
post_initto run some async function right after all modules are inited. - Override
do_stopto run some async function when exiting.
We use contexts to store the needed information for sending a message, so you only need to pass the message text when using the method reply.
Methods sending texts like reply (defined in TelegramBotBaseWrapper, bot_method_wrapper.py) have 4 different versions. These methods will automatically split the long text into parts, so it may send many messages. The original version returns id of the last message. V2 returns a list of ids of all messages (sorted). V3 returns the last Message object, and V4 returns all Message objects (sorted).
Creating a bot with command /timer
import time
from typing import TYPE_CHECKING
from antares_bot.framework import command_callback_wrapper
from antares_bot.module_base import TelegramBotModuleBase
from antares_bot.context_manager import callback_job_wrapper
if TYPE_CHECKING:
from telegram import Update
from antares_bot.context import RichCallbackContext
class Timer(TelegramBotModuleBase):
def mark_handlers(self):
return [self.timer]
@command_callback_wrapper
async def timer(self, update: "Update", context: "RichCallbackContext") -> bool:
assert update.message is not None
assert update.message.text is not None
@callback_job_wrapper
async def cb(context_new):
await self.reply("Time up!")
self.job_queue.run_once(cb, 5, name=f"{time.time()}")
return True
We use callback_job_wrapper to wrap the outer context for the nested function cb, and thus reply can reply to the correct message after 5 seconds.
i18n:
from antares_bot.basic_language import BasicLanguage as Lang
await self.send_to(self.get_master_id(), Lang.t(Lang.UNKNOWN_ERROR))
You can define any i18n config like Lang.UNKNOWN_ERROR
UNKNOWN_ERROR = {
"zh-CN": "哎呀,出现了未知的错误呢……",
"en": "Oops, an unknown error occurred...",
}
Call set_lang for each user to define their language.
Custom commands:
-
/execexecute some python code (master only).selfis defined to be theTelegramBotobject inbot_inst.py./exec import objgraph return list(map(lambda x:x.misfire_grace_time, objgraph.by_type("Job"))) # Execution succeeded, return value: [60, 60]
-
restart,stoprestart/stop the bot (master only). IfAntaresBotConfig.SYSTEMD_SERVICE_NAMEis configured,/restartwill try to callsystemctl restartfor you. IfAntaresBotConfig.PULL_WHEN_STOPis configured, these two commands will performgit pull. -
get_idsee/help get_id. -
helpcheck the docstring of command. For more information see/help help. -
debug_modeswitch the logging level toDEBUG(master only).
Also, you can start the bot by yourself, without calling antares_bot in the command line.
if __name__ == "__main__":
from antares_bot import __main__
inst = __main__.bootstrap()
inst.run()
Note
- Only support Python version >= 3.10
Metadata
Release files for antares-bot 21.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| antares_bot-21.4.tar.gz | 59.9 kB | Without provenance | Details for antares_bot-21.4.tar.gz |
Release files / antares_bot-21.4.tar.gz
| Download URL | antares_bot-21.4.tar.gz |
|---|---|
| Size | 59.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
60f426bb45c990f436317e83d73c6d325ea27399886dffe96093da8b1bff6988
|
|
BLAKE2b-256 checksum How to use checksums |
ffb82f4251e6fa7d355a958ee5893eca50497fff65668551b94cc942e6ddb01a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|
Security for antares-bot 21.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.