use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct ChatGroupRequestBuilder {
config: Config,
task_id: String,
name: Option<String>,
owner_ids: Vec<String>,
member_ids: Vec<String>,
}
impl ChatGroupRequestBuilder {
pub fn new(config: Config, task_id: impl Into<String>) -> Self {
Self {
config,
task_id: task_id.into(),
name: None,
owner_ids: Vec::new(),
member_ids: Vec::new(),
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn owner_id(mut self, owner_id: impl Into<String>) -> Self {
self.owner_ids.push(owner_id.into());
self
}
pub fn owner_ids(mut self, owner_ids: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.owner_ids.extend(owner_ids.into_iter().map(Into::into));
self
}
pub fn member_id(mut self, member_id: impl Into<String>) -> Self {
self.member_ids.push(member_id.into());
self
}
pub fn member_ids(mut self, member_ids: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.member_ids
.extend(member_ids.into_iter().map(Into::into));
self
}
pub async fn execute(self) -> SDKResult<ChatGroupResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(self, option: RequestOption) -> SDKResult<ChatGroupResponse> {
let url = format!("/open-apis/apaas/v1/user_tasks/{}/chat_group", self.task_id);
let request = ChatGroupRequest {
name: self.name,
owner_ids: self.owner_ids,
member_ids: self.member_ids,
};
let req: ApiRequest<ChatGroupResponse> =
ApiRequest::post(&url).body(serde_json::to_value(&request)?);
let resp = Transport::request(req, &self.config, Some(option)).await?;
resp.data
.ok_or_else(|| openlark_core::error::validation_error("Operation", "响应数据为空"))
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
struct ChatGroupRequest {
#[serde(rename = "name", skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(rename = "owner_ids", skip_serializing_if = "Vec::is_empty")]
owner_ids: Vec<String>,
#[serde(rename = "member_ids", skip_serializing_if = "Vec::is_empty")]
member_ids: Vec<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ChatGroupResponse {
#[serde(rename = "chat_id")]
pub chat_id: String,
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "message")]
pub message: String,
}
impl ApiResponseTrait for ChatGroupResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[deprecated(note = "renamed to ChatGroupRequestBuilder, will be removed in v1.0 (#271)")]
pub type ChatGroupBuilder = ChatGroupRequestBuilder;
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_chat_group_user_task_returns_data_on_success() {
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/open-apis/apaas/v1/user_tasks/task_001/chat_group"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0,
"msg": "success",
"data": {
"chat_id": "oc_001",
"name": "任务协作群",
"message": "建群成功"
}
})))
.mount(&server)
.await;
let config = Config::builder()
.app_id("ci_app_id")
.app_secret("ci_app_secret")
.base_url(server.uri())
.enable_token_cache(false)
.build();
let resp = ChatGroupRequestBuilder::new(config, "task_001")
.name("任务协作群")
.owner_id("u_001")
.member_ids(vec!["u_002".to_string(), "u_003".to_string()])
.execute()
.await
.expect("发起群聊应成功");
assert_eq!(resp.chat_id, "oc_001");
assert_eq!(resp.name, "任务协作群");
assert_eq!(resp.message, "建群成功");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/apaas/v1/user_tasks/task_001/chat_group"
);
}
}