use serde::{Deserialize, Serialize};
use crate::{
error::{CommonError, CommonResponse},
wechat::WxApiRequestBuilder,
SdkResult,
};
use super::material::FileStruct;
#[derive(Debug, Serialize, Deserialize)]
pub struct KFList {
kf_list: Vec<KFAccount>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct KFAccount {
pub kf_account: String,
pub kf_headimgurl: String,
pub kf_id: String,
pub kf_nick: String,
#[serde(alias = "invite_wx")]
pub kf_wx: String,
pub invite_expire_time: Option<i64>,
pub invite_status: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct KFOnlineList {
kf_online_list: Vec<KFOnlineAccount>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct KFOnlineAccount {
pub kf_account: String,
pub status: i8,
pub kf_id: String,
pub accepted_case: i8,
}
pub struct CustomServiceModule<'a, T: WxApiRequestBuilder>(pub(crate) &'a T);
impl<'a, T: WxApiRequestBuilder> CustomServiceModule<'a, T> {
pub async fn getkflist(&self) -> SdkResult<KFList> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist";
let sdk = self.0;
let res: CommonResponse<KFList> = sdk.wx_get(base_url).await?.send().await?.json().await?;
res.into()
}
pub async fn getonlinekflist(&self) -> SdkResult<KFOnlineList> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist";
let sdk = self.0;
let res: CommonResponse<KFOnlineList> =
sdk.wx_get(base_url).await?.send().await?.json().await?;
res.into()
}
pub async fn kfaccount_add(&self, kf_account: String, nickname: String) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/kfaccount/add";
let sdk = self.0;
let msg: CommonError = sdk
.wx_post(base_url)
.await?
.json(&serde_json::json!({
"kf_account": kf_account,
"nickname": nickname
}))
.send()
.await?
.json()
.await?;
msg.into()
}
pub async fn kfaccount_inviteworker(
&self,
kf_account: String,
invite_wx: String,
) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/kfaccount/inviteworker";
let sdk = self.0;
let msg: CommonError = sdk
.wx_post(base_url)
.await?
.json(&serde_json::json!({
"kf_account": kf_account,
"invite_wx": invite_wx
}))
.send()
.await?
.json()
.await?;
msg.into()
}
pub async fn kfaccount_update(&self, kf_account: String, nickname: String) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/kfaccount/update";
let sdk = self.0;
let msg: CommonError = sdk
.wx_post(base_url)
.await?
.json(&serde_json::json!({
"kf_account": kf_account,
"nickname": nickname
}))
.send()
.await?
.json()
.await?;
msg.into()
}
pub async fn kfaccount_uploadheadimg(
&self,
kf_account: String,
file: FileStruct,
) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/kfaccount/uploadheadimg";
let sdk = self.0;
let builder = sdk.wx_post(base_url).await?;
let builder = builder.query(&[("kf_account", kf_account)]);
let part = reqwest::multipart::Part::bytes(file.file)
.file_name(file.filename)
.mime_str(file.conten_type.as_ref());
let form = reqwest::multipart::Form::new().part("media", part.unwrap());
let res: CommonError = builder.multipart(form).send().await?.json().await?;
res.into()
}
pub async fn kfaccount_del(&self, kf_account: String) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/kfaccount/del";
let sdk = self.0;
let builder = sdk.wx_get(base_url).await?;
let builder = builder.query(&[("kf_account", kf_account)]);
let res: CommonError = builder.send().await?.json().await?;
res.into()
}
pub async fn kfsession_create(&self, kf_account: String, openid: String) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/kfsession/create";
let sdk = self.0;
let msg: CommonError = sdk
.wx_post(base_url)
.await?
.json(&serde_json::json!({
"kf_account": kf_account,
"openid": openid
}))
.send()
.await?
.json()
.await?;
msg.into()
}
pub async fn kfsession_close(&self, kf_account: String, openid: String) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/customservice/kfsession/close";
let sdk = self.0;
let msg: CommonError = sdk
.wx_post(base_url)
.await?
.json(&serde_json::json!({
"kf_account": kf_account,
"openid": openid
}))
.send()
.await?
.json()
.await?;
msg.into()
}
}