use serde::{Serialize, Deserialize};
use serde_json::{json, Value};
use crate::{session::SessionStore, request::{RequestType}, WechatCommonResponse, WechatMpClient, LabradorResult};
use crate::wechat::mp::method::{MpTemplateMessageMethod, WechatMpMethod};
#[derive(Debug, Clone)]
pub struct WechatMpTemplateMessage<'a, T: SessionStore> {
client: &'a WechatMpClient<T>,
}
#[allow(unused)]
impl<'a, T: SessionStore> WechatMpTemplateMessage<'a, T> {
#[inline]
pub fn new(client: &WechatMpClient<T>) -> WechatMpTemplateMessage<T> {
WechatMpTemplateMessage {
client,
}
}
pub async fn set_industry(&self, industry_id1: &str, industry_id2: &str) -> LabradorResult<WechatCommonResponse> {
self.client.post(WechatMpMethod::TemplateMessage(MpTemplateMessageMethod::SetIndustry), vec![], json!({
"industry_id1": industry_id1,
"industry_id2": industry_id2,
}), RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn get_industry(&self) -> LabradorResult<IndustryResponse> {
let response = self.client.post(WechatMpMethod::TemplateMessage(MpTemplateMessageMethod::GetIndustry), vec![], Value::Null, RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse::<IndustryResponse>(response)
}
pub async fn send_mp_message(&self, data: TemplateMessage) -> LabradorResult<WechatCommonResponse> {
self.client.post(WechatMpMethod::TemplateMessage(MpTemplateMessageMethod::SendTemplate), vec![], data, RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn get_template_id(&self, template_id_short: &str) -> LabradorResult<String> {
let response = self.client.post(WechatMpMethod::TemplateMessage(MpTemplateMessageMethod::GetTemplateId), vec![], json!({ "template_id_short": template_id_short }), RequestType::Json).await?.json::<Value>()?;
let v = WechatCommonResponse::parse::<Value>(response)?;
let template_id = v["template_id"].as_str().unwrap_or_default();
Ok(template_id.to_string())
}
pub async fn get_template_list(&self) -> LabradorResult<Vec<TemplateMessageInfo>> {
let response = self.client.post(WechatMpMethod::TemplateMessage(MpTemplateMessageMethod::GetTemplateList), vec![], Value::Null, RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse_with_key::<Vec<TemplateMessageInfo>>(response, "template_list")
}
pub async fn delete_template(&self, template_id: &str) -> LabradorResult<WechatCommonResponse> {
self.client.post(WechatMpMethod::TemplateMessage(MpTemplateMessageMethod::DeleteTemplate), vec![], json!({ "template_id": template_id }), RequestType::Json).await?.json::<WechatCommonResponse>()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateMessage {
pub touser: Option<String>,
pub template_id: String,
pub url: Option<String>,
pub miniprogram: Option<Value>,
pub data: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndustryResponse {
primary_industry: Option<IndustryClass>,
secondary_industry: Option<IndustryClass>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndustryClass {
first_class: Option<String>,
second_class: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateMessageInfo {
template_id: Option<String>,
title: Option<String>,
primary_industry: Option<String>,
deputy_industry: Option<String>,
content: Option<String>,
example: Option<String>,
}