use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::EndpointBuilder,
http::Transport,
req_option::RequestOption,
SDKResult,
},
service::im::v2::models::{ButtonInfo, UserIdType},
};
pub struct GroupsBotsService {
pub config: Config,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BotTimeSentiveRequest {
pub receive_id: String,
pub content: Value,
pub msg_type: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BotTimeSentiveResponse {
pub message_id: String,
pub send_time: String,
}
impl ApiResponseTrait for BotTimeSentiveResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateFeedCardButtonRequest {
pub buttons: Vec<ButtonInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateFeedCardButtonResponse {
pub message_id: String,
pub update_time: String,
pub updated_button_count: i32,
}
impl ApiResponseTrait for UpdateFeedCardButtonResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TimelyReminderRequest {
pub content: Value,
pub target_users: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reminder_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimelyReminderResponse {
pub reminder_id: String,
pub send_time: String,
pub success_count: i32,
pub failed_users: Vec<String>,
}
impl ApiResponseTrait for TimelyReminderResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl GroupsBotsService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn bot_time_sentive(
&self,
receive_id_type: UserIdType,
request: BotTimeSentiveRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<BotTimeSentiveResponse>> {
let api_req = ApiRequest {
http_method: Method::POST,
api_path: crate::core::endpoints::im::IM_V2_GROUPS_BOTS_TIME_SENSITIVE.to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
query_params: HashMap::from([(
"receive_id_type",
receive_id_type.as_str().to_string(),
)]),
body: serde_json::to_vec(&request)?,
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
pub async fn update(
&self,
message_id: &str,
request: UpdateFeedCardButtonRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<UpdateFeedCardButtonResponse>> {
let api_req = ApiRequest {
http_method: Method::PUT,
api_path: EndpointBuilder::replace_param(
crate::core::endpoints::im::IM_V2_GROUPS_BOTS_UPDATE,
"message_id",
message_id,
),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: serde_json::to_vec(&request)?,
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
pub async fn patch(
&self,
receive_id_type: UserIdType,
request: TimelyReminderRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<TimelyReminderResponse>> {
let api_req = ApiRequest {
http_method: Method::PATCH,
api_path: crate::core::endpoints::im::IM_V2_GROUPS_BOTS_PATCH.to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
query_params: HashMap::from([(
"receive_id_type",
receive_id_type.as_str().to_string(),
)]),
body: serde_json::to_vec(&request)?,
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
}