this repo has no description
1import asyncio
2import logging
3from pathlib import Path
4
5import click
6import coloredlogs
7import tomllib
8
9from velascobot2.bot import Bot
10from velascobot2.messenger import Messenger
11
12logger = logging.getLogger(__name__)
13
14
15def load_config(config_path: Path | None) -> dict:
16 assert config_path is not None
17 with open(config_path, "rb") as f:
18 return tomllib.load(f)
19
20
21def run_bot(bot: Bot) -> None:
22 asyncio.run(bot.wake_up())
23 bot.app.run_polling(poll_interval=0.5)
24 assert "@" + bot.app.bot.username == bot.messenger.username
25
26
27@click.command()
28@click.option(
29 "--config",
30 type=click.Path(exists=True, path_type=Path),
31 default="./config.toml",
32 help="path to config.toml file (default: ./config.toml)",
33)
34@click.option("--token", help="telegram bot token (overrides config file)")
35def main(config: Path | None, token: str | None) -> None:
36 if not token and not (config and config.exists()):
37 raise click.UsageError(
38 "must provide either --token, --config or a valid config file "
39 "at ./config.toml"
40 )
41 cfg = load_config(config)
42
43 bot_name = "Telegram Bot"
44 if token:
45 bot_token = token
46 else:
47 bot_token = cfg["bot"]["token"]
48
49 bot_name = cfg.get("messenger", {}).get("name", bot_name)
50 bosses = cfg.get("messenger", {}).get("bosses", [])
51 nicknames = cfg.get("messenger", {}).get("nicknames", [])
52 reply = cfg.get("messenger", {}).get("reply", 0.1)
53 repeat = cfg.get("messenger", {}).get("repeat", 0.05)
54 wake_up = cfg.get("messenger", {}).get("wake_up", False)
55 mute_time = cfg.get("messenger", {}).get("mute_time", 120)
56 max_len = cfg.get("messenger", {}).get("max_len", 50)
57
58 motd_chats = cfg.get("bot", {}).get("motd_chats", [])
59 motd_list = cfg.get("bot", {}).get("motd_list", [])
60 whitelist = cfg.get("bot", {}).get("whitelist", None)
61
62 log_format = (
63 "[{}][%(asctime)s]%(name)s::%(levelname)s:%(message)s"
64 ).format(bot_name.upper())
65
66 coloredlogs.install(level=logging.INFO, fmt=log_format)
67
68 messenger = Messenger(
69 bot_name,
70 bosses,
71 nicknames,
72 reply,
73 repeat,
74 wake_up,
75 mute_time=mute_time,
76 max_len=max_len,
77 )
78
79 bot = Bot(
80 messenger, bot_token, motd_chats, motd_list, whitelist, testing=False
81 )
82 run_bot(bot)
83
84
85if __name__ == "__main__":
86 main()