use std::fmt::{Display, Formatter};
use derive_builder::Builder;
use reqwest::Client;
use serde::{ Deserialize, Serialize};
use crate::RPayResult;
#[derive(Debug, Clone, Serialize, Deserialize, Builder)]
#[builder(pattern = "mutable")]
pub struct CustomerService {
#[builder(setter(into))]
pub access_token: String,
#[builder(setter(into))]
pub touser: String,
#[builder(setter(into))]
#[serde(rename = "msgtype")]
pub msg_type: MsgType,
#[builder(default, setter(into))]
pub text: Option<Text>,
#[builder(default, setter(into))]
pub image : Option<Image>,
#[builder(default, setter(into))]
pub link: Option<Link>,
#[builder(default, setter(into))]
pub miniprogrampage: Option<Miniprogrampage>,
}
impl CustomerService {
pub async fn send(&mut self) -> RPayResult<Response> {
let url = format!("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={}", self.access_token);
let json_body = serde_json::to_string(self).unwrap();
println!("{}", json_body);
let resp = Client::new()
.post(url)
.body(json_body)
.send()
.await?
.json::<Response>()
.await?;
Ok(resp)
}
pub async fn get_thumb_media_id(&mut self, _text: String) -> RPayResult<Response> {
let url = format!("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={}&type=thumb", self.access_token);
let resp = Client::new()
.post(url)
.send()
.await?
.json::<Response>()
.await?;
println!("{:?}", resp);
Ok(resp)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response {
pub errcode: i64,
pub errmsg: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Miniprogrampage {
pub title: Option<String>,
pub pagepath: Option<String>,
pub thumb_media_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Link {
pub title: Option<String>,
pub description: Option<String>,
pub url: Option<String>,
pub thumb_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Image {
pub media_id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Text {
pub content: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MsgType {
#[serde(rename = "text")]
Text,
#[serde(rename = "link")]
Link,
#[serde(rename = "miniprogrampage")]
Miniprogrampage
}
impl Display for MsgType {
fn fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result {
match self {
MsgType::Text => write!(fmt, "text"),
MsgType::Link => write!(fmt, "link"),
MsgType::Miniprogrampage => write!(fmt, "miniprogrampage"),
}
}
}