use std::collections::HashMap;
use derive_builder::Builder;
use reqwest::Client;
use serde::{ Deserialize, Serialize};
use serde_json::Value;
use crate::{core::common::Lang, RPayResult};
#[derive(Debug, Clone, Serialize, Deserialize, Builder)]
#[builder(pattern = "mutable")]
pub struct Message {
#[builder(setter(into))]
pub access_token: String,
#[builder(setter(into))]
pub touser: String,
#[builder(setter(into))]
pub template_id: String,
#[builder(default,setter(into))]
pub page: Option<String>,
#[builder(default,setter(into))]
pub mini_program: Option<MiniProgram>,
#[builder(setter(into))]
pub data: HashMap<String, DataItem>,
#[builder(default,setter(into))]
pub miniprogram_state: String,
#[builder(default,setter(into))]
pub lang: Lang,
}
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub enum MiniProgramState {
#[serde(rename = "developer")]
#[default]
Developer,
#[serde(rename = "trial")]
Trial,
#[serde(rename = "formal")]
Formal
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MiniProgram {
#[serde(rename = "appid")]
pub app_id: String,
#[serde(rename = "pagepath")]
pub page_path: Value
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataItem {
pub value: String,
}
impl Message {
pub async fn send(&mut self) -> RPayResult<Response> {
let url = format!("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={}", self.access_token);
let json_body = serde_json::to_string(self).unwrap();
let resp = Client::new()
.post(url)
.body(json_body)
.send()
.await?
.json::<Response>()
.await?;
Ok(resp)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Builder)]
pub struct Response {
pub errmsg: String,
pub errcode: i64
}