1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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=False
)
run_bot(bot)
if __name__ == "__main__":
main()
|