C# .NET调用微信模板消息接口
直接上代码
// 调用接口方法
public static string PushMsg(T_PushTemplate tp, T_Template ttemp, string openid, string name)
{
WechatTemplateModel model = new WechatTemplateModel();
Content Content = new Content();
Field first = new Field();
Field keyword1 = new Field();
Field keyword2 = new Field();
Field keyword3 = new Field();
Field remark = new Field();
string time = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
if (ttemp.template_id.Trim() == ConfigurationManager.AppSettings["VideoUploadID"]) //VideoUploadID
{
first.value = ttemp.first.Trim();
keyword1.value = ttemp.keyword1.Trim();
keyword2.value = tp.Title;
keyword3.value = DateTime.Now.ToString();
remark.value = ttemp.remark.Trim();
first.color = ttemp.firstColor.Trim();
keyword1.color = ttemp.keyword1Color.Trim();
keyword2.color = ttemp.TemplateNameColor.Trim();
remark.color = ttemp.remarkColor.Trim();
}
else if (ttemp.template_id.Trim() == ConfigurationManager.AppSettings["DeliveryStatusUpdateID"]) //
{
first.value = ttemp.first.Trim();
keyword1.value = tp.Title;
keyword2.value = DateTime.Now.ToString();
remark.value = ttemp.remark.Trim();
keyword1.color = ttemp.TemplateNameColor.Trim();
remark.color = ttemp.remarkColor.Trim();
}
else
{
first.value = ttemp.first.Trim();
keyword1.value = tp.Title; //keyword1 -- 会议主题
keyword2.value = ttemp.keyword1.Trim(); //keyword2 -- 开始时间
remark.value = ttemp.remark.Trim();
first.color = ttemp.firstColor.Trim();
keyword1.color = ttemp.TemplateNameColor.Trim();
keyword2.color = ttemp.keyword1Color.Trim();
remark.color = ttemp.remarkColor.Trim();
}
Content.first = first;
Content.keyword1 = keyword1;
Content.keyword2 = keyword2;
Content.keyword3 = keyword3;
Content.remark = remark;
model.data = Content;
model.touser = openid.Trim();
model.template_id = ttemp.template_id.Trim();
model.url = tp.FileURL.Trim();
//通过接口获取token
EntityWXAccessToken token = TokenHandler.GetToken();
string str1 = "";
var result = WechatHandler.PushTemplateMsg(token, JsonConvert.SerializeObject(model), out str1);
return result.errcode.ToString();
}
//模板消息需要的实体
public class WechatTemplateModel
{
public string touser { get; set; }
public string template_id { get; set; }
public string url { get; set; }
public Miniprogram miniprogram { get; set; }
public Content data { get; set; }
}
public class Content
{
public Field first { get; set; }
public Field keyword1 { get; set; }
public Field keyword2 { get; set; }
public Field keyword3 { get; set; }
public Field remark { get; set; }
}
public class Field
{
public string value { get; set; }
public string color { get; set; }
}
public class Miniprogram
{
public string appid { get; set; }
public string pagepath { get; set; }
}
总结
微信模板消息接口是每个User需要一个post请求,模板需要在Console后台申请,取得模板ID才可以调用接口,更多请访问微信模板消息开发者文档
PS:仅供学习使用,如有侵权请联系我删除。
版权声明:本文为CSDN_wcy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。