kcode-tg-kennedy-bot 0.5.7

A host-integrated Telegram Bot API facade over durable transport state
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
use kcode_telegram_transport_state::{DeliveryMedia, SentMessage};
use teloxide::{
    payloads::SendDocumentSetters,
    requests::Request as TelegramRequest,
    types::{InputFile, ReplyParameters},
};

use super::*;

const TELEGRAM_CAPTION_LIMIT: usize = 1_024;
const MAX_FILE_NAME_CHARACTERS: usize = 255;
const MAX_MIME_TYPE_CHARACTERS: usize = 255;

#[derive(Debug, Default)]
struct OutboundFile {
    conversation_id: Option<String>,
    expected_conversation_id: Option<String>,
    kind: Option<String>,
    explicit_file_name: Option<String>,
    mime_type: Option<String>,
    caption: Option<String>,
    complete: bool,
    bytes: Option<Vec<u8>>,
}

fn outbound_file(
    attachment: Attachment,
    maximum_bytes: usize,
    conversation_id: Option<String>,
    expected_conversation_id: Option<String>,
    complete: bool,
) -> Result<OutboundFile, ApiError> {
    if attachment.bytes.len() > maximum_bytes {
        return Err(ApiError::bad(format!(
            "The file exceeds the configured {maximum_bytes}-byte Telegram media limit."
        )));
    }
    Ok(OutboundFile {
        conversation_id,
        expected_conversation_id,
        kind: attachment.kind,
        explicit_file_name: attachment.file_name,
        mime_type: attachment.media_type,
        caption: attachment.caption,
        complete,
        bytes: Some(attachment.bytes),
    })
}

impl Service {
    pub async fn send_cold_private_attachment(
        &self,
        telegram_user_id: i64,
        attachment: Attachment,
    ) -> Result<Value, Error> {
        let input = outbound_file(attachment, self.state.max_voice_bytes, None, None, false)?;
        send_private_attachment(self.state.clone(), telegram_user_id, input).await
    }

    pub async fn send_private_attachment(
        &self,
        telegram_user_id: i64,
        conversation_id: String,
        expected_conversation_id: Option<String>,
        attachment: Attachment,
    ) -> Result<Value, Error> {
        let input = outbound_file(
            attachment,
            self.state.max_voice_bytes,
            Some(conversation_id),
            expected_conversation_id,
            false,
        )?;
        send_private_attachment(self.state.clone(), telegram_user_id, input).await
    }

    pub async fn send_group_attachment(
        &self,
        group_id: String,
        attachment: Attachment,
    ) -> Result<Value, Error> {
        let input = outbound_file(attachment, self.state.max_voice_bytes, None, None, false)?;
        send_group_attachment(self.state.clone(), group_id, input).await
    }

    pub async fn send_event_attachment(
        &self,
        event_id: String,
        conversation_id: String,
        attachment: Attachment,
        complete: bool,
    ) -> Result<Value, Error> {
        let native = attachment.kind.is_some();
        let input = outbound_file(
            attachment,
            self.state.max_voice_bytes,
            Some(conversation_id),
            None,
            complete,
        )?;
        if native {
            send_event_media(self.state.clone(), event_id, input).await
        } else {
            send_event_file(self.state.clone(), event_id, input).await
        }
    }
}

async fn send_private_attachment(
    state: AppState,
    telegram_user_id: i64,
    input: OutboundFile,
) -> Result<Value, ApiError> {
    let started = Instant::now();
    let delivery = match input.conversation_id.as_ref() {
        Some(conversation_id) => Some(
            state
                .transport
                .private_delivery(
                    telegram_user_id,
                    conversation_id.clone(),
                    input.expected_conversation_id.clone(),
                )
                .map_err(ApiError::state)?,
        ),
        None if input.expected_conversation_id.is_some() => {
            return Err(ApiError::bad(
                "expectedConversationId requires conversationId.",
            ));
        }
        None => None,
    };
    let cold_delivery;
    let target = if let Some(delivery) = delivery.as_ref() {
        delivery
    } else {
        cold_delivery = state
            .transport
            .cold_private_delivery(telegram_user_id)
            .map_err(ApiError::state)?;
        &cold_delivery
    };
    let kind = input
        .kind
        .as_deref()
        .map(|value| {
            native_media::NativeMediaKind::parse(value).ok_or_else(|| {
                ApiError::bad(
                    "kind must be photo, video, animation, audio, video_note, or sticker.",
                )
            })
        })
        .transpose()?;
    validate_caption(input.caption.as_deref(), kind, "attachment")?;
    let caption = input.caption.as_deref().and_then(nonempty_verbatim);
    let bytes = required_bytes(input.bytes)?;
    let supplied_file_name = input.explicit_file_name.as_deref();
    if let Some(file_name) = supplied_file_name {
        validate_file_name(file_name)?;
    }
    let mime_type = input.mime_type.as_deref().unwrap_or_else(|| {
        kind.map(|kind| kind.fallback_mime(supplied_file_name))
            .unwrap_or("application/octet-stream")
    });
    validate_mime_type(mime_type)?;
    let file_name = supplied_file_name
        .map(ToOwned::to_owned)
        .unwrap_or_else(|| {
            kind.map(|kind| kind.default_file_name(telegram_user_id, mime_type))
                .unwrap_or_else(|| format!("attachment-{telegram_user_id}.bin"))
        });
    validate_file_name(&file_name)?;
    let bot = state.bot.as_ref().ok_or_else(ApiError::unavailable)?;
    let sent = send_attachment(
        bot,
        TelegramAttachment {
            chat_id: target.chat_id(),
            kind,
            bytes,
            file_name: &file_name,
            caption,
            reply_parameters: None,
            label: "private attachment",
        },
    )
    .await?;
    if let Some(delivery) = delivery {
        delivery.record_accepted().map_err(ApiError::state)?;
        tracing::info!(
            %telegram_user_id,
            conversation_id=%input.conversation_id.as_deref().expect("bound delivery"),
            duration_ms=started.elapsed().as_millis(),
            "Telegram session-bound direct-message attachment"
        );
    } else {
        tracing::info!(
            %telegram_user_id,
            duration_ms=started.elapsed().as_millis(),
            "Telegram cold direct-message attachment"
        );
    }
    let mut response = json!({
        "telegramUserId":telegram_user_id,
        "kind":kind.map(|kind| kind.as_str()).unwrap_or("document"),
        "fileName":file_name,
        "mimeType":mime_type,
        "telegramMessageId":i64::from(sent.id.0),
    });
    if let Some(conversation_id) = input.conversation_id {
        response["conversationId"] = json!(conversation_id);
    }
    Ok(response)
}

async fn send_group_attachment(
    state: AppState,
    group_id: String,
    input: OutboundFile,
) -> Result<Value, ApiError> {
    let started = Instant::now();
    let group_id = validate_opaque_group_id(&group_id)?.to_owned();
    let kind = input
        .kind
        .as_deref()
        .map(|value| {
            native_media::NativeMediaKind::parse(value).ok_or_else(|| {
                ApiError::bad(
                    "kind must be photo, video, animation, audio, video_note, or sticker.",
                )
            })
        })
        .transpose()?;
    validate_caption(input.caption.as_deref(), kind, "attachment")?;
    let caption = input.caption.as_deref().and_then(nonempty_verbatim);
    let bytes = required_bytes(input.bytes)?;
    let supplied_file_name = input.explicit_file_name.as_deref();
    if let Some(file_name) = supplied_file_name {
        validate_file_name(file_name)?;
    }
    let mime_type = input.mime_type.as_deref().unwrap_or_else(|| {
        kind.map(|kind| kind.fallback_mime(supplied_file_name))
            .unwrap_or("application/octet-stream")
    });
    validate_mime_type(mime_type)?;
    let file_name = supplied_file_name
        .map(ToOwned::to_owned)
        .unwrap_or_else(|| {
            kind.map(|kind| kind.default_file_name(0, mime_type))
                .unwrap_or_else(|| "attachment.bin".into())
        });
    validate_file_name(&file_name)?;
    let delivery = validated_group_delivery(&state, &group_id).await?;
    let bot = state.bot.as_ref().ok_or_else(ApiError::unavailable)?;
    let sent = send_attachment(
        bot,
        TelegramAttachment {
            chat_id: delivery.chat_id(),
            kind,
            bytes,
            file_name: &file_name,
            caption,
            reply_parameters: None,
            label: "cold group attachment",
        },
    )
    .await?;
    tracing::info!(
        %group_id,
        duration_ms=started.elapsed().as_millis(),
        "Telegram cold group attachment"
    );
    Ok(json!({
        "groupId":group_id,
        "kind":kind.map(|kind| kind.as_str()).unwrap_or("document"),
        "fileName":file_name,
        "mimeType":mime_type,
        "telegramMessageId":i64::from(sent.id.0),
    }))
}

async fn send_event_file(
    state: AppState,
    event_id: String,
    input: OutboundFile,
) -> Result<Value, ApiError> {
    let conversation_id = input
        .conversation_id
        .as_deref()
        .ok_or_else(|| ApiError::bad("conversationId is required."))?;
    let file_name = input
        .explicit_file_name
        .as_deref()
        .ok_or_else(|| ApiError::bad("The file must have a fileName."))?;
    validate_file_name(file_name)?;
    let mime_type = input
        .mime_type
        .as_deref()
        .unwrap_or("application/octet-stream");
    validate_mime_type(mime_type)?;
    let caption = input.caption.as_deref().and_then(nonempty_verbatim);
    if caption.is_some_and(|value| value.encode_utf16().count() > TELEGRAM_CAPTION_LIMIT) {
        return Err(ApiError::bad(
            "The Telegram file caption exceeds 1024 UTF-16 code units.",
        ));
    }
    let bytes = required_bytes(input.bytes)?;
    let delivery = state
        .transport
        .event_delivery(&event_id, conversation_id)
        .map_err(ApiError::state)?;
    let event = delivery.event();
    let reply = group_reply_parameters(event);
    let bot = state.bot.as_ref().ok_or_else(ApiError::unavailable)?;
    let sent = send_attachment(
        bot,
        TelegramAttachment {
            chat_id: event.chat_id,
            kind: None,
            bytes: bytes.clone(),
            file_name,
            caption,
            reply_parameters: reply,
            label: "file",
        },
    )
    .await?;
    let sent_message = SentMessage {
        message_id: i64::from(sent.id.0),
        text: sent.text().unwrap_or("").into(),
        sent_at: sent.date.to_rfc3339(),
    };
    let event = delivery
        .record_document(
            sent_message,
            DeliveryMedia {
                kind: "document".into(),
                bytes,
                mime_type: mime_type.into(),
                file_name: file_name.into(),
                caption: caption.map(ToOwned::to_owned),
                duration_seconds: None,
            },
            input.complete,
        )
        .map_err(ApiError::state)?;
    Ok(json!({
        "event":event,
        "fileName":file_name,
        "mimeType":mime_type,
        "telegramMessageId":i64::from(sent.id.0),
        "complete":input.complete,
    }))
}

async fn send_event_media(
    state: AppState,
    event_id: String,
    input: OutboundFile,
) -> Result<Value, ApiError> {
    let conversation_id = input
        .conversation_id
        .as_deref()
        .ok_or_else(|| ApiError::bad("conversationId is required."))?;
    let kind_text = input
        .kind
        .as_deref()
        .ok_or_else(|| ApiError::bad("kind is required."))?;
    let kind = native_media::NativeMediaKind::parse(kind_text).ok_or_else(|| {
        ApiError::bad("kind must be photo, video, animation, audio, video_note, or sticker.")
    })?;
    validate_caption(input.caption.as_deref(), Some(kind), "media")?;
    let caption = input.caption.as_deref().and_then(nonempty_verbatim);
    let bytes = required_bytes(input.bytes)?;
    let delivery = state
        .transport
        .event_delivery(&event_id, conversation_id)
        .map_err(ApiError::state)?;
    let event = delivery.event();
    let supplied_file_name = input.explicit_file_name.as_deref();
    if let Some(file_name) = supplied_file_name {
        validate_file_name(file_name)?;
    }
    let mime_type = input
        .mime_type
        .as_deref()
        .unwrap_or_else(|| kind.fallback_mime(supplied_file_name));
    validate_mime_type(mime_type)?;
    let file_name = supplied_file_name
        .map(ToOwned::to_owned)
        .unwrap_or_else(|| kind.default_file_name(event.message_id, mime_type));
    validate_file_name(&file_name)?;
    let reply = group_reply_parameters(event);
    let bot = state.bot.as_ref().ok_or_else(ApiError::unavailable)?;
    let sent = send_attachment(
        bot,
        TelegramAttachment {
            chat_id: event.chat_id,
            kind: Some(kind),
            bytes: bytes.clone(),
            file_name: &file_name,
            caption,
            reply_parameters: reply,
            label: "native media",
        },
    )
    .await?;
    let duration_seconds = native_media::message_duration(&sent, kind);
    let sent_message = SentMessage {
        message_id: i64::from(sent.id.0),
        text: sent.text().unwrap_or("").into(),
        sent_at: sent.date.to_rfc3339(),
    };
    let event = delivery
        .record_native_media(
            sent_message,
            DeliveryMedia {
                kind: kind.as_str().into(),
                bytes,
                mime_type: mime_type.into(),
                file_name: file_name.clone(),
                caption: caption.map(ToOwned::to_owned),
                duration_seconds,
            },
            input.complete,
        )
        .map_err(ApiError::state)?;
    Ok(json!({
        "event":event,
        "kind":kind.as_str(),
        "fileName":file_name,
        "mimeType":mime_type,
        "telegramMessageId":i64::from(sent.id.0),
        "complete":input.complete,
    }))
}

struct TelegramAttachment<'a> {
    chat_id: i64,
    kind: Option<native_media::NativeMediaKind>,
    bytes: Vec<u8>,
    file_name: &'a str,
    caption: Option<&'a str>,
    reply_parameters: Option<ReplyParameters>,
    label: &'a str,
}

async fn send_attachment(
    bot: &Bot,
    attachment: TelegramAttachment<'_>,
) -> Result<Message, ApiError> {
    let result = if let Some(kind) = attachment.kind {
        native_media::send_native_media(
            bot,
            attachment.chat_id,
            kind,
            &attachment.bytes,
            attachment.file_name,
            attachment.caption,
            attachment.reply_parameters,
        )
        .await
    } else {
        let mut request = bot.send_document(
            ChatId(attachment.chat_id),
            InputFile::memory(attachment.bytes).file_name(attachment.file_name.to_owned()),
        );
        if let Some(caption) = attachment.caption {
            request = request.caption(caption.to_owned());
        }
        if let Some(reply_parameters) = attachment.reply_parameters {
            request = request.reply_parameters(reply_parameters);
        }
        telegram_requests::retry_request("send_document", || request.clone().send()).await
    };
    result.map_err(|error| {
        tracing::warn!(
            chat_id = attachment.chat_id,
            error_class = telegram_requests::request_error_class(&error),
            label = attachment.label,
            "Telegram attachment send failed"
        );
        let message = if attachment.label == "private attachment"
            || attachment.label == "cold group attachment"
        {
            "Telegram did not accept the attachment."
        } else if attachment.kind.is_some() {
            "Telegram did not accept the native media."
        } else {
            "Telegram did not accept the file."
        };
        ApiError::new("telegram_send_failed", message)
    })
}

fn required_bytes(bytes: Option<Vec<u8>>) -> Result<Vec<u8>, ApiError> {
    let bytes = bytes.ok_or_else(|| ApiError::bad("A nonempty file part is required."))?;
    if bytes.is_empty() {
        return Err(ApiError::bad("A nonempty file part is required."));
    }
    Ok(bytes)
}

fn validate_caption(
    caption: Option<&str>,
    kind: Option<native_media::NativeMediaKind>,
    label: &str,
) -> Result<(), ApiError> {
    if caption.is_some() && kind.is_some_and(|kind| !kind.accepts_caption()) {
        return Err(ApiError::bad(format!(
            "caption is not accepted for {} media.",
            kind.expect("checked as present").as_str()
        )));
    }
    let caption = caption.and_then(nonempty_verbatim);
    if caption.is_some_and(|value| value.encode_utf16().count() > TELEGRAM_CAPTION_LIMIT) {
        return Err(ApiError::bad(format!(
            "The Telegram {label} caption exceeds 1024 UTF-16 code units."
        )));
    }
    Ok(())
}

fn group_reply_parameters(event: &Event) -> Option<ReplyParameters> {
    (event.session_kind == "group")
        .then(|| i32::try_from(event.message_id).ok())
        .flatten()
        .map(|message_id| {
            ReplyParameters::new(teloxide::types::MessageId(message_id))
                .allow_sending_without_reply()
        })
}

fn validate_file_name(value: &str) -> Result<(), ApiError> {
    if value.trim().is_empty()
        || value.chars().count() > MAX_FILE_NAME_CHARACTERS
        || value
            .chars()
            .any(|character| character.is_control() || matches!(character, '/' | '\\'))
    {
        return Err(ApiError::bad(
            "fileName must be a nonempty path-free name of at most 255 characters.",
        ));
    }
    Ok(())
}

fn validate_mime_type(value: &str) -> Result<(), ApiError> {
    if value.trim().is_empty()
        || value.chars().count() > MAX_MIME_TYPE_CHARACTERS
        || value.chars().any(char::is_control)
    {
        return Err(ApiError::bad(
            "The file content type must be a nonempty value of at most 255 characters.",
        ));
    }
    Ok(())
}