企业微信发送应用消息的实现

企业号升级到企业微信后,发送应用消息的接口也变化了不少,除了原来的文本、图片、文件、语音、视频、图文消息等消息外,增加了文本卡片、markdown消息、小程序通知消息等内容,不过它们都可以共用一个接口进行发送,只是它们的对象不太一样,本篇随笔主要介绍整个企业微信应用消息处理这部分内容,包括不同消息的实体关系和接口发送的实现等内容。

1、企业微信消息对象的定义

在早期还是微信企业号的时候,我对发送企业号消息的接口也已经实现,参考《C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)》,这次对企业号升级到企业微信接口,对所有接口进行了梳理和测试。

我们先看看企业微信对应用消息接口的介绍(https://work.weixin.qq.com/api/doc#90000/90135/90236

根据消息的类型,我们增加了一些额外的对象实体类,修改后的关系图如下所示。

我们来看看新增的文本卡片、markdown消息、小程序通知消息等内容的对象定义代码。

所有消息的基类信息CorpSendBase基类对象代码如下

    /// <summary>
    /// 企业号发送消息的基础消息内容
    /// </summary>
    public class CorpSendBase
    {
        /// <summary>
        /// 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
        /// </summary>
        public string touser { get; set; }

        /// <summary>
        /// 部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
        /// </summary>
        public string toparty { get; set; }

        /// <summary>
        /// 标签ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
        /// </summary>
        public string totag { get; set; }

        /// <summary>
        /// 消息类型
        /// </summary>
        public string msgtype { get; set; }

        /// <summary>
        /// 企业应用的id,整型。可在应用的设置页面查看
        /// </summary>
        public string agentid { get; set; }

        /// <summary>
        /// 表示是否是保密消息,0表示否,1表示是,默认0
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string safe { get; set; }

    }

其他的文本卡片、markdown消息、小程序通知消息等内容的对象继承它,增加自己对象的消息即可。

如文本卡片的类及其子类的代码如下所示。

    /// <summary>
    /// 文本卡片消息
    /// </summary>
    public class CorpSendTextCard : CorpSendBase
    {
        public CorpSendTextCard()
        {
            this.msgtype = CorpMsgType.textcard;
            this.textcard = new CorpSendTextCardEntity();
        }

        public CorpSendTextCard(string title, string description, string url, string btntxt = "详情")
        {
            this.msgtype = CorpMsgType.textcard;
            this.textcard = new CorpSendTextCardEntity(title, description, url, btntxt);
        }

        /// <summary>
        /// 消息内容
        /// </summary>
        public CorpSendTextCardEntity textcard { get; set; }
    }

    public class CorpSendTextCardEntity
    {
        /// <summary>
        /// 标题,不超过128个字节,超过会自动截断
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 描述,不超过512个字节,超过会自动截断
        /// </summary>
        public string description { get; set; }
        /// <summary>
        /// 点击后跳转的链接。
        /// </summary>
        public string url { get; set; }
        /// <summary>
        /// 按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断。
        /// </summary>
        public string btntxt { get; set; }

        public CorpSendTextCardEntity()
        { }

        public CorpSendTextCardEntity(string title, string description, string url, string btntxt = "详情")
        {
            this.title = title;
            this.description = description;
            this.url = url;
            this.btntxt = btntxt;
        }
    }

效果借用官方的效果图,如下所示

markdown消息对象如下所示

   /// <summary>
    /// markdown消息
    /// 目前仅支持markdown语法的子集
    /// 微工作台(原企业号)不支持展示markdown消息
    /// </summary>
    public class CorpSendMarkdown : CorpSendBase
    {
        public CorpSendMarkdown()
        {
            this.msgtype = CorpMsgType.markdown;
            this.markdown = new CorpSendMarkdownEntity();
        }

        public CorpSendMarkdown(string content)
        {
            this.msgtype = CorpMsgType.markdown;
            this.markdown = new CorpSendMarkdownEntity(content);
        }

        /// <summary>
        /// 消息内容
        /// </summary>
        public CorpSendMarkdownEntity markdown { get; set; }
    }

效果如下所示

 

小程序通知消息

    /// <summary>
    /// 小程序通知消息
    /// </summary>
    public class CorpSendMiniProgram : CorpSendBase
    {
        public CorpSendMiniProgram()
        {
            this.msgtype = CorpMsgType.miniprogram_notice;
            this.textcard = new CorpSendMiniProgramEntity();
        }

        /// <summary>
        /// 消息内容
        /// </summary>
        public CorpSendMiniProgramEntity textcard { get; set; }
    }

小程序的消息界面效果如下

有了这些消息的定义,我们就可以统一使用接口进行发送了。

 

2、发送企业微信信息

定义一个消息发送的接口,接口函数的参数,包括accesstoken和消息对象的基类,如下所示。

    /// <summary>
    /// 企业微信消息管理接口定义
    /// </summary>
    public interface ICorpMessageApi
    {
        /// <summary>
        /// 发送消息。
        /// 消息型应用支持文本、图片、语音、视频、文件、图文等消息类型。主页型应用只支持文本消息类型,且文本长度不超过20个字。
        /// 需要管理员对应用有使用权限,对收件人touser、toparty、totag有查看权限,否则本次调用失败。
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        CommonResult SendMessage(string accessToken, CorpSendBase data);
    }

实现接口的代码如下所示

        /// <summary>
        /// 发送消息。
        /// 消息型应用支持文本、图片、语音、视频、文件、图文等消息类型。主页型应用只支持文本消息类型,且文本长度不超过20个字。
        /// 需要管理员对应用有使用权限,对收件人touser、toparty、totag有查看权限,否则本次调用失败。
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public CommonResult SendMessage(string accessToken, CorpSendBase data)
        {        
            CommonResult result = new CommonResult();
            var url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", accessToken);
            var postData = data.ToJson();

            //数据不用加密发送
            CorpSendResult sendResult = WeJsonHelper<CorpSendResult>.ConvertJson(url, postData);
            if (sendResult != null)
            {
                result.Success = (sendResult.errcode == 0);
                if (!result.Success)
                {
                    result.ErrorMessage = string.Format("invaliduser:{0},invalidparty:{1},invalidtag:{2}",
                        sendResult.invaliduser, sendResult.invalidparty, sendResult.invalidtag);
                }
            }

            return result;
        }

定义好相应的发送对象后,我们就可以进行统一的消息发送操作,包括文本、图片、文件、语音等等类型的消息,注意有些消息是需要上传到服务器上,然后在根据mediaId进行发送出去的。

程序测试接口发送的调用代码如下所示:

文本卡片发送代码

        /// <summary>
        /// 文本卡片消息发送
        /// </summary>
        private void btnSendTextCard_Click(object sender, EventArgs e)
        {
            ICorpMessageApi bll = new CorpMessageApi();
            CorpSendTextCard msg = new CorpSendTextCard("中秋节礼品领取", "今年中秋节公司有豪礼相送", "http://www.iqidi.com", "更多详情");
            msg.agentid = agentid;
            msg.touser = "wuhuacong";

            CommonResult result = bll.SendMessage(token, msg);
            if (result != null)
            {
                Console.WriteLine("发送TextCard消息:{0} {1} {2}", fileMediaId, (result.Success ? "成功" : "失败"), result.ErrorMessage);
            }
        }

Markdown的发送代码如下所示。

        /// <summary>
        /// MarkDown消息发送
        /// </summary>
        private void btnSendMarkDown_Click(object sender, EventArgs e)
        {
            ICorpMessageApi bll = new CorpMessageApi();
            string content = @"您的会议室已经预定,稍后会同步到`邮箱`
>**事项详情**
>事 项:<font color='info'>开会</font>
>组织者:@miglioguan
>参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang
>  
>会议室:< font color ='info'>广州TIT 1楼 301</font>
>日期:< font color ='warning'>2018年5月18日</font>
>时间:< font color ='comment'>上午9:00-11:00</font>
>
> 请准时参加会议。
>
> 如需修改会议信息,请点击:[修改会议信息] (https://work.weixin.qq.com)";
            CorpSendMarkdown msg = new CorpSendMarkdown(content);
            msg.agentid = agentid;
            msg.touser = "wuhuacong";

            CommonResult result = bll.SendMessage(token, msg);
            if (result != null)
            {
                Console.WriteLine("发送Markdown消息:{0} {1} {2}", fileMediaId, (result.Success ? "成功" : "失败"), result.ErrorMessage);
            }
        }

 

posted on 2019-02-26 12:30  伍华聪  阅读(24638)  评论(3编辑  收藏  举报

导航