.Net Core 企业微信更新模版卡片消息

2.编写代码

2.1接口定义

应用可以发送模板卡片消息,发送之后可再通过接口更新可回调的用户任务卡片消息的替换文案信息(仅原卡片为 按钮交互型、投票选择型、多项选择型的卡片以及填写了action_menu字段的文本通知型、图文展示型可以调用本接口更新)。

请注意,当应用调用发送模版卡片消息后,接口会返回一个response_code,通过response_code用户可以调用本接口一次。后续如果有用户点击任务卡片,回调接口也会带上response_code,开发者通过该code也可以调用本接口一次,注意response_code的有效期是24小时,超过24小时后将无法使用。

请求方式:POST(HTTPS
请求地址: https://qyapi.weixin.qq.com/cgi-bin/message/update_template_card?access_token=ACCESS_TOKEN

参数说明:

access_token:接口授权

2.2 appsettings配置

  根据实际情况填写、

      corpid 企业ID

      corpsecret 应用密钥,

      CallBackToken 企业微信后台,开发者设置的Token,

      EncodingAESKey企业微信后台,开发者设置的EncodingAESKey。

复制代码
复制代码
 "Wx": {
    "Baseurl": "https://qyapi.weixin.qq.com/cgi-bin/",
    "PushUrl": "message/send?access_token={0}",
    "PushCardUrl": "message/update_template_card?access_token={0}",
    "PushTokenUrl": "gettoken?corpid=&corpsecret=",
    "CallBackToken": "",
    "EncodingAESKey": "",
    "corpid": ""
  }
复制代码
复制代码

2.3 Startup.cs

复制代码
复制代码
public void ConfigureServices(IServiceCollection services)
 {
       services.AddHttpClient("WxClient", config => 
            {
                config.BaseAddress = new Uri(Configuration["Wx:baseurl"]);
                config.DefaultRequestHeaders.Add("Accept", "application/json");
            });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    GlobalContext.httpClientFactory = app.ApplicationServices.GetService<IHttpClientFactory>();
}
复制代码
复制代码

2.4 GlobalContext.cs

  提供了Token,推送等方法。

 GlobalContext.cs

2.5 回调中编写内容

  下图中是按钮交互性的按钮,点击确认会触发回调服务器的方法,在回调服务中根据返回的FromUserName和ResponseCode调用更新模板方法,把按钮改为已推送。

 

   下面代码为回调服务中的Post方法,在1中搭建回调服务器中的方法。业务逻辑,失主请求获取联系方式,拾取人点击确认,推送联系方式至失主企业微信。将确认更新成已发送。可根据自己的实际业务替换内容,其中UpdateCard方法为更新模板已推送方法。

复制代码
复制代码
[HttpPost, Route("callback/interAspect")]
        public ContentResult AcceptMessage(string msg_signature,string timestamp,string nonce)
        {
            //获取被动响应包
            string encrypt = "";
            using (StreamReader sr = new StreamReader(Request.Body, Encoding.UTF8))
            {
                encrypt = sr.ReadToEndAsync().Result;

            }
            //验证
            WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(AppSetting.Configuration["Wx:CallBackToken"]
                , AppSetting.Configuration["Wx:EncodingAESKey"]
                , AppSetting.Configuration["Wx:corpid"]);

            string sMsg = "";  // 解析之后的明文
            int ret = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, encrypt, ref sMsg);
            if (ret != 0)
            {
                throw new Exception();
            }
            // ret==0表示解密成功,sMsg表示解密之后的明文xml串
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sMsg);
            XmlNode root = doc.FirstChild;

            string userName = root["FromUserName"].InnerText;
            string eventKey = root["EventKey"].InnerText;
            string responseCode = root["ResponseCode"].InnerText;

            //业务逻辑 eventKey是我保存的请求人推送UserID。
            var content = GlobalContext.GetTextContent(eventKey, $"我的联系方式:" + userName, 1);
            var message = GlobalContext.WxPush(content);
            if (message == "推送成功!")
            {
                try
                {
                    var responseContent = GlobalContext.UpdateTextCardContent(new string[] { userName }, responseCode, "已推送", 1);
            var updateMessage = GlobalContext.UpdateCard(responseContent);
                    if (updateMessage == "推送成功!")
                    {
                        return Content("成功");
                    }
                    else
                    {throw new Exception();
                    }
                }
                catch(Exception ex)
                {throw new Exception();
                }
               
            }
            else
            {
                throw new Exception();
            }
        }
复制代码
复制代码

3.测试

 4.链接

  更新模板卡片:https://developer.work.weixin.qq.com/document/path/94888

 

转 https://www.cnblogs.com/zspwf/p/16392462.html

posted @   dreamw  阅读(479)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示