1use anyhow::Result;
8use async_trait::async_trait;
9use oxios_gateway::plugin::{ChannelBundle, ChannelContext, ChannelPlugin};
10
11use crate::{TelegramChannel, TelegramSessionSettings};
12
13#[derive(Default)]
15pub struct TelegramPlugin;
16
17impl TelegramPlugin {
18 pub fn new() -> Self {
20 Self
21 }
22}
23
24#[async_trait]
25impl ChannelPlugin for TelegramPlugin {
26 fn name(&self) -> &str {
27 "telegram"
28 }
29
30 async fn setup(&self, ctx: ChannelContext) -> Result<ChannelBundle> {
31 let config = ctx.config.read().clone();
32 let token = std::env::var(&config.channels.telegram.bot_token_env).map_err(|_| {
33 anyhow::anyhow!(
34 "Telegram bot token not found. Set {} env var.",
35 config.channels.telegram.bot_token_env
36 )
37 })?;
38 let allowed = config.channels.telegram.allowed_users.clone();
39
40 let session_settings = TelegramSessionSettings {
41 rotation_hours: config.channels.telegram.session.rotation_hours,
42 max_messages_per_session: config.channels.telegram.session.max_messages,
43 };
44
45 let rotation_hours = session_settings.rotation_hours;
46 let max_messages = session_settings.max_messages_per_session;
47
48 let channel = TelegramChannel::new(token, allowed).with_session_settings(session_settings);
49
50 tracing::info!(
51 rotation_hours = rotation_hours,
52 max_messages = max_messages,
53 "Telegram channel created with session management"
54 );
55
56 Ok(ChannelBundle {
57 channel: Box::new(channel),
58 tasks: vec![],
59 })
60 }
61}