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

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

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


核心基本类:

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

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

文字类型的消息

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

发送消息

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

测试代码:

 private void button9_Click(object sender, EventArgs e)
        {
            TextMsg data = new TextMsg("测试发送文字消息给整个企业" + 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("测试发送文字消息给医疗部" + 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("测试发送文字消息给所有用户" + 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("单用户文字消息" + DateTime.Now);
            data.agentid = 7;
            data.safe = "0";          
            data.touser = "iaodong";
            BLLMsg.SendMessage(data);
        }

主动发送消息官方文档

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

上一篇:微信红包架构分析


下一篇:微信二维码网页登录、支付宝手机二维码支付模式实现