public void SendTemplateMessageTest() { string templateId = "QMLApk4W8Jm9sUgwsCd-6jT1vxZAEUQXdJHMjF8bE3Y"; string detailUrl = "http://www.github.com/wlitsoft/weixinsdk"; TemplateMessageParameterDictionary pdic = new TemplateMessageParameterDictionary(); pdic.Add("Content", new TemplateMessageParameterDataModel($"当前时间:{DateTime.Now}")); SendTemplateMessageResultModel result = TemplateMessageApi.SendTemplateMessage(base.TestOpenId, templateId, detailUrl, "#ffffff", pdic); Assert.NotNull(result); Assert.True(result.MsgId > 0); }
/// <summary> /// 发送模板消息。 /// </summary> /// <param name="openId">发送给用户的编号。</param> /// <param name="templateId">要使用的模板编号。</param> /// <param name="detailUrl">模板消息的详情链接地址。</param> /// <param name="topColor">模板消息头 html 颜色。</param> /// <param name="parameterDic">模板参数字典。</param> /// <returns>发送模板消息结果对象。</returns> public static SendTemplateMessageResultModel SendTemplateMessage(string openId, string templateId, string detailUrl, string topColor, TemplateMessageParameterDictionary parameterDic) { #region 参数校验 if (string.IsNullOrEmpty(openId)) { throw new StringNullOrEmptyException(nameof(openId)); } if (string.IsNullOrEmpty(templateId)) { throw new StringNullOrEmptyException(nameof(templateId)); } if (string.IsNullOrEmpty(detailUrl)) { throw new StringNullOrEmptyException(nameof(detailUrl)); } if (string.IsNullOrEmpty(topColor)) { throw new StringNullOrEmptyException(nameof(topColor)); } if (parameterDic == null) { throw new ObjectNullException(nameof(parameterDic)); } #endregion string url = $"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={WeixinApp.TokenService.GetToken()}"; #region 组织数据 var postdata = new { touser = openId, template_id = templateId, url = detailUrl, topcolor = topColor, data = parameterDic }; #endregion return(url.PostApiInvokeResult <SendTemplateMessageResultModel>(postdata)); }