botkit-telegram 0.2.0

Telegram implementation for botkit
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
use std::any::Any;

use botkit_core::action::AnyChatActionSender;
use botkit_core::{ContextData, OptionValue};

use crate::action::TelegramActionSender;
use crate::client::TelegramClient;
use crate::types::{EntityType, Update, UpdateKind};

/// Telegram context data - implements ContextData for platform abstraction
pub struct TelegramContextData {
    pub update: Update,
    // Client for API calls
    client: TelegramClient,
    // Values derived once at construction, so the accessors can hand out
    // borrows instead of rebuilding strings on every call.
    channel_id: String,
    chat_id: Option<i64>,
    user_id: String,
    user_name: String,
    command_name: Option<String>,
    command_args: Option<String>,
}

impl TelegramContextData {
    pub fn new(update: Update, client: TelegramClient) -> Self {
        let (chat_id, user, actor_chat) = match &update.kind {
            UpdateKind::Message(m) | UpdateKind::EditedMessage(m) => {
                (Some(m.chat.id), m.from.as_ref(), None)
            }
            // An inline-message callback has no chat to reply in.
            UpdateKind::CallbackQuery(cq) => {
                (cq.message.as_ref().map(|m| m.chat.id), Some(&cq.from), None)
            }
            UpdateKind::MessageReaction(r) => {
                (Some(r.chat.id), r.user.as_ref(), r.actor_chat.as_ref())
            }
            UpdateKind::Unknown => (None, None, None),
        };

        // Anonymous reactions come from a channel acting as itself; surface
        // the acting chat as the sender instead of an empty identity.
        let (user_id, user_name) = user
            .map(|u| (u.id.to_string(), display_name(u)))
            .or_else(|| {
                actor_chat.map(|c| {
                    (
                        c.id.to_string(),
                        c.title
                            .clone()
                            .or_else(|| c.username.clone())
                            .unwrap_or_default(),
                    )
                })
            })
            .unwrap_or_default();

        let (command_name, command_args) = extract_command(&update);

        Self {
            channel_id: chat_id.map(|id| id.to_string()).unwrap_or_default(),
            chat_id,
            user_id,
            user_name,
            command_name,
            command_args,
            update,
            client,
        }
    }

    /// Get the client for making API calls
    pub fn client(&self) -> &TelegramClient {
        &self.client
    }

    /// The numeric chat ID, absent for updates with no chat to reply in
    pub fn chat_id(&self) -> Option<i64> {
        self.chat_id
    }

    /// The forum topic this update belongs to, when the chat has topics.
    pub fn thread_id(&self) -> Option<i64> {
        match &self.update.kind {
            UpdateKind::Message(m) | UpdateKind::EditedMessage(m) => m.message_thread_id,
            UpdateKind::CallbackQuery(cq) => cq.message.as_ref()?.message_thread_id,
            UpdateKind::MessageReaction(_) | UpdateKind::Unknown => None,
        }
    }
}

/// Telegram only guarantees `first_name`; append the surname when present.
fn display_name(user: &crate::types::User) -> String {
    match &user.last_name {
        Some(last) => format!("{} {last}", user.first_name),
        None => user.first_name.clone(),
    }
}

/// Pull `/command@bot args` out of a message.
///
/// Entity offsets and lengths come straight off the wire and are counted in
/// UTF-16 code units, so they are resolved against the text's UTF-16 view and
/// every index is checked - a malformed update must not panic the webhook.
fn extract_command(update: &Update) -> (Option<String>, Option<String>) {
    let UpdateKind::Message(message) = &update.kind else {
        return (None, None);
    };

    let (Some(text), Some(entities)) = (&message.text, &message.entities) else {
        return (None, None);
    };

    // Telegram only treats a command as an invocation when it opens the message.
    let entity = entities
        .iter()
        .find(|e| matches!(e.entity_type, EntityType::BotCommand) && e.offset == 0);

    let Some(entity) = entity.filter(|e| e.length > 0) else {
        return (None, None);
    };

    let split = usize::try_from(entity.length)
        .ok()
        .and_then(|length| utf16_offset_to_byte_index(text, length));

    let Some(split) = split else {
        return (None, None);
    };

    let (command, rest) = text.split_at(split);

    // Strip the leading slash and any `@bot_name` suffix.
    let name = command
        .trim_start_matches('/')
        .split('@')
        .next()
        .unwrap_or_default();

    if name.is_empty() {
        return (None, None);
    }

    let args = rest.trim();
    (
        Some(name.to_string()),
        (!args.is_empty()).then(|| args.to_string()),
    )
}

/// Convert a UTF-16 code-unit offset into a byte index, or `None` if it runs
/// past the end of the string or lands mid-character.
fn utf16_offset_to_byte_index(text: &str, offset: usize) -> Option<usize> {
    if offset == 0 {
        return Some(0);
    }

    let mut units = 0;
    for (index, ch) in text.char_indices() {
        if units == offset {
            return Some(index);
        }
        units += ch.len_utf16();
    }

    (units == offset).then_some(text.len())
}

impl ContextData for TelegramContextData {
    fn channel_id(&self) -> &str {
        &self.channel_id
    }

    fn user_id(&self) -> &str {
        &self.user_id
    }

    fn user_name(&self) -> &str {
        &self.user_name
    }

    fn command_name(&self) -> Option<&str> {
        self.command_name.as_deref()
    }

    fn command_args(&self) -> Option<&str> {
        self.command_args.as_deref()
    }

    fn option(&self, _name: &str) -> Option<OptionValue> {
        // Telegram doesn't have structured options like Discord
        None
    }

    fn button_id(&self) -> Option<&str> {
        match &self.update.kind {
            UpdateKind::CallbackQuery(cq) => cq.data.as_deref(),
            _ => None,
        }
    }

    fn message_content(&self) -> Option<&str> {
        match &self.update.kind {
            UpdateKind::Message(m) | UpdateKind::EditedMessage(m) => {
                m.text.as_deref().or(m.caption.as_deref())
            }
            UpdateKind::CallbackQuery(cq) => cq.message.as_ref()?.text.as_deref().or(cq
                .message
                .as_ref()?
                .caption
                .as_deref()),
            UpdateKind::MessageReaction(_) | UpdateKind::Unknown => None,
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn action_sender(&self) -> Option<AnyChatActionSender> {
        Some(AnyChatActionSender::new(TelegramActionSender::new(
            self.client.clone(),
            self.chat_id?,
            self.thread_id(),
        )))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn update(json: serde_json::Value) -> Update {
        serde_json::from_value(json).expect("valid update")
    }

    fn message(text: &str, entities: serde_json::Value) -> Update {
        update(serde_json::json!({
            "update_id": 1,
            "message": {
                "message_id": 1,
                "date": 0,
                "chat": { "id": 42, "type": "private" },
                "from": { "id": 7, "is_bot": false, "first_name": "Ada", "last_name": "Lovelace" },
                "text": text,
                "entities": entities,
            }
        }))
    }

    fn command_entity(length: i64) -> serde_json::Value {
        serde_json::json!([{ "type": "bot_command", "offset": 0, "length": length }])
    }

    #[test]
    fn parses_a_command_with_arguments() {
        let update = message("/greet world and beyond", command_entity(6));
        let (name, args) = extract_command(&update);
        assert_eq!(name.as_deref(), Some("greet"));
        assert_eq!(args.as_deref(), Some("world and beyond"));
    }

    #[test]
    fn parses_a_bare_command() {
        let (name, args) = extract_command(&message("/ping", command_entity(5)));
        assert_eq!(name.as_deref(), Some("ping"));
        assert_eq!(args, None);
    }

    #[test]
    fn strips_the_bot_mention_suffix() {
        let (name, args) = extract_command(&message("/ping@my_bot now", command_entity(12)));
        assert_eq!(name.as_deref(), Some("ping"));
        assert_eq!(args.as_deref(), Some("now"));
    }

    #[test]
    fn ignores_commands_that_do_not_open_the_message() {
        let update = message(
            "see /ping",
            serde_json::json!([{ "type": "bot_command", "offset": 4, "length": 5 }]),
        );
        assert_eq!(extract_command(&update), (None, None));
    }

    #[test]
    fn ignores_messages_without_a_command_entity() {
        let update = message("just chatting", serde_json::json!([]));
        assert_eq!(extract_command(&update), (None, None));
    }

    #[test]
    fn entity_lengths_are_counted_in_utf16_code_units() {
        // The emoji is one char but two UTF-16 units, so the arguments start at
        // byte 9 even though the command is 7 chars long.
        let update = message("/wave🎉 hi", command_entity(7));
        let (name, args) = extract_command(&update);
        assert_eq!(name.as_deref(), Some("wave🎉"));
        assert_eq!(args.as_deref(), Some("hi"));
    }

    #[test]
    fn out_of_range_entity_lengths_do_not_panic() {
        for length in [-1, 0, 6, 9999] {
            let update = message("/ping", command_entity(length));
            assert_eq!(extract_command(&update), (None, None), "length {length}");
        }
    }

    #[test]
    fn entity_lengths_landing_mid_character_do_not_panic() {
        // Length 1 splits the 2-unit emoji in half.
        let update = message("🎉x", command_entity(1));
        assert_eq!(extract_command(&update), (None, None));
    }

    #[test]
    fn callback_queries_expose_their_button_and_chat() {
        let update = update(serde_json::json!({
            "update_id": 2,
            "callback_query": {
                "id": "cb1",
                "from": { "id": 7, "is_bot": false, "first_name": "Ada" },
                "chat_instance": "x",
                "data": "confirm_yes",
                "message": {
                    "message_id": 1,
                    "date": 0,
                    "chat": { "id": 42, "type": "private" },
                    "text": "Are you sure?"
                }
            }
        }));

        let data = TelegramContextData::new(update, TelegramClient::new("token"));
        assert_eq!(data.chat_id(), Some(42));
        assert_eq!(data.button_id(), Some("confirm_yes"));
        assert_eq!(data.command_name(), None);
        assert_eq!(data.message_content(), Some("Are you sure?"));
        assert_eq!(data.user_name(), "Ada");
    }

    #[test]
    fn inline_callback_queries_have_no_chat_to_reply_in() {
        let update = update(serde_json::json!({
            "update_id": 3,
            "callback_query": {
                "id": "cb2",
                "from": { "id": 7, "is_bot": false, "first_name": "Ada" },
                "chat_instance": "x",
                "inline_message_id": "inline-1",
                "data": "x"
            }
        }));

        let data = TelegramContextData::new(update, TelegramClient::new("token"));
        assert_eq!(data.chat_id(), None);
        assert_eq!(data.channel_id(), "");
        assert!(data.action_sender().is_none());
    }

    #[test]
    fn messages_expose_the_sender_and_chat() {
        let data = TelegramContextData::new(
            message("/greet you", command_entity(6)),
            TelegramClient::new("token"),
        );
        assert_eq!(data.chat_id(), Some(42));
        assert_eq!(data.channel_id(), "42");
        assert_eq!(data.user_id(), "7");
        assert_eq!(data.user_name(), "Ada Lovelace");
        assert_eq!(data.command_name(), Some("greet"));
        assert_eq!(data.command_args(), Some("you"));
        assert!(data.action_sender().is_some());
    }

    #[test]
    fn media_caption_serves_as_message_content() {
        // A photo/document message has no `text`; its caption is the text.
        let update = update(serde_json::json!({
            "update_id": 5,
            "message": {
                "message_id": 9,
                "date": 0,
                "chat": { "id": 42, "type": "private" },
                "from": { "id": 7, "is_bot": false, "first_name": "Ada" },
                "caption": "look at this",
                "photo": [
                    { "file_id": "p1", "file_unique_id": "u1", "width": 90, "height": 90 },
                    { "file_id": "p2", "file_unique_id": "u2", "width": 800, "height": 600 }
                ]
            }
        }));

        let data = TelegramContextData::new(update, TelegramClient::new("token"));
        assert_eq!(data.message_content(), Some("look at this"));
        assert_eq!(data.chat_id(), Some(42));
    }

    #[test]
    fn unsupported_update_kinds_are_inert() {
        let update = update(serde_json::json!({
            "update_id": 4,
            "poll": { "id": "p1" }
        }));

        let data = TelegramContextData::new(update, TelegramClient::new("token"));
        assert_eq!(data.chat_id(), None);
        assert_eq!(data.user_id(), "");
        assert_eq!(data.command_name(), None);
        assert_eq!(data.button_id(), None);
        assert_eq!(data.message_content(), None);
    }
}