use serde::{Serialize, Deserialize};
use serde_json::{ Value};
use crate::{session::SessionStore, request::{RequestType}, WechatCommonResponse, LabradorResult};
use crate::wechat::constants::{KEFU_MSGTYPE_IMAGE, KEFU_MSGTYPE_MA_PAGE, KEFU_MSGTYPE_TEXT};
use crate::wechat::miniapp::method::{MaMessageMethod, WechatMaMethod};
use crate::wechat::miniapp::WechatMaClient;
#[derive(Debug, Clone)]
pub struct WechatMaMessage<'a, T: SessionStore> {
client: &'a WechatMaClient<T>,
}
#[allow(unused)]
impl<'a, T: SessionStore> WechatMaMessage<'a, T> {
#[inline]
pub fn new(client: &WechatMaClient<T>) -> WechatMaMessage<T> {
WechatMaMessage {
client,
}
}
pub async fn send_kefu_msg(&self, message: WxMaKefuMsgRequest) -> LabradorResult<WechatCommonResponse> {
self.client.post(WechatMaMethod::Message(MaMessageMethod::SendCustomMsg), vec![], &message, RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn send_subscribe_msg(&self, data: WxMaSubscribeMsgRequest) -> LabradorResult<WechatCommonResponse> {
self.client.post(WechatMaMethod::Message(MaMessageMethod::SendSubscribeMsg), vec![], data, RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn send_uniform_msg(&self, data: WxMaUniformMsgRequest) -> LabradorResult<WechatCommonResponse> {
self.client.post(WechatMaMethod::Message(MaMessageMethod::SendUniformTemplate), vec![], &data, RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn create_updatable_message_activity_id<D: Serialize>(&self, data: D) -> LabradorResult<Value> {
self.client.get(WechatMaMethod::Message(MaMessageMethod::CreateActivityId), vec![], RequestType::Json).await?.json::<serde_json::Value>()
}
pub async fn create_updatable_message<D: Serialize>(&self, data: D) -> LabradorResult<()> {
self.client.post(WechatMaMethod::Message(MaMessageMethod::SendUpdatableMsg), vec![], data, RequestType::Json).await?.json::<serde_json::Value>()?;
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WxMaKefuMsgRequest {
pub touser: String,
pub msgtype: String,
pub text: Option<KfText>,
pub image: Option<KfImage>,
pub link: Option<KfLink>,
pub miniprogrampage: Option<KfMaPage>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KfText {
pub content: Option<String>,
}
#[allow(unused)]
impl KfText {
pub fn new() -> Self {
Self {
content: None,
}
}
fn content(mut self, content: &str) -> Self {
self.content = content.to_string().into();
self
}
pub fn build_msg(self) -> WxMaKefuMsgRequest {
WxMaKefuMsgRequest {
touser: "".to_string(),
msgtype: KEFU_MSGTYPE_TEXT.to_string(),
text: self.into(),
image: None,
link: None,
miniprogrampage: None
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KfImage {
pub media_id: Option<String>,
}
#[allow(unused)]
impl KfImage {
pub fn new() -> Self {
Self {
media_id: None,
}
}
fn media_id(mut self, media_id: &str) -> Self {
self.media_id = media_id.to_string().into();
self
}
pub fn build_msg(self) -> WxMaKefuMsgRequest {
WxMaKefuMsgRequest {
touser: "".to_string(),
msgtype: KEFU_MSGTYPE_IMAGE.to_string(),
text: None,
image: self.into(),
link: None,
miniprogrampage: None
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KfLink {
pub title: Option<String>,
pub description: Option<String>,
pub thumb_url: Option<String>,
pub url: Option<String>,
}
#[allow(unused)]
impl KfLink {
pub fn new() -> Self {
Self {
title: None,
description: None,
thumb_url: None,
url: None
}
}
fn url(mut self, url: &str) -> Self {
self.url = url.to_string().into();
self
}
fn title(mut self, title: &str) -> Self {
self.title = title.to_string().into();
self
}
fn thumb_url(mut self, thumb_url: &str) -> Self {
self.thumb_url = thumb_url.to_string().into();
self
}
fn description(mut self, description: &str) -> Self {
self.description = description.to_string().into();
self
}
pub fn build_msg(self) -> WxMaKefuMsgRequest {
WxMaKefuMsgRequest {
touser: "".to_string(),
msgtype: KEFU_MSGTYPE_IMAGE.to_string(),
text: None,
image: None,
link: self.into(),
miniprogrampage: None
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KfMaPage {
pub title: Option<String>,
pub pagepath: Option<String>,
pub thumb_media_id: Option<String>,
}
#[allow(unused)]
impl KfMaPage {
pub fn new() -> Self {
Self {
title: None,
pagepath: None,
thumb_media_id: None
}
}
fn thumb_media_id(mut self, thumb_media_id: &str) -> Self {
self.thumb_media_id = thumb_media_id.to_string().into();
self
}
fn pagepath(mut self, pagepath: &str) -> Self {
self.pagepath = pagepath.to_string().into();
self
}
fn title(mut self, title: &str) -> Self {
self.title = title.to_string().into();
self
}
pub fn build_msg(self) -> WxMaKefuMsgRequest {
WxMaKefuMsgRequest {
touser: "".to_string(),
msgtype: KEFU_MSGTYPE_MA_PAGE.to_string(),
text: None,
image: None,
link: None,
miniprogrampage: self.into()
}
}
}
#[allow(unused)]
impl WxMaKefuMsgRequest {
fn touser(mut self, touser: &str) -> Self {
self.touser = touser.to_string().into();
self
}
fn text() -> KfText {
KfText::new()
}
fn image() -> KfImage {
KfImage::new()
}
fn link() -> KfLink {
KfLink::new()
}
fn miniprogram() -> KfMaPage {
KfMaPage::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WxMaSubscribeMsgRequest {
pub touser: String,
pub template_id: String,
pub page: Option<String>,
pub data: Option<Value>,
pub miniprogram_state: Option<String>,
pub lang: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WxMaUniformMsgRequest {
touser: String,
weapp_template_msg: Option<WeappTemplateMsg>,
mp_template_msg: MpTemplateMsg,
}
#[derive(Debug, Clone,Deserialize, Serialize)]
pub struct MsgData {
name: String,
value: String,
}
#[derive(Debug, Clone,Serialize, Deserialize)]
pub struct WeappTemplateMsg {
template_id: String,
page: String,
form_id: String,
data: Value,
emphasis_keyword: String,
}
#[allow(unused)]
impl WxMaUniformMsgRequest {
pub fn new<S: Into<String>>(touser: S, weapp_template_msg: Option<WeappTemplateMsg>, mp_template_msg:MpTemplateMsg) -> WxMaUniformMsgRequest {
WxMaUniformMsgRequest {
touser: touser.into(),
weapp_template_msg,
mp_template_msg,
}
}
}
#[derive(Debug, Clone,Serialize, Deserialize)]
pub struct MpTemplateMsg {
appid: String,
template_id: String,
url: String,
miniprogram: Value,
data: Value,
}