diff options
| author | Guillermo Serrahima <dev@vylion.com> | 2026-02-02 02:43:14 +0100 |
|---|---|---|
| committer | Guillermo Serrahima <dev@vylion.com> | 2026-02-02 02:43:14 +0100 |
| commit | 512915dd4587491dad19c13cde92daada5756d72 (patch) | |
| tree | 3fbf32282066486b59b3f1c8120a073e304a7a7e /main.py | |
| parent | 91a1bed08873ad7698f6f5e25324a69677dbcdac (diff) | |
| download | velascobot2-512915dd4587491dad19c13cde92daada5756d72.tar.xz velascobot2-512915dd4587491dad19c13cde92daada5756d72.zip | |
Initial commit. Achieved minimum functionality
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 84 |
1 files changed, 84 insertions, 0 deletions
@@ -0,0 +1,84 @@ +import logging +from pathlib import Path + +import click +import coloredlogs +import tomllib + +from velascobot2.bot import Bot +from velascobot2.messenger import Messenger + +logger = logging.getLogger(__name__) + + +def load_config(config_path: Path | None) -> dict: + assert config_path is not None + with open(config_path, "rb") as f: + return tomllib.load(f) + + +def run_bot(bot: Bot) -> None: + bot.app.run_polling(poll_interval=1.0) + assert "@" + bot.app.bot.username == bot.messenger.username + + +@click.command() +@click.option( + "--config", + type=click.Path(exists=True, path_type=Path), + default="./config.toml", + help="path to config.toml file (default: ./config.toml)", +) +@click.option("--token", help="telegram bot token (overrides config file)") +def main(config: Path | None, token: str | None) -> None: + if not token and not (config and config.exists()): + raise click.UsageError( + "must provide either --token, --config or a valid config file " + "at ./config.toml" + ) + cfg = load_config(config) + + bot_name = "Telegram Bot" + if token: + bot_token = token + else: + bot_token = cfg["bot"]["token"] + + bot_name = cfg.get("messenger", {}).get("name", bot_name) + bosses = cfg.get("messenger", {}).get("bosses", []) + nicknames = cfg.get("messenger", {}).get("nicknames", []) + reply = cfg.get("messenger", {}).get("reply", 0.1) + repeat = cfg.get("messenger", {}).get("repeat", 0.05) + wake_up = cfg.get("messenger", {}).get("wake_up", False) + mute_time = cfg.get("messenger", {}).get("mute_time", 60) + max_len = cfg.get("messenger", {}).get("max_len", 50) + + motd_chats = cfg.get("bot", {}).get("motd_chats", []) + motd_list = cfg.get("bot", {}).get("motd_list", []) + whitelist = cfg.get("bot", {}).get("whitelist", None) + + log_format = ( + "[{}][%(asctime)s]%(name)s::%(levelname)s:%(message)s" + ).format(bot_name.upper()) + + coloredlogs.install(level=logging.INFO, fmt=log_format) + + messenger = Messenger( + bot_name, + bosses, + nicknames, + reply, + repeat, + wake_up, + mute_time=mute_time, + max_len=max_len, + ) + + bot = Bot( + messenger, bot_token, motd_chats, motd_list, whitelist, testing=True + ) + run_bot(bot) + + +if __name__ == "__main__": + main() |
