Skip to main content

botkit_telegram/
action.rs

1use std::time::Duration;
2
3use botkit_core::action::{ChatAction, ChatActionFuture, ChatActionSender};
4
5use crate::client::TelegramClient;
6
7/// Telegram chat action sender
8///
9/// Sends chat actions to Telegram with appropriate action strings.
10#[derive(Clone)]
11pub struct TelegramActionSender {
12    client: TelegramClient,
13    chat_id: i64,
14}
15
16impl TelegramActionSender {
17    /// Create a new Telegram action sender
18    pub fn new(client: TelegramClient, chat_id: i64) -> Self {
19        Self { client, chat_id }
20    }
21
22    /// Map unified ChatAction to Telegram API action string
23    fn action_string(action: ChatAction) -> &'static str {
24        match action {
25            ChatAction::Typing => "typing",
26            ChatAction::UploadPhoto => "upload_photo",
27            ChatAction::RecordVideo => "record_video",
28            ChatAction::UploadVideo => "upload_video",
29            ChatAction::RecordVoice => "record_voice",
30            ChatAction::UploadVoice => "upload_voice",
31            ChatAction::UploadDocument => "upload_document",
32            ChatAction::ChooseSticker => "choose_sticker",
33            ChatAction::FindLocation => "find_location",
34            ChatAction::RecordVideoNote => "record_video_note",
35            ChatAction::UploadVideoNote => "upload_video_note",
36        }
37    }
38}
39
40impl ChatActionSender for TelegramActionSender {
41    fn send_action(&self, action: ChatAction) -> ChatActionFuture<'_> {
42        Box::pin(async move {
43            self.client
44                .send_chat_action(self.chat_id, Self::action_string(action))
45                .await
46        })
47    }
48
49    fn action_expiry(&self) -> Duration {
50        // Telegram typing expires after 5 seconds
51        Duration::from_secs(5)
52    }
53
54    fn clone_boxed(&self) -> Box<dyn ChatActionSender> {
55        Box::new(self.clone())
56    }
57}