open_lark/service/im/v2/groups_bots/
mod.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use std::collections::HashMap;
5
6use crate::{
7    core::{
8        api_req::ApiRequest,
9        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
10        config::Config,
11        constants::AccessTokenType,
12        http::Transport,
13        req_option::RequestOption,
14        SDKResult,
15    },
16    service::im::v2::models::{ButtonInfo, UserIdType},
17};
18
19/// 群聊或机器人消息服务
20pub struct GroupsBotsService {
21    pub config: Config,
22}
23
24/// 机器人单聊即时提醒请求
25#[derive(Debug, Serialize, Deserialize)]
26pub struct BotTimeSentiveRequest {
27    /// 接收用户ID
28    pub receive_id: String,
29    /// 提醒消息内容
30    pub content: Value,
31    /// 消息类型
32    pub msg_type: String,
33}
34
35/// 机器人单聊即时提醒响应
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct BotTimeSentiveResponse {
38    /// 消息ID
39    pub message_id: String,
40    /// 发送时间
41    pub send_time: String,
42}
43
44impl ApiResponseTrait for BotTimeSentiveResponse {
45    fn data_format() -> ResponseFormat {
46        ResponseFormat::Data
47    }
48}
49
50/// 更新消息流卡片按钮请求
51#[derive(Debug, Serialize, Deserialize)]
52pub struct UpdateFeedCardButtonRequest {
53    /// 按钮信息列表
54    pub buttons: Vec<ButtonInfo>,
55    /// 更新原因
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub reason: Option<String>,
58}
59
60/// 更新消息流卡片按钮响应
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct UpdateFeedCardButtonResponse {
63    /// 消息ID
64    pub message_id: String,
65    /// 更新时间
66    pub update_time: String,
67    /// 更新的按钮数量
68    pub updated_button_count: i32,
69}
70
71impl ApiResponseTrait for UpdateFeedCardButtonResponse {
72    fn data_format() -> ResponseFormat {
73        ResponseFormat::Data
74    }
75}
76
77/// 即时提醒请求
78#[derive(Debug, Serialize, Deserialize)]
79pub struct TimelyReminderRequest {
80    /// 提醒消息内容
81    pub content: Value,
82    /// 目标用户ID列表
83    pub target_users: Vec<String>,
84    /// 提醒类型
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub reminder_type: Option<String>,
87}
88
89/// 即时提醒响应
90#[derive(Debug, Clone, Serialize, Deserialize)]
91pub struct TimelyReminderResponse {
92    /// 提醒ID
93    pub reminder_id: String,
94    /// 发送时间
95    pub send_time: String,
96    /// 成功发送的用户数量
97    pub success_count: i32,
98    /// 失败的用户ID列表
99    pub failed_users: Vec<String>,
100}
101
102impl ApiResponseTrait for TimelyReminderResponse {
103    fn data_format() -> ResponseFormat {
104        ResponseFormat::Data
105    }
106}
107
108impl GroupsBotsService {
109    pub fn new(config: Config) -> Self {
110        Self { config }
111    }
112
113    /// 机器人单聊即时提醒
114    pub async fn bot_time_sentive(
115        &self,
116        receive_id_type: UserIdType,
117        request: BotTimeSentiveRequest,
118        option: Option<RequestOption>,
119    ) -> SDKResult<BaseResponse<BotTimeSentiveResponse>> {
120        let api_req = ApiRequest {
121            http_method: Method::POST,
122            api_path: "/open-apis/im/v2/groups-bots/bot_time_sentive".to_string(),
123            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
124            query_params: HashMap::from([(
125                "receive_id_type".to_string(),
126                receive_id_type.as_str().to_string(),
127            )]),
128            body: serde_json::to_vec(&request)?,
129            ..Default::default()
130        };
131
132        Transport::request(api_req, &self.config, option).await
133    }
134
135    /// 更新消息流卡片按钮
136    pub async fn update(
137        &self,
138        message_id: &str,
139        request: UpdateFeedCardButtonRequest,
140        option: Option<RequestOption>,
141    ) -> SDKResult<BaseResponse<UpdateFeedCardButtonResponse>> {
142        let api_req = ApiRequest {
143            http_method: Method::PUT,
144            api_path: format!("/open-apis/im/v2/groups-bots/{message_id}/update"),
145            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
146            body: serde_json::to_vec(&request)?,
147            ..Default::default()
148        };
149
150        Transport::request(api_req, &self.config, option).await
151    }
152
153    /// 即时提醒
154    pub async fn patch(
155        &self,
156        receive_id_type: UserIdType,
157        request: TimelyReminderRequest,
158        option: Option<RequestOption>,
159    ) -> SDKResult<BaseResponse<TimelyReminderResponse>> {
160        let api_req = ApiRequest {
161            http_method: Method::PATCH,
162            api_path: "/open-apis/im/v2/groups-bots/patch".to_string(),
163            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
164            query_params: HashMap::from([(
165                "receive_id_type".to_string(),
166                receive_id_type.as_str().to_string(),
167            )]),
168            body: serde_json::to_vec(&request)?,
169            ..Default::default()
170        };
171
172        Transport::request(api_req, &self.config, option).await
173    }
174}