One technology per day: wechat development – send template message

Time:2021-6-9

summary

Template messages are used only for official account sending important service notifications to users. They can only be used in service scenarios that meet their requirements, such as credit card notification, purchase success notice, etc. It does not support marketing messages such as advertisements and all other messages that may cause harassment to users.

The method of establishing template is shown in

https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html

Let’s talk about the specific code implementation.

Document definition

Interface call request description

HTTP request mode: Post https://api.weixin.qq.com/cgi-bin/message/template/send?access_ token=ACCESS_ TOKEN

Post data description

An example of post data is as follows:

{
           "touser":"OPENID",
           "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
           "url":"http://weixin.qq.com/download",  
           "miniprogram":{
             "appid":"xiaochengxuappid12345",
             "pagepath":"index?foo=bar"
           },          
           "data":{
                   "first": {
                       "Value": "congratulations on your purchase!",
                       "color":"#173177"
                   },
                   "keyword1":{
                       "Value": "chocolate",
                       "color":"#173177"
                   },
                   "keyword2": {
                       "Value": "39.8 yuan",
                       "color":"#173177"
                   },
                   "keyword3": {
                       "Value": "September 22, 2014,",
                       "color":"#173177"
                   },
                   "remark":{
                       "Value": "welcome to buy again!",
                       "color":"#173177"
                   }
           }
       }

Implementation mode

1. Define organization template data

Template_Id = TemplateIditem.TemplateId.FirstOrDefault(o => o.type == "Disconnected").value;
                var Data = new
                {
                    First = new templatedataitem() {value = you are disconnected! "},
                    keyword1 = new TemplateDataItem() { value = userMgmtExtra.EMail },
                    Keyword2 = new templatedataitem() {value = datetime. Now. ToString ("mm, DD, yyyy")},
                    Remark = new templatedataitem() {value = "you have unbound wechat. If you want to continue to receive xxxxx system information, please rebind to the account of XXXXX. Thank you for your use"}
                };

                TemplateData = JsonConvert.SerializeObject(Data);

 

if (item.OpenId != null && Template_Id != null && TemplateData != null)
            {
                return new TemplateSendParam()
                {
                    OpenId = item.OpenId,
                    Template_Id = Template_Id,
                    TemplateData = TemplateData
                };
            }

2. Post request wechat API

private static Tuple TemplateSend(string AppId, string Appsecret, string OPENID, string template_id, string TemplateData, bool isResh)
        {
            string jsonstr = "{\"touser\":\"" + OPENID + "\",\"template_id\":\"" + template_id + "\", \"data\":" + TemplateData + " }";
            var accessToken = TryGetAccessToken(AppId, Appsecret, isResh);
            string url = APIConfig.Templatesend(accessToken);
            string str = APIConfig.CreatePostHttpResponse(url, jsonstr);
            _ Logger. Debug ("send template message '+" input' + URL '+ "- jsonstr' +" - output '+ STR ");
            return new Tuple(JsonConvert.DeserializeObject(str), url, jsonstr);
        }

effect