企業(yè)微信發(fā)送應(yīng)用消息的實現(xiàn)

企業(yè)號升級到企業(yè)微信后,發(fā)送應(yīng)用消息的接口也變化了不少,除了原來的文本、圖片、文件、語音、視頻、圖文消息等消息外,增加了文本卡片、markdown消息、小程序通知消息等內(nèi)容,不過它們都可以共用一個接口進(jìn)行發(fā)送,只是它們的對象不太一樣,本篇隨筆主要介紹整個企業(yè)微信應(yīng)用消息處理這部分內(nèi)容,包括不同消息的實體關(guān)系和接口發(fā)送的實現(xiàn)等內(nèi)容。

1、企業(yè)微信消息對象的定義

在早期還是微信企業(yè)號的時候,我對發(fā)送企業(yè)號消息的接口也已經(jīng)實現(xiàn),參考《C#開發(fā)微信門戶及應(yīng)用(19)-微信企業(yè)號的消息發(fā)送(文本、圖片、文件、語音、視頻、圖文消息等)》,這次對企業(yè)號升級到企業(yè)微信接口,對所有接口進(jìn)行了梳理和測試。

我們先看看企業(yè)微信對應(yīng)用消息接口的介紹(https://work.weixin.qq.com/api/doc#90000/90135/90236

image

根據(jù)消息的類型,我們增加了一些額外的對象實體類,修改后的關(guān)系圖如下所示。

image

我們來看看新增的文本卡片、markdown消息、小程序通知消息等內(nèi)容的對象定義代碼。

所有消息的基類信息CorpSendBase基類對象代碼如下

    /// <summary>
    /// 企業(yè)號發(fā)送消息的基礎(chǔ)消息內(nèi)容
    /// </summary>
    public class CorpSendBase
    {
        /// <summary>
        /// 成員ID列表(消息接收者,多個接收者用‘|’分隔,最多支持1000個)。特殊情況:指定為@all,則向關(guān)注該企業(yè)應(yīng)用的全部成員發(fā)送
        /// </summary>
        public string touser { get; set; }

        /// <summary>
        /// 部門ID列表,多個接收者用‘|’分隔,最多支持100個。當(dāng)touser為@all時忽略本參數(shù)
        /// </summary>
        public string toparty { get; set; }

        /// <summary>
        /// 標(biāo)簽ID列表,多個接收者用‘|’分隔,最多支持100個。當(dāng)touser為@all時忽略本參數(shù)
        /// </summary>
        public string totag { get; set; }

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

        /// <summary>
        /// 企業(yè)應(yīng)用的id,整型??稍趹?yīng)用的設(shè)置頁面查看
        /// </summary>
        public string agentid { get; set; }

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

    }

其他的文本卡片、markdown消息、小程序通知消息等內(nèi)容的對象繼承它,增加自己對象的消息即可。

如文本卡片的類及其子類的代碼如下所示。

    /// <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>
        /// 消息內(nèi)容
        /// </summary>
        public CorpSendTextCardEntity textcard { get; set; }
    }

    public class CorpSendTextCardEntity
    {
        /// <summary>
        /// 標(biāo)題,不超過128個字節(jié),超過會自動截斷
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 描述,不超過512個字節(jié),超過會自動截斷
        /// </summary>
        public string description { get; set; }
        /// <summary>
        /// 點擊后跳轉(zhuǎn)的鏈接。
        /// </summary>
        public string url { get; set; }
        /// <summary>
        /// 按鈕文字。 默認(rèn)為“詳情”, 不超過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;
        }
    }

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

image

markdown消息對象如下所示

   /// <summary>
    /// markdown消息
    /// 目前僅支持markdown語法的子集
    /// 微工作臺(原企業(yè)號)不支持展示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>
        /// 消息內(nèi)容
        /// </summary>
        public CorpSendMarkdownEntity markdown { get; set; }
    }

效果如下所示

小程序通知消息

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

        /// <summary>
        /// 消息內(nèi)容
        /// </summary>
        public CorpSendMiniProgramEntity textcard { get; set; }
    }

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

有了這些消息的定義,我們就可以統(tǒng)一使用接口進(jìn)行發(fā)送了。

2、發(fā)送企業(yè)微信信息

定義一個消息發(fā)送的接口,接口函數(shù)的參數(shù),包括accesstoken和消息對象的基類,如下所示。

    /// <summary>
    /// 企業(yè)微信消息管理接口定義
    /// </summary>
    public interface ICorpMessageApi
    {
        /// <summary>
        /// 發(fā)送消息。
        /// 消息型應(yīng)用支持文本、圖片、語音、視頻、文件、圖文等消息類型。主頁型應(yīng)用只支持文本消息類型,且文本長度不超過20個字。
        /// 需要管理員對應(yīng)用有使用權(quán)限,對收件人touser、toparty、totag有查看權(quán)限,否則本次調(diào)用失敗。
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        CommonResult SendMessage(string accessToken, CorpSendBase data);
    }

實現(xiàn)接口的代碼如下所示

        /// <summary>
        /// 發(fā)送消息。
        /// 消息型應(yīng)用支持文本、圖片、語音、視頻、文件、圖文等消息類型。主頁型應(yīng)用只支持文本消息類型,且文本長度不超過20個字。
        /// 需要管理員對應(yīng)用有使用權(quán)限,對收件人touser、toparty、totag有查看權(quán)限,否則本次調(diào)用失敗。
        /// </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();

            //數(shù)據(jù)不用加密發(fā)送
            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;
        }

定義好相應(yīng)的發(fā)送對象后,我們就可以進(jìn)行統(tǒng)一的消息發(fā)送操作,包括文本、圖片、文件、語音等等類型的消息,注意有些消息是需要上傳到服務(wù)器上,然后在根據(jù)mediaId進(jìn)行發(fā)送出去的。

程序測試接口發(fā)送的調(diào)用代碼如下所示:

文本卡片發(fā)送代碼

        /// <summary>
        /// 文本卡片消息發(fā)送
        /// </summary>
        private void btnSendTextCard_Click(object sender, EventArgs e)
        {
            ICorpMessageApi bll = new CorpMessageApi();
            CorpSendTextCard msg = new CorpSendTextCard("中秋節(jié)禮品領(lǐng)取", "今年中秋節(jié)公司有豪禮相送", "http://www.iqidi.com", "更多詳情");
            msg.agentid = agentid;
            msg.touser = "wuhuacong";

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

Markdown的發(fā)送代碼如下所示。

        /// <summary>
        /// MarkDown消息發(fā)送
        /// </summary>
        private void btnSendMarkDown_Click(object sender, EventArgs e)
        {
            ICorpMessageApi bll = new CorpMessageApi();
            string content = @"您的會議室已經(jīng)預(yù)定,稍后會同步到`郵箱`
>**事項詳情**
>事 項:<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>
>
> 請準(zhǔn)時參加會議。
>
> 如需修改會議信息,請點擊:[修改會議信息] (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("發(fā)送Markdown消息:{0} {1} {2}", fileMediaId, (result.Success ? "成功" : "失敗"), result.ErrorMessage);
            }
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容