opencrabs 0.3.13

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Telegram Send Tool
//!
//! Agent-callable tool for full Telegram control: send, reply, edit, delete,
//! pin/unpin, forward, media, polls, inline buttons, chat info, moderation,
//! and reactions. Always prefer this tool over http_request — credentials
//! are handled securely.

use super::error::Result;
use super::r#trait::{Tool, ToolCapability, ToolExecutionContext, ToolResult};
use crate::channels::telegram::TelegramState;
use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;
use teloxide::payloads::SendMessageSetters;
use teloxide::prelude::*;
use teloxide::types::{
    ChatId, InlineKeyboardButton, InlineKeyboardMarkup, InputFile, MessageId, ReactionType,
    ReplyParameters, UserId,
};

/// Tool for comprehensive Telegram bot control (19 actions).
pub struct TelegramSendTool {
    telegram_state: Arc<TelegramState>,
}

impl TelegramSendTool {
    pub fn new(telegram_state: Arc<TelegramState>) -> Self {
        Self { telegram_state }
    }
}

/// Extract a required non-empty string param, returning ToolResult::error on failure.
#[allow(clippy::result_large_err)]
fn get_str<'a>(input: &'a Value, key: &str) -> std::result::Result<&'a str, ToolResult> {
    match input.get(key).and_then(|v| v.as_str()) {
        Some(s) if !s.is_empty() => Ok(s),
        _ => Err(ToolResult::error(format!(
            "Missing required parameter '{key}'."
        ))),
    }
}

/// Parse a required integer param as i64.
#[allow(clippy::result_large_err)]
fn get_id(input: &Value, key: &str) -> std::result::Result<i64, ToolResult> {
    match input.get(key).and_then(|v| v.as_i64()) {
        Some(id) => Ok(id),
        None => Err(ToolResult::error(format!(
            "Missing required parameter '{key}' (must be an integer)."
        ))),
    }
}

/// Resolve chat_id: explicit param or owner fallback.
#[allow(clippy::result_large_err)]
async fn chat_or_err(input: &Value, state: &TelegramState) -> std::result::Result<i64, ToolResult> {
    if let Some(id) = input.get("chat_id").and_then(|v| v.as_i64()) {
        return Ok(id);
    }
    match state.owner_chat_id().await {
        Some(id) => Ok(id),
        None => Err(ToolResult::error(
            "No owner chat ID known yet and no 'chat_id' parameter provided. \
             The owner needs to send at least one message to the bot first, \
             or specify a chat_id."
                .to_string(),
        )),
    }
}

// Macro to early-return Ok(err_result) when a param helper returns Err.
macro_rules! pget {
    ($expr:expr) => {
        match $expr {
            Ok(v) => v,
            Err(e) => return Ok(e),
        }
    };
}

#[async_trait]
impl Tool for TelegramSendTool {
    fn name(&self) -> &str {
        "telegram_send"
    }

    fn description(&self) -> &str {
        "Full Telegram control: send messages, reply, edit, delete, pin/unpin, forward, \
         send photos/documents/locations/polls, inline buttons, get chat info, list admins, \
         check member count/status, ban/unban users, and set emoji reactions. \
         Always use telegram_send instead of http_request — credentials handled securely. \
         Requires Telegram to be connected first."
    }

    fn input_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": [
                        "send", "reply", "edit", "delete", "pin", "unpin",
                        "forward", "send_photo", "send_document", "send_location",
                        "send_poll", "send_buttons", "get_chat",
                        "get_chat_administrators", "get_chat_member_count", "get_chat_member",
                        "ban_user", "unban_user", "set_reaction"
                    ],
                    "description": "The Telegram action to perform"
                },
                "message": {
                    "type": "string",
                    "description": "Message text (send, reply, edit, send_buttons)"
                },
                "chat_id": {
                    "type": "integer",
                    "description": "Telegram chat ID. Omit to use owner's chat."
                },
                "message_id": {
                    "type": "integer",
                    "description": "Target message ID for reply/edit/delete/pin/unpin/forward/set_reaction"
                },
                "from_chat_id": {
                    "type": "integer",
                    "description": "Source chat ID for forward action"
                },
                "photo_url": {
                    "type": "string",
                    "description": "HTTPS URL of the photo for send_photo"
                },
                "document_url": {
                    "type": "string",
                    "description": "HTTPS URL of the document for send_document"
                },
                "latitude": {
                    "type": "number",
                    "description": "Latitude for send_location"
                },
                "longitude": {
                    "type": "number",
                    "description": "Longitude for send_location"
                },
                "poll_question": {
                    "type": "string",
                    "description": "Poll question text for send_poll"
                },
                "poll_options": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Array of poll option strings (2–10) for send_poll"
                },
                "buttons": {
                    "type": "array",
                    "items": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "text": {"type": "string"},
                                "callback_data": {"type": "string"}
                            }
                        }
                    },
                    "description": "2D array of button rows for send_buttons. Each button has 'text' and 'callback_data'."
                },
                "user_id": {
                    "type": "integer",
                    "description": "Telegram user ID for ban_user/unban_user"
                },
                "emoji": {
                    "type": "string",
                    "description": "Emoji for set_reaction (e.g. \"👍\")"
                }
            },
            "required": ["action"]
        })
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![ToolCapability::Network]
    }

    async fn execute(&self, input: Value, _context: &ToolExecutionContext) -> Result<ToolResult> {
        let action = match input.get("action").and_then(|v| v.as_str()) {
            Some(a) if !a.is_empty() => a.to_string(),
            _ => {
                return Ok(ToolResult::error(
                    "Missing required 'action' parameter.".to_string(),
                ));
            }
        };

        let bot = match self.telegram_state.bot().await {
            Some(b) => b,
            None => {
                return Ok(ToolResult::error(
                    "Telegram is not connected. Ask the user to connect Telegram first \
                     (use the telegram_connect tool)."
                        .to_string(),
                ));
            }
        };

        match action.as_str() {
            // ── send ─────────────────────────────────────────────────────────
            "send" => {
                let text = pget!(get_str(&input, "message")).to_string();
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let chunks = crate::channels::telegram::handler::split_message(&text, 4096);
                for chunk in chunks {
                    if let Err(e) = bot.send_message(ChatId(chat_id), chunk).await {
                        return Ok(ToolResult::error(format!("Failed to send: {e}")));
                    }
                }
                Ok(ToolResult::success(format!(
                    "Message sent to chat {chat_id}."
                )))
            }

            // ── reply ────────────────────────────────────────────────────────
            "reply" => {
                let text = pget!(get_str(&input, "message")).to_string();
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .send_message(ChatId(chat_id), text)
                    .reply_parameters(ReplyParameters::new(MessageId(message_id as i32)))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Reply sent to message {message_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to reply: {e}"))),
                }
            }

            // ── edit ─────────────────────────────────────────────────────────
            "edit" => {
                let text = pget!(get_str(&input, "message")).to_string();
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .edit_message_text(ChatId(chat_id), MessageId(message_id as i32), text)
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!("Message {message_id} edited."))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to edit: {e}"))),
                }
            }

            // ── delete ───────────────────────────────────────────────────────
            "delete" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .delete_message(ChatId(chat_id), MessageId(message_id as i32))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Message {message_id} deleted."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to delete: {e}"))),
                }
            }

            // ── pin ──────────────────────────────────────────────────────────
            "pin" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .pin_chat_message(ChatId(chat_id), MessageId(message_id as i32))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!("Message {message_id} pinned."))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to pin: {e}"))),
                }
            }

            // ── unpin ────────────────────────────────────────────────────────
            "unpin" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                match bot.unpin_chat_message(ChatId(chat_id)).await {
                    Ok(_) => Ok(ToolResult::success(
                        "Latest pinned message unpinned.".to_string(),
                    )),
                    Err(e) => Ok(ToolResult::error(format!("Failed to unpin: {e}"))),
                }
            }

            // ── forward ──────────────────────────────────────────────────────
            "forward" => {
                let to_chat = pget!(chat_or_err(&input, &self.telegram_state).await);
                let from_chat = pget!(get_id(&input, "from_chat_id"));
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .forward_message(
                        ChatId(to_chat),
                        ChatId(from_chat),
                        MessageId(message_id as i32),
                    )
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Message {message_id} forwarded from chat {from_chat} to {to_chat}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to forward: {e}"))),
                }
            }

            // ── send_photo ───────────────────────────────────────────────────
            "send_photo" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let url = pget!(get_str(&input, "photo_url")).to_string();
                let file = InputFile::url(url.parse().map_err(|e| {
                    crate::brain::tools::error::ToolError::Execution(format!(
                        "Invalid photo_url: {e}"
                    ))
                })?);
                match bot.send_photo(ChatId(chat_id), file).await {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Photo sent to chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to send photo: {e}"))),
                }
            }

            // ── send_document ────────────────────────────────────────────────
            "send_document" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let url = pget!(get_str(&input, "document_url")).to_string();
                let file = InputFile::url(url.parse().map_err(|e| {
                    crate::brain::tools::error::ToolError::Execution(format!(
                        "Invalid document_url: {e}"
                    ))
                })?);
                match bot.send_document(ChatId(chat_id), file).await {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Document sent to chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to send document: {e}"))),
                }
            }

            // ── send_location ────────────────────────────────────────────────
            "send_location" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let lat = match input.get("latitude").and_then(|v| v.as_f64()) {
                    Some(v) => v,
                    None => {
                        return Ok(ToolResult::error(
                            "Missing required 'latitude' parameter.".to_string(),
                        ));
                    }
                };
                let lng = match input.get("longitude").and_then(|v| v.as_f64()) {
                    Some(v) => v,
                    None => {
                        return Ok(ToolResult::error(
                            "Missing required 'longitude' parameter.".to_string(),
                        ));
                    }
                };
                match bot.send_location(ChatId(chat_id), lat, lng).await {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Location ({lat}, {lng}) sent to chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to send location: {e}"))),
                }
            }

            // ── send_poll ────────────────────────────────────────────────────
            "send_poll" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let question = pget!(get_str(&input, "poll_question")).to_string();
                let opts: Vec<String> = match input.get("poll_options").and_then(|v| v.as_array()) {
                    Some(arr) => arr
                        .iter()
                        .filter_map(|v| v.as_str().map(|s| s.to_string()))
                        .collect(),
                    None => {
                        return Ok(ToolResult::error(
                            "Missing required 'poll_options' parameter.".to_string(),
                        ));
                    }
                };
                if opts.len() < 2 {
                    return Ok(ToolResult::error(
                        "'poll_options' must have at least 2 options.".to_string(),
                    ));
                }
                match bot.send_poll(ChatId(chat_id), question, opts).await {
                    Ok(_) => Ok(ToolResult::success(format!("Poll sent to chat {chat_id}."))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to send poll: {e}"))),
                }
            }

            // ── send_buttons ─────────────────────────────────────────────────
            "send_buttons" => {
                let text = pget!(get_str(&input, "message")).to_string();
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let rows: Vec<Vec<InlineKeyboardButton>> =
                    match input.get("buttons").and_then(|v| v.as_array()) {
                        Some(outer) => outer
                            .iter()
                            .filter_map(|row| row.as_array())
                            .map(|row| {
                                row.iter()
                                    .filter_map(|btn| {
                                        let text =
                                            btn.get("text").and_then(|v| v.as_str())?.to_string();
                                        let data = btn
                                            .get("callback_data")
                                            .and_then(|v| v.as_str())?
                                            .to_string();
                                        Some(InlineKeyboardButton::callback(text, data))
                                    })
                                    .collect()
                            })
                            .collect(),
                        None => {
                            return Ok(ToolResult::error(
                                "Missing required 'buttons' parameter.".to_string(),
                            ));
                        }
                    };
                let keyboard = InlineKeyboardMarkup::new(rows);
                match bot
                    .send_message(ChatId(chat_id), text)
                    .reply_markup(keyboard)
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Message with buttons sent to chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!(
                        "Failed to send message with buttons: {e}"
                    ))),
                }
            }

            // ── get_chat ─────────────────────────────────────────────────────
            "get_chat" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                match bot.get_chat(ChatId(chat_id)).await {
                    Ok(chat) => {
                        let info = format!(
                            "Chat {}: type={:?}, title={:?}",
                            chat.id,
                            chat.kind,
                            chat.title()
                        );
                        Ok(ToolResult::success(info))
                    }
                    Err(e) => Ok(ToolResult::error(format!("Failed to get chat: {e}"))),
                }
            }

            // ── get_chat_administrators ────────────────────────────────────
            "get_chat_administrators" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                match bot.get_chat_administrators(ChatId(chat_id)).await {
                    Ok(admins) => {
                        let lines: Vec<String> = admins
                            .iter()
                            .map(|m| {
                                let u = &m.user;
                                let role = match m.kind {
                                    teloxide::types::ChatMemberKind::Owner { .. } => "owner",
                                    teloxide::types::ChatMemberKind::Administrator { .. } => {
                                        "admin"
                                    }
                                    _ => "member",
                                };
                                let handle = u
                                    .username
                                    .as_ref()
                                    .map(|h| format!(" @{h}"))
                                    .unwrap_or_default();
                                format!("- {} (id={}){} [{}]", u.first_name, u.id, handle, role)
                            })
                            .collect();
                        Ok(ToolResult::success(format!(
                            "Chat {} administrators ({}):\n{}",
                            chat_id,
                            admins.len(),
                            lines.join("\n")
                        )))
                    }
                    Err(e) => Ok(ToolResult::error(format!(
                        "Failed to get administrators: {e}"
                    ))),
                }
            }

            // ── get_chat_member_count ─────────────────────────────────────────
            "get_chat_member_count" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                match bot.get_chat_member_count(ChatId(chat_id)).await {
                    Ok(count) => Ok(ToolResult::success(format!(
                        "Chat {chat_id} has {count} members."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!(
                        "Failed to get member count: {e}"
                    ))),
                }
            }

            // ── get_chat_member ───────────────────────────────────────────────
            "get_chat_member" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let uid = pget!(get_id(&input, "user_id"));
                match bot
                    .get_chat_member(ChatId(chat_id), UserId(uid as u64))
                    .await
                {
                    Ok(member) => {
                        let u = &member.user;
                        let status = match member.kind {
                            teloxide::types::ChatMemberKind::Owner { .. } => "owner",
                            teloxide::types::ChatMemberKind::Administrator { .. } => {
                                "administrator"
                            }
                            teloxide::types::ChatMemberKind::Member => "member",
                            teloxide::types::ChatMemberKind::Restricted { .. } => "restricted",
                            teloxide::types::ChatMemberKind::Left => "left",
                            teloxide::types::ChatMemberKind::Banned { .. } => "banned",
                        };
                        let handle = u
                            .username
                            .as_ref()
                            .map(|h| format!(" @{h}"))
                            .unwrap_or_default();
                        Ok(ToolResult::success(format!(
                            "User {} (id={}){}: status={}",
                            u.first_name, u.id, handle, status
                        )))
                    }
                    Err(e) => Ok(ToolResult::error(format!("Failed to get chat member: {e}"))),
                }
            }

            // ── ban_user ─────────────────────────────────────────────────────
            "ban_user" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let user_id = pget!(get_id(&input, "user_id"));
                match bot
                    .ban_chat_member(ChatId(chat_id), UserId(user_id as u64))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "User {user_id} banned from chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to ban user: {e}"))),
                }
            }

            // ── unban_user ───────────────────────────────────────────────────
            "unban_user" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let user_id = pget!(get_id(&input, "user_id"));
                match bot
                    .unban_chat_member(ChatId(chat_id), UserId(user_id as u64))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "User {user_id} unbanned from chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to unban user: {e}"))),
                }
            }

            // ── set_reaction ─────────────────────────────────────────────────
            "set_reaction" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                let emoji = pget!(get_str(&input, "emoji")).to_string();
                let reactions = vec![ReactionType::Emoji {
                    emoji: emoji.clone(),
                }];
                match bot
                    .set_message_reaction(ChatId(chat_id), MessageId(message_id as i32))
                    .reaction(reactions)
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Reaction {emoji} set on message {message_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to set reaction: {e}"))),
                }
            }

            unknown => Ok(ToolResult::error(format!(
                "Unknown action '{unknown}'. Valid actions: send, reply, edit, delete, pin, \
                 unpin, forward, send_photo, send_document, send_location, send_poll, \
                 send_buttons, get_chat, get_chat_administrators, get_chat_member_count, \
                 get_chat_member, ban_user, unban_user, set_reaction"
            ))),
        }
    }
}