Skip to main content

orch/cli/
notify.rs

1//! `orch notify` — send a Telegram message from the command line.
2//!
3//! Allows agents (and humans) to send Telegram messages directly by running:
4//!
5//! ```bash
6//! orch notify "Job completed successfully"
7//! orch notify --target 602365815 "Job failed: backup.sh exited with code 1"
8//! ```
9//!
10//! The `--target` flag overrides the `channels.telegram.chat_id` from config.
11//! If both are absent, the command returns an error.
12
13use crate::channels::telegram::TelegramChannel;
14use crate::channels::{Channel, OutgoingMessage};
15
16/// Send a Telegram notification message.
17///
18/// # Arguments
19///
20/// * `message` — the text to send
21/// * `target`  — optional chat_id override; falls back to `channels.telegram.chat_id`
22pub async fn send(message: &str, target: Option<&str>) -> anyhow::Result<()> {
23    // Resolve bot token
24    let token = crate::config::get("channels.telegram.bot_token").map_err(|_| {
25        anyhow::anyhow!(
26            "Telegram bot token not configured.\n\
27             Set `channels.telegram.bot_token` in ~/.orch/config.yml"
28        )
29    })?;
30
31    // Resolve chat_id: --target arg → config default
32    let chat_id: Option<String> = target
33        .map(|s| s.to_string())
34        .or_else(|| crate::config::get("channels.telegram.chat_id").ok());
35
36    if chat_id.is_none() {
37        anyhow::bail!(
38            "No Telegram chat_id configured.\n\
39             Use --target <chat_id> or set `channels.telegram.chat_id` in ~/.orch/config.yml"
40        );
41    }
42
43    // Build the channel with the resolved chat_id
44    let channel = TelegramChannel::new(token, chat_id.clone())?;
45
46    // Build the outgoing message
47    let msg = OutgoingMessage {
48        thread_id: chat_id.unwrap_or_default(),
49        body: message.to_string(),
50        reply_to: None,
51        metadata: serde_json::Value::Null,
52        topic_id: None,
53    };
54
55    channel.send(&msg).await?;
56
57    println!("Telegram message sent.");
58    Ok(())
59}