微信消息模板

发送模板消息

接口调用请求说明

http请求方式: POST
https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

POST数据说明

POST数据示例如下:

      {
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"miniprogram":{
"appid":"xiaochengxuappid12345",
"pagepath":"index?foo=bar"
},
"data":{
"first": {
"value":"恭喜你购买成功!",
"color":"#173177"
},
"keyword1":{
"value":"巧克力",
"color":"#173177"
},
"keyword2": {
"value":"39.8元",
"color":"#173177"
},
"keyword3": {
"value":"2014年9月22日",
"color":"#173177"
},
"remark":{
"value":"欢迎再次购买!",
"color":"#173177"
}
}
}

参数说明

参数是否必填说明
touser 接收者openid
template_id 模板ID
url 模板跳转链接(海外帐号没有跳转能力)
miniprogram 跳小程序所需数据,不需跳小程序可不用传该数据
appid 所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系,暂不支持小游戏)
pagepath 所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar),要求该小程序已发布,暂不支持小游戏
data 模板数据
color 模板内容字体颜色,不填默认为黑色

注:url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。

返回码说明

在调用模板消息接口后,会返回JSON数据包。正常时的返回JSON数据包示例:

 {
"errcode":0,
"errmsg":"ok",
"msgid":200228332
}

使用效果

 

 

 

  private SendResultDto SendMessage(ActivityAwardDto activityAward, AwardingRecord awardingRecord, int awardType)
        {
            SendResultDto sendResult = new SendResultDto
            {
                errcode = "-1",
                errmsg = "发送中奖通知失败",
                msgid = string.Empty
            };
            try
            {
                MessageTemplateDto messageTemplateDto = new MessageTemplateDto();
                MessageData messageData = new MessageData();
                messageData.first = new DataValue { value = "恭喜你中奖了", color = "#173177" };
                messageData.keyword1 = new DataValue { value = "红牛扫码赢红包活动", color = "#173177" };
                messageData.keyword2 = new DataValue { value = awardType == (int)AwardTypeEnum.RedPacket ? "红包" + activityAward.RedPacketPrice + "" : "优惠券" + activityAward.CouponPrice + "", color = "#C70000" };
                messageData.keyword3 = new DataValue { value = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm"), color = "#173177" };
                messageData.remark = new DataValue { value = "点击查看详情。", color = "#173177" };

                messageTemplateDto.touser = awardingRecord.WechatUserOpenId;
                messageTemplateDto.template_id = "你的模板id";
                messageTemplateDto.topcolor = "#000000";
                if (awardType == (int)AwardTypeEnum.RedPacket)
                {
                    messageTemplateDto.url = _configuration["AppSetting:GetActivityUrl"] + "UserAward?publicAccountId=" + awardingRecord.PublicAccountId.Value.ToString();
                }
                if (awardType == (int)AwardTypeEnum.Coupon)
                {
                    messageTemplateDto.miniprogram = new Miniprogram { appid = "你的appid", pagepath = "pages/drawn/index?balance=" + activityAward.CouponPrice.ToString() };
                }

                messageTemplateDto.data = messageData;
                var json = JsonConvert.SerializeObject(messageTemplateDto);

                SendMessageTemplateService sendMessageTemplateService = new SendMessageTemplateService(_configuration, HttpContext);
                //发送消息模板
                sendResult = sendMessageTemplateService.SendMessageTemplate(messageTemplateDto);

                SendMessageLog sendMessageLog = new SendMessageLog
                {
                    AwardingRecordId = awardingRecord.Id,
                    ActivityId = awardingRecord.ActivityId,
                    WechatUserId = awardingRecord.WechatUserId,
                    WechatUserName = awardingRecord.WechatUserName,
                    MessageTitle = "红包中奖通知",
                    Touser = messageTemplateDto.topcolor,
                    TemplateId = messageTemplateDto.template_id,
                    Url = messageTemplateDto.url,
                    Errcode = sendResult.errcode,
                    Errmsg = sendResult.errmsg,
                    Msgid = sendResult.msgid,
                    CreationTime = DateTime.Now
                };
                //添加发送日志
                _sendMessageLogDomainService.Create(sendMessageLog);
            }
            catch (Exception ex)
            {
                Logger.Error("SendMessageForWinning Error", ex);
            }

            return sendResult;
        }
        #endregion

 

posted on 2019-04-24 09:55  Leegj  阅读(695)  评论(0编辑  收藏  举报