Enterprise ID development: actively send messages, and enterprises send messages
The primary enterprise ID actively sends messages, that is, the messages actively pushed by the enterprise ID, which is suitable for enterprise notifications and announcements. Therefore, if the company requires notification to all employees, it should use the initiative to send messages.
The format is json and flexible. When the json value of touser, toparty, and totag is null, the server ignores it. I was still worried about how to ignore the null field when generating the json format if it is null.
Core basics:
Public class MsgBase {public MsgBase () {this. safe = "0"; // indicates whether the message is confidential, 0 indicates no, 1 indicates yes, default 0} // <summary> // UserID list (Message Receiver, multiple receivers are separated by '| ). Special case: If it is specified as @ all, it will send // </summary> public string touser {get; set ;} /// <summary> /// PartyID list. Multiple recipients are separated by '|. When touser is @ all, ignore this parameter /// </summary> public string toparty {get; set ;}/// <summary> /// TagID list, multiple recipients are separated by '|. When touser is @ all, ignore this parameter // </summary> public string totag {get; set ;} /// <summary> /// Message Type // </summary> public string msgtype {get; set ;} /// <summary> /// Enterprise Application id, which is an integer. You can check /// </summary> public int agentid {get; set ;}/// <summary> /// to check whether the message is a confidential message, 0 indicates no, 1 indicates yes, default 0 // </summary> public string safe {get; set ;}}
Public static class MsgType {public enum MsgBaseEnum {Text = 1, image = 2, voice = 3, video = 4, file = 5, news = 6, mpnews = 7 }; public static string GetMsgTypeText (MsgBaseEnum type) {string text = ""; switch (type) {case MsgBaseEnum. text: text = "text"; break; case MsgBaseEnum. image: text = "image"; break; case MsgBaseEnum. voice: text = "voice"; break; case MsgBaseEnum. video: text = "video"; break; case MsgBaseEnum. file: text = "file"; break; case MsgBaseEnum. news: text = "news"; break; case MsgBaseEnum. mpnews: text = "mpnews"; break; default: throw new Exception ("type =" + type + ", messages of this type are not implemented");} return text ;}}
Text message
public class TextMsg : MsgBase { public TextMsg(string content) { this.text = new TextMsgContent(content); this.msgtype = MsgType.GetMsgTypeText(MsgType.MsgBaseEnum.Text); } public TextMsgContent text { get; set; } } public class TextMsgContent { public TextMsgContent(string content) { this.content = content; } public string content { get; set; } }
Send message
Public static class BLLMsg {public static bool SendMessage (MsgBase data) {string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/message/send? Access_token = {0} "; string accessToken = BLLAccessToken. getAccessToken (); var url = string. format (urlFormat, accessToken); WebUtils ut = new WebUtils (); var postData = Tools. toJsonString <MsgBase> (data); // the data does not need to be encrypted to send LogInfo. info ("Send message:" + postData); string sendResult = ut. doPost (url, postData); SendMsgResult tempAccessTokenjson = Tools. jsonStringToObj <SendMsgResult> (sendResult); if (tempAccessTokenjson. hasError () {LogInfo. error ("message sending Error:" + Tools. toJsonString <SendMsgResult> (tempAccessTokenjson); return false;} return true ;}}
Test code:
Private void button9_Click (object sender, EventArgs e) {TextMsg data = new TextMsg ("test sending a text message to the entire enterprise" + DateTime. now); data. agentid = 7; data. safe = "0"; data. toparty = "1"; BLLMsg. sendMessage (data);} private void button11_Click (object sender, EventArgs e) {TextMsg data = new TextMsg ("test sending text messages to the Medical Department" + DateTime. now); data. agentid = 7; data. safe = "0"; data. toparty = "2"; BLLMsg. sendMessage (data);} private void button10_Click (object sender, EventArgs e) {TextMsg data = new TextMsg ("test sending a text message to all users" + DateTime. now); data. agentid = 7; data. safe = "0"; data. touser = "@ all"; BLLMsg. sendMessage (data);} private void button1_Click (object sender, EventArgs e) {TextMsg data = new TextMsg ("single-user text message" + DateTime. now); data. agentid = 7; data. safe = "0"; data. touser = "iaodong"; BLLMsg. sendMessage (data );}
Actively send official message documentation
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.