您的位置:首页 > 移动开发 > 微信开发

微信企业号开发:主动发送消息

2017-04-17 14:23 204 查看
主企业号主动发送消息,也就是企业号主动推送的消息,适合于企业的通知,通告等。因此如果公司有通知,要求通知到所有员工,就应该使用主动发送消息。

格式是json格式,而且微信很灵活,当touser,toparty,totag的json值是null时,微信服务器主动忽略了。原来还担心,如果是null,在生成json格式时如何忽略掉是null的字段。

核心基本类:

[csharp] view
plain copy

public class MsgBase  

    {  

       public MsgBase()  

       {  

           this.safe = "0"; //表示是否是保密消息,0表示否,1表示是,默认0  

       }  

        /// <summary>  

        /// UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送  

        /// </summary>  

        public string touser { get; set; }  

  

        /// <summary>  

        /// PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数  

        /// </summary>  

        public string toparty { get; set; }  

  

        /// <summary>  

        /// TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数  

        /// </summary>  

        public string totag { get; set; }  

        /// <summary>  

        /// 消息类型  

        /// </summary>  

        public string msgtype { get; set; }  

        /// <summary>  

        /// 企业应用的id,整型。可在应用的设置页面查看  

        /// </summary>  

        public int  agentid { get; set; }  

  

        /// <summary>  

        /// 表示是否是保密消息,0表示否,1表示是,默认0  

        /// </summary>       

        public string safe { get; set; }  

    }  

[csharp] view
plain copy

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 + ",此类型的消息没有实现");  

            

          }  

          return text;        

      }  

    }  

文字类型的消息

[csharp] view
plain copy

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; }  

   }  

发送消息

[csharp] view
plain copy

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);  

          //数据不用加密发送    

          LogInfo.Info("发送消息: " + postData);  

          string sendResult = ut.DoPost(url, postData);  

          SendMsgResult tempAccessTokenjson = Tools.JsonStringToObj<SendMsgResult>(sendResult);  

          if (tempAccessTokenjson.HasError())  

          {  

              LogInfo.Error("发送消息错误: " + Tools.ToJsonString<SendMsgResult>(tempAccessTokenjson));  

              return false;  

          }  

          

  

          return true;  

      }  

   }  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: