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