tg-cli 0.2.1

A "unix-like" utility for sending yourself Telegram messages from the terminal
Documentation
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use std::{
    io::{IsTerminal, Read},
    time::{Duration, Instant},
};

use clap::{Args, Parser, Subcommand};
use std::path::PathBuf;
use teloxide::{payloads::GetUpdatesSetters, prelude::Requester, types::UpdateKind};
use tg_cli::{
    BotConfigStatus, ParseMode, SecretServiceStatus, TgSession, TokenStatus, delete_bot_config,
    inspect_bot_config, list_profile_names, listen_config, send_tg_message,
};

use crate::setup::run_setup;

mod setup;

#[derive(Parser)]
#[command(
    name = "tg",
    version,
    about = "Send Telegram messages from the command line"
)]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,

    /// Profile to use (overrides TG_PROFILE environment variable)
    #[arg(short = 'p', long, global = true)]
    profile: Option<String>,

    /// Interpret escape sequences (\n, \t, \\)
    #[arg(short = 'e')]
    escape: bool,

    /// No trailing newline (strips trailing newline from input)
    #[arg(short = 'n')]
    no_newline: bool,

    /// Telegram parse mode: markdown or html
    #[arg(short = 'm', long, default_value = "markdown")]
    parse_mode: String,

    /// Suppress non-error output
    #[arg(short = 'q', long)]
    quiet: bool,

    /// Send silently (no device notification)
    #[arg(short = 's', long)]
    silent: bool,

    /// Interactive mode: stream stdin updates and edit a single Telegram message (max 1 update/s)
    #[arg(short = 'i', long)]
    interactive: bool,

    /// Seconds between interactive message edits
    #[arg(
        short = 'f',
        long,
        default_value_t = 1,
        value_name = "SECONDS",
        requires = "interactive",
        value_parser = clap::value_parser!(u64).range(1..)
    )]
    interactive_frequency: u64,

    /// Message text (reads stdin if empty)
    words: Vec<String>,
}

#[derive(Subcommand)]
enum Command {
    /// Run interactive setup to configure bot token and chat ID
    Setup,
    /// Listen for incoming Telegram messages and print them to stdout (stop with /eof)
    Listen,
    /// Send files as Telegram document attachments
    Attach(AttachArgs),
    /// Manage configuration
    Config(ConfigArgs),
}

#[derive(Args)]
struct AttachArgs {
    /// Files to attach
    #[arg(required = true)]
    files: Vec<PathBuf>,

    /// Send silently (no device notification)
    #[arg(short = 's', long)]
    silent: bool,

    /// Suppress non-error output
    #[arg(short = 'q', long)]
    quiet: bool,
}

#[derive(Args)]
struct ConfigArgs {
    #[command(subcommand)]
    action: ConfigAction,
}

#[derive(Subcommand)]
enum ConfigAction {
    /// Print current config path and contents
    Show,
    /// Delete config file (allows re-running setup)
    Reset,
}

fn resolve_profile(cli_arg: Option<String>) -> Option<String> {
    cli_arg.or_else(|| std::env::var("TG_PROFILE").ok())
}

fn validate_profile_name(name: &str) {
    if name.is_empty()
        || !name
            .chars()
            .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
    {
        eprintln!(
            "Invalid profile name '{name}': only alphanumeric characters, hyphens, and underscores are allowed."
        );
        std::process::exit(1);
    }
}

fn interpret_escapes(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('n') => result.push('\n'),
                Some('t') => result.push('\t'),
                Some('r') => result.push('\r'),
                Some('\\') => result.push('\\'),
                Some('0') => result.push('\0'),
                Some(other) => {
                    result.push('\\');
                    result.push(other);
                }
                None => result.push('\\'),
            }
        } else {
            result.push(c);
        }
    }
    result
}

#[tokio::main]
async fn main() {
    pretty_env_logger::init();

    let cli = Cli::parse();

    let profile = resolve_profile(cli.profile.clone());
    if let Some(ref name) = profile {
        validate_profile_name(name);
    }

    match cli.command {
        Some(Command::Setup) => run_setup(profile.as_deref()).await,
        Some(Command::Listen) => {
            if let Err(err) = run_listen(profile.as_deref()).await {
                eprintln!("{}", err);
                std::process::exit(1);
            }
        }
        Some(Command::Attach(args)) => {
            if let Err(err) = run_attach(args, profile.as_deref()).await {
                eprintln!("{}", err);
                std::process::exit(1);
            }
        }
        Some(Command::Config(args)) => run_config(args.action, profile.as_deref()).await,
        None => run_messgae(cli, profile.as_deref()).await,
    }
}

async fn run_messgae(cli: Cli, profile: Option<&str>) {
    if cli.interactive {
        if let Err(err) = run_interactive(cli, profile).await {
            eprintln!("{}", err);
            std::process::exit(1);
        }
        return;
    }

    let mut text = if cli.words.is_empty() {
        let mut buf = String::new();
        std::io::stdin()
            .read_to_string(&mut buf)
            .expect("failed to read stdin");
        buf
    } else {
        cli.words.join(" ")
    };

    if cli.escape {
        text = interpret_escapes(&text);
    }

    if cli.no_newline {
        text = text.trim_end_matches(['\n', '\r']).to_string();
    }

    let parse_mode = match cli.parse_mode.as_str() {
        "markdown" | "md" => ParseMode::Markdown,
        "html" => ParseMode::Html,
        other => {
            eprintln!("Unknown parse mode '{}'. Use 'markdown' or 'html'.", other);
            std::process::exit(1);
        }
    };

    if let Err(err) = send_tg_message(text, parse_mode, cli.silent, profile).await {
        eprintln!("{}", err);
        std::process::exit(1);
    }
}

async fn run_listen(profile: Option<&str>) -> Result<(), tg_cli::SendMessageError> {
    let (bot, target_chat) = listen_config(profile).await?;

    // Start from the next update so listen mode only captures new incoming messages.
    let mut offset: i32 = match bot.get_updates().timeout(0).await {
        Ok(existing) => existing
            .iter()
            .map(|u| i32::try_from(u.id.0).unwrap_or(i32::MAX).saturating_add(1))
            .max()
            .unwrap_or(0),
        Err(err) => return Err(tg_cli::SendMessageError::Request(err)),
    };

    loop {
        let updates = bot
            .get_updates()
            .offset(offset)
            .timeout(30)
            .await
            .map_err(tg_cli::SendMessageError::Request)?;

        for update in updates {
            offset = i32::try_from(update.id.0)
                .unwrap_or(i32::MAX)
                .saturating_add(1);

            let UpdateKind::Message(message) = update.kind else {
                continue;
            };

            if message.chat.id != target_chat {
                continue;
            }

            let Some(text) = message.text() else {
                continue;
            };

            if text.trim() == "/eof" {
                return Ok(());
            }

            println!("{}", text);
        }
    }
}

fn parse_mode_from_cli(raw: &str) -> ParseMode {
    match raw {
        "markdown" | "md" => ParseMode::Markdown,
        "html" => ParseMode::Html,
        other => {
            eprintln!("Unknown parse mode '{}'. Use 'markdown' or 'html'.", other);
            std::process::exit(1);
        }
    }
}

async fn push_update(
    session: &TgSession,
    message_id: &mut Option<i32>,
    text: String,
    parse_mode: ParseMode,
    silent: bool,
) -> Result<(), tg_cli::SendMessageError> {
    if text.is_empty() {
        return Ok(());
    }

    if let Some(id) = *message_id {
        session.edit_message(id, text, parse_mode).await
    } else {
        let id = session.send_message(text, parse_mode, silent).await?;
        *message_id = Some(id);
        Ok(())
    }
}

async fn run_interactive(cli: Cli, profile: Option<&str>) -> Result<(), tg_cli::SendMessageError> {
    let parse_mode = parse_mode_from_cli(&cli.parse_mode);
    let session = TgSession::from_config(profile).await?;
    let mut message_id: Option<i32> = None;
    let update_interval = Duration::from_secs(cli.interactive_frequency);

    if !cli.words.is_empty() {
        let initial = cli.words.join(" ");
        push_update(&session, &mut message_id, initial, parse_mode, cli.silent).await?;
    }

    if std::io::stdin().is_terminal() {
        return Ok(());
    }

    let mut stdin = std::io::stdin().lock();
    let mut byte = [0u8; 1];
    let mut frame = Vec::<u8>::new();
    let mut pending: Option<String> = None;
    let now = Instant::now();
    let mut last_sent = now.checked_sub(update_interval).unwrap_or(now);

    loop {
        let n = stdin.read(&mut byte).expect("failed to read stdin");
        if n == 0 {
            break;
        }

        match byte[0] {
            b'\n' | b'\r' => {
                if !frame.is_empty() {
                    let text = String::from_utf8_lossy(&frame).to_string();
                    pending = Some(text);
                    frame.clear();
                }
            }
            b => frame.push(b),
        }

        if pending.is_some() && last_sent.elapsed() >= update_interval {
            let text = pending.take().unwrap_or_default();
            push_update(&session, &mut message_id, text, parse_mode, cli.silent).await?;
            last_sent = Instant::now();
        }
    }

    if !frame.is_empty() {
        pending = Some(String::from_utf8_lossy(&frame).to_string());
    }

    if let Some(text) = pending {
        push_update(&session, &mut message_id, text, parse_mode, cli.silent).await?;
    }

    Ok(())
}

async fn run_attach(
    args: AttachArgs,
    profile: Option<&str>,
) -> Result<(), tg_cli::SendMessageError> {
    let session = TgSession::from_config(profile).await?;

    for path in &args.files {
        if !path.exists() {
            eprintln!("File not found: {}", path.display());
            std::process::exit(1);
        }

        session.send_document(path, args.silent).await?;

        if !args.quiet {
            eprintln!("Sent: {}", path.display());
        }
    }

    Ok(())
}

async fn run_config(action: ConfigAction, profile: Option<&str>) {
    match action {
        ConfigAction::Show => {
            let status = inspect_bot_config(profile).await;

            if let Some(name) = profile {
                println!("Profile: {name}");
            }
            println!("Config path: {}", status.path.display());
            println!(
                "Config file: {}",
                if status.config_file_present {
                    "present"
                } else {
                    "missing"
                }
            );
            println!(
                "Chat ID: {}",
                match status.chat_id {
                    Some(chat_id) => chat_id.to_string(),
                    None => "not configured".to_string(),
                }
            );
            print_config_status(status);

            if profile.is_none() {
                let names = list_profile_names();
                if !names.is_empty() {
                    println!("\nAvailable profiles:");
                    for name in &names {
                        let ps = inspect_bot_config(Some(name)).await;
                        let chat = match ps.chat_id {
                            Some(id) => id.to_string(),
                            None => "not configured".to_string(),
                        };
                        let token = match ps.token {
                            TokenStatus::SecretService => "configured (Secret Service)",
                            TokenStatus::PlaintextFallback => "configured (plaintext fallback)",
                            TokenStatus::NotConfigured => "not configured",
                        };
                        println!("  {name}: Chat ID: {chat}, Token: {token}");
                    }
                }
            }
        }
        ConfigAction::Reset => {
            let removed_any = delete_bot_config(profile).await;
            if removed_any {
                match profile {
                    None => println!("Configuration deleted. Run `tg setup` to reconfigure."),
                    Some(name) => println!(
                        "Profile '{name}' deleted. Run `tg --profile {name} setup` to reconfigure."
                    ),
                }
            } else {
                match profile {
                    None => println!("No configuration found."),
                    Some(name) => println!("Profile '{name}' not found."),
                }
            }
        }
    }
}

fn print_config_status(status: BotConfigStatus) {
    match status.secret_service {
        SecretServiceStatus::Available => println!("Secret Service: available"),
        SecretServiceStatus::Unavailable => println!("Secret Service: unavailable"),
        SecretServiceStatus::Error(err) => println!("Secret Service: error ({err})"),
    }

    let token = match status.token {
        TokenStatus::SecretService => "configured (Secret Service)",
        TokenStatus::PlaintextFallback => "configured (plaintext fallback)",
        TokenStatus::NotConfigured => "not configured",
    };
    println!("Token: {token}");
}