use serde::{Serialize, Deserialize};
use crate::{session::SessionStore, request::{RequestType}, WechatCommonResponse, LabradorResult, WechatCpClient, LabraError};
use crate::wechat::cp::constants::{ GROUP_ROBOT_MSG_IMAGE, GROUP_ROBOT_MSG_MARKDOWN, GROUP_ROBOT_MSG_NEWS, GROUP_ROBOT_MSG_TEXT};
use crate::wechat::cp::method::{WechatCpMethod};
#[derive(Debug, Clone)]
pub struct WechatCpGroupRobot<'a, T: SessionStore> {
client: &'a WechatCpClient<T>,
}
#[allow(unused)]
impl<'a, T: SessionStore> WechatCpGroupRobot<'a, T> {
#[inline]
pub fn new(client: &WechatCpClient<T>) -> WechatCpGroupRobot<T> {
WechatCpGroupRobot {
client,
}
}
fn get_webhook_url(&self) -> LabradorResult<String> {
if let Some(webhook_url) = &self.client.webhook_url {
Ok(webhook_url.to_string())
} else {
return Err(LabraError::ApiError("请先设置WebhookKey".to_string()))
}
}
pub async fn send_text(&self, content: &str, mentioneds: Vec<String>, mobiles: Vec<String>) -> LabradorResult<WechatCommonResponse> {
self.send_text_with_url(&self.get_webhook_url()?, content, mentioneds, mobiles).await
}
pub async fn send_text_with_url(&self, webhook_url: &str, content: &str, mentioneds: Vec<String>, mobiles: Vec<String>) -> LabradorResult<WechatCommonResponse> {
let req = WechatCpGroupRobotMessage {
msg_type: GROUP_ROBOT_MSG_TEXT.to_string(),
content: content.to_string().into(),
mentioned_list: mentioneds.into(),
mentioned_mobile_list: mobiles.into(),
base64: None,
md5: None,
articles: None,
media_id: None
};
self.client.post(WechatCpMethod::Custom {need_token: false, method_url: webhook_url.to_string()}, vec![], req, RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn send_markdown(&self, content: &str) -> LabradorResult<WechatCommonResponse> {
self.send_markdown_with_url(&self.get_webhook_url()?, content).await
}
pub async fn send_markdown_with_url(&self, webhook_url: &str, content: &str) -> LabradorResult<WechatCommonResponse> {
let req = WechatCpGroupRobotMessage {
msg_type: GROUP_ROBOT_MSG_MARKDOWN.to_string(),
content: content.to_string().into(),
mentioned_list: None,
mentioned_mobile_list: None,
base64: None,
md5: None,
articles: None,
media_id: None
};
self.client.post(WechatCpMethod::Custom {need_token: false, method_url: webhook_url.to_string()}, vec![], req, RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn send_image(&self, base64: &str, md5: &str) -> LabradorResult<WechatCommonResponse> {
self.send_image_with_url(&self.get_webhook_url()?, base64, md5).await
}
pub async fn send_image_with_url(&self, webhook_url: &str, base64: &str, md5: &str) -> LabradorResult<WechatCommonResponse> {
let req = WechatCpGroupRobotMessage {
msg_type: GROUP_ROBOT_MSG_IMAGE.to_string(),
content: None,
mentioned_list: None,
mentioned_mobile_list: None,
base64: base64.to_string().into(),
md5: md5.to_string().into(),
articles: None,
media_id: None
};
self.client.post(WechatCpMethod::Custom {need_token: false, method_url: webhook_url.to_string()}, vec![], req, RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn send_news(&self, articles: Vec<WechatCpNewArticle>) -> LabradorResult<WechatCommonResponse> {
self.send_news_with_url(&self.get_webhook_url()?, articles).await
}
pub async fn send_news_with_url(&self, webhook_url: &str, articles: Vec<WechatCpNewArticle>) -> LabradorResult<WechatCommonResponse> {
let req = WechatCpGroupRobotMessage {
msg_type: GROUP_ROBOT_MSG_NEWS.to_string(),
content: None,
mentioned_list: None,
mentioned_mobile_list: None,
base64: None,
md5: None,
articles: articles.into(),
media_id: None
};
self.client.post(WechatCpMethod::Custom {need_token: false, method_url: webhook_url.to_string()}, vec![], req, RequestType::Json).await?.json::<WechatCommonResponse>()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WechatCpMenuInfo {
pub buttons: Vec<WechatCpMenuButton>,
pub match_rule: Option<WechatCpMenuRule>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WechatCpMenuButton {
#[serde(rename="type")]
pub r#type: String,
pub name: String,
pub key: Option<String>,
pub url: Option<String>,
pub media_id: Option<String>,
pub article_id: Option<String>,
pub appid: Option<String>,
pub pagepath: Option<String>,
pub sub_button: Vec<WechatCpMenuButton>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WechatCpMenuRule {
pub tag_id: Option<String>,
pub sex: Option<String>,
pub province: Option<String>,
pub city: Option<String>,
pub client_platform_type: Option<String>,
pub language: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WechatCpGroupRobotMessage {
pub msg_type: String,
pub content: Option<String>,
pub mentioned_list: Option<Vec<String>>,
pub mentioned_mobile_list: Option<Vec<String>>,
pub base64: Option<String>,
pub md5: Option<String>,
pub articles: Option<Vec<WechatCpNewArticle>>,
pub media_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WechatCpNewArticle {
pub title: String,
pub description: String,
pub url: Option<String>,
pub pic_url: Option<String>,
pub btn_text: Option<String>,
pub appid: Option<String>,
pub pagepath: Option<String>,
}