Skip to content

Commit 2afc95d

Browse files
committed
add telegram support
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
1 parent fc8a106 commit 2afc95d

10 files changed

Lines changed: 96 additions & 4 deletions

File tree

csp_bot/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from chatom import Channel, Message, User
1818

1919
from .bot import Bot
20-
from .bot_config import BotConfig, DiscordConfig, SlackConfig, SymphonyConfig
20+
from .bot_config import BotConfig, DiscordConfig, SlackConfig, SymphonyConfig, TelegramConfig
2121
from .commands import (
2222
BaseCommand,
2323
BaseCommandModel,
@@ -53,6 +53,7 @@
5353
"DiscordConfig",
5454
"SlackConfig",
5555
"SymphonyConfig",
56+
"TelegramConfig",
5657
# Commands
5758
"BaseCommand",
5859
"BaseCommandModel",

csp_bot/backends/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
from .discord import *
1111
from .slack import *
1212
from .symphony import *
13+
from .telegram import *

csp_bot/backends/telegram.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Telegram backend integration using chatom.
2+
3+
This module provides Telegram-specific types and adapters through chatom.
4+
"""
5+
6+
__all__ = (
7+
"TelegramConfig",
8+
"TelegramAdapter",
9+
"TelegramMessage",
10+
"TelegramUser",
11+
)
12+
13+
try:
14+
from chatom.telegram import (
15+
TelegramConfig,
16+
TelegramMessage,
17+
TelegramUser,
18+
)
19+
from csp_adapter_telegram import TelegramAdapter
20+
except ImportError:
21+
from chatom import Message as ChatomMessage, User as ChatomUser
22+
from pydantic import BaseModel
23+
24+
class TelegramConfig(BaseModel):
25+
"""Placeholder when chatom.telegram is not available."""
26+
27+
pass
28+
29+
TelegramAdapter = None
30+
TelegramMessage = ChatomMessage
31+
TelegramUser = ChatomUser

csp_bot/bot.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
SlackAdapter,
3535
SymphonyAdapter,
3636
SymphonyPresenceStatus,
37+
TelegramAdapter,
3738
)
3839
from .bot_config import BotConfig
3940
from .commands import (
@@ -108,6 +109,13 @@ def connect(self, channels: GatewayChannels) -> None:
108109
self._configs["symphony"] = self.config.symphony
109110
self._adapters["symphony"] = SymphonyAdapter(self.config.symphony.config)
110111

112+
# Initialize Telegram
113+
if self.config.telegram:
114+
if TelegramAdapter is None:
115+
raise ImportError("Telegram adapter not installed. Please install csp-adapter-telegram.")
116+
self._configs["telegram"] = self.config.telegram
117+
self._adapters["telegram"] = TelegramAdapter(self.config.telegram.config)
118+
111119
# Fetch bot info for all backends at startup
112120
for backend in self._adapters.keys():
113121
log.info(f"Fetching bot info for {backend}...")
@@ -167,7 +175,7 @@ def connect(self, channels: GatewayChannels) -> None:
167175
self._adapters["symphony"].publish_presence(presence)
168176

169177
# Set up user access queries
170-
for backend in ["symphony", "slack", "discord"]:
178+
for backend in ["symphony", "slack", "discord", "telegram"]:
171179
config = self._configs.get(backend)
172180
if config and config.user_access_channels:
173181
self._update_user_access(backend)

csp_bot/bot_config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
DiscordConfig as ChatomDiscordConfig,
1414
SlackConfig as ChatomSlackConfig,
1515
SymphonyConfig as ChatomSymphonyConfig,
16+
TelegramConfig as ChatomTelegramConfig,
1617
)
1718

1819
__all__ = (
@@ -21,6 +22,7 @@
2122
"DiscordConfig",
2223
"SlackConfig",
2324
"SymphonyConfig",
25+
"TelegramConfig",
2426
)
2527

2628

@@ -89,6 +91,15 @@ class SymphonyConfig(BackendConfig):
8991
)
9092

9193

94+
class TelegramConfig(BackendConfig):
95+
"""Telegram bot configuration."""
96+
97+
config: ChatomTelegramConfig = Field(
98+
default_factory=ChatomTelegramConfig,
99+
description="Chatom Telegram configuration.",
100+
)
101+
102+
92103
class BotConfig(BaseModel):
93104
"""Main bot configuration.
94105
@@ -99,6 +110,7 @@ class BotConfig(BaseModel):
99110
discord: Optional[DiscordConfig] = None
100111
slack: Optional[SlackConfig] = None
101112
symphony: Optional[SymphonyConfig] = None
113+
telegram: Optional[TelegramConfig] = None
102114

103115
ratelimit_seconds: float = Field(
104116
default=1.0,

csp_bot/commands/help.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ def execute(
8585
for cmd_key, name, help_text in helps:
8686
lines.append(f"| /{cmd_key} | {name} | {help_text} |")
8787
content = "\n".join(lines)
88+
elif command.backend == "telegram":
89+
# Telegram uses HTML formatting
90+
lines = ["<b>Bot Commands Help</b>", ""]
91+
for cmd_key, name, help_text in helps:
92+
lines.append(f"/{cmd_key} — <b>{name}</b>: {help_text}")
93+
content = "\n".join(lines)
8894
else:
8995
# Plain text fallback
9096
lines = ["Bot Commands Help", ""]

csp_bot/config/gateway/all.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ slack_bot_token: ???
1111
symphony_bot_name: ???
1212
symphony_cert: ???
1313
symphony_key: ???
14+
telegram_bot_name: ???
15+
telegram_bot_token: ???
1416

1517
modules:
1618
bot:
@@ -37,7 +39,13 @@ modules:
3739
_target_: csp_bot.SymphonyAdapterConfig
3840
cert_string: ${symphony_cert}
3941
key_string: ${symphony_key}
42+
telegram_config:
43+
_target_: csp_bot.TelegramConfig
44+
bot_name: ${telegram_bot_name}
45+
adapter_config:
46+
_target_: csp_bot.TelegramAdapterConfig
47+
bot_token: ${telegram_bot_token}
4048

4149
hydra:
4250
job:
43-
name: csp-bot--discord[${discord_bot_name}]-slack[${slack_bot_name}]-symphony[${symphony_bot_name}]
51+
name: csp-bot--discord[${discord_bot_name}]-slack[${slack_bot_name}]-symphony[${symphony_bot_name}]-telegram[${telegram_bot_name}]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# @package _global_
2+
defaults:
3+
- /modules
4+
- _self_
5+
6+
bot_name: ???
7+
bot_token: ???
8+
9+
modules:
10+
bot:
11+
_target_: csp_bot.Bot
12+
config:
13+
_target_: csp_bot.BotConfig
14+
telegram_config:
15+
_target_: csp_bot.TelegramConfig
16+
bot_name: ${bot_name}
17+
adapter_config:
18+
_target_: csp_bot.TelegramAdapterConfig
19+
bot_token: ${bot_token}
20+
21+
hydra:
22+
job:
23+
name: csp-bot-telegram[${bot_name}]

csp_bot/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"Backend",
2323
)
2424

25-
Backend = Literal["discord", "slack", "symphony"]
25+
Backend = Literal["discord", "slack", "symphony", "telegram"]
2626

2727

2828
def is_valid_url(url: str) -> bool:

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ keywords = [
1818
"csp",
1919
"stream-processing",
2020
"slack",
21+
"telegram",
2122
"chat",
2223
"chatbot",
2324
]
@@ -58,6 +59,7 @@ develop = [
5859
"csp-adapter-discord>=0.2,<0.3",
5960
"csp-adapter-slack>=0.4,<0.5",
6061
"csp-adapter-symphony>=0.4,<0.5",
62+
"csp-adapter-telegram>=0.1,<0.2",
6163
"hatchling",
6264
"mdformat",
6365
"mdformat-tables>=1",

0 commit comments

Comments
 (0)