use super::{get_send, post_send};
use crate::{wechat::WxApiRequestBuilder, SdkResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddTemplate {
pub tid: String,
pub kid_list: Vec<i64>,
#[serde(default)]
pub scene_desc: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PriTmplId {
pub pri_tmpl_id: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CategoryData {
pub data: Vec<Category>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Category {
pub id: i32,
pub name: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TemplateKeywords {
pub count: i64,
pub data: Vec<TemplateKeyword>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TemplateKeyword {
pub kid: i32,
pub name: String,
pub example: String,
pub rule: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct QueryTemplateTitleList {
pub ids: String,
pub start: i32,
pub limit: i32,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TemplateTitleList {
pub count: i64,
pub data: Vec<TemplateTitle>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TemplateTitle {
pub tid: i64,
pub title: String,
#[serde(rename = "type")]
pub type_: i32,
pub category_id: i64,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TemplateList {
pub data: Vec<Template>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Template {
pub pri_tmpl_id: String,
pub title: String,
pub content: String,
pub example: String,
#[serde(rename = "type")]
pub type_: i32,
pub keyword_enum_value_list: Vec<KeywordEnum>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct KeywordEnum {
pub keyword_code: String,
pub enum_value_list: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SendData {
pub touser: String,
pub template_id: String,
#[serde(default)]
pub page: Option<String>,
pub data: HashMap<String, StrValue>,
#[serde(default)]
pub miniprogram_state: Option<String>,
#[serde(default)]
pub lang: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct StrValue {
pub value: String,
}
pub struct SubscribeMessageModule<'a, T: WxApiRequestBuilder>(pub(crate) &'a T);
impl<'a, T: WxApiRequestBuilder> SubscribeMessageModule<'a, T> {
pub async fn add_template(&self, data: &AddTemplate) -> SdkResult<PriTmplId> {
let url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate";
post_send(self.0, url, data).await
}
pub async fn delete_template(&self, data: &PriTmplId) -> SdkResult<()> {
let url = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate";
post_send(self.0, url, data).await
}
pub async fn get_category(&self) -> SdkResult<CategoryData> {
let url = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory";
get_send(self.0, url, &()).await
}
pub async fn get_pub_template_keywords_by_id(&self, tid: &str) -> SdkResult<TemplateKeywords> {
let query = &serde_json::json!({ "tid": tid });
let url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords";
get_send(self.0, url, query).await
}
pub async fn get_pub_template_title_list(
&self,
query: &QueryTemplateTitleList,
) -> SdkResult<TemplateTitleList> {
let url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles";
get_send(self.0, url, query).await
}
pub async fn get_template_list(&self) -> SdkResult<TemplateList> {
let url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate";
get_send(self.0, url, &()).await
}
pub async fn send(&self, data: &SendData) -> SdkResult<()> {
let url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
post_send(self.0, url, data).await
}
}