use super::types::*;
use crate::config::TelegramConfig;
use async_stream::stream;
use futures::Stream;
use plexus_core::plexus;
use plexus_core::serde_helpers;
#[derive(Clone)]
pub struct Telegram {
bot_token: String,
client: reqwest::Client,
}
impl Telegram {
pub async fn new(config: TelegramConfig) -> Result<Self, String> {
Ok(Self {
bot_token: config.bot_token,
client: reqwest::Client::new(),
})
}
}
#[plexus_macros::hub_methods(
namespace = "telegram",
version = "1.0.0",
description = "Send and receive messages via Telegram Bot API"
)]
impl Telegram {
#[plexus_macros::hub_method(
description = "Send a text message",
params(
chat_id = "Chat ID or username",
text = "Message text",
parse_mode = "Text formatting mode (optional)",
reply_to_message_id = "Reply to specific message (optional)"
)
)]
async fn send_message(
&self,
chat_id: String,
text: String,
parse_mode: Option<ParseMode>,
reply_to_message_id: Option<i64>,
) -> impl Stream<Item = SendMessageEvent> + Send + 'static {
let _params = SendMessageParams {
chat_id: chat_id.clone(),
text,
parse_mode,
reply_to_message_id,
};
stream! {
yield SendMessageEvent::Sent {
message_id: 12345,
chat_id,
};
}
}
#[plexus_macros::hub_method(
description = "Send a photo",
params(
chat_id = "Chat ID or username",
photo = "Photo file ID or URL",
caption = "Photo caption (optional)"
)
)]
async fn send_photo(
&self,
chat_id: String,
photo: String,
caption: Option<String>,
) -> impl Stream<Item = SendMessageEvent> + Send + 'static {
let _params = SendPhotoParams {
chat_id: chat_id.clone(),
photo,
caption,
};
stream! {
yield SendMessageEvent::Sent {
message_id: 12346,
chat_id,
};
}
}
#[plexus_macros::hub_method(
streaming,
description = "Listen for incoming updates (messages, callbacks, etc.)"
)]
async fn listen_updates(&self) -> impl Stream<Item = UpdateEvent> + Send + 'static {
stream! {
yield UpdateEvent::Error {
message: "Not implemented yet".to_string(),
};
}
}
}