C# 微信消息模板 发送
程序员文章站
2022-08-02 10:57:31
项目要用到微信提醒 ,加上调转到小程序页面,或者 指定url 用到 RestSharp、Senparc.Weixin 类库 一开始直接照着微信示例直接post进去 发现一直提示 47001 ,估计是我姿势水平不太够,还是用个类库操作吧 ......
项目要用到微信提醒 ,加上调转到小程序页面,或者 指定url
用到 restsharp、senparc.weixin 类库
一开始直接照着微信示例直接post进去 发现一直提示 47001 ,估计是我姿势水平不太够,还是用个类库操作吧
using restsharp;
using senparc.weixin.mp.advancedapis.templatemessage;
using system;
namespace templateapp1
{
class program
{
public static string openid = "";
public static string template_id = "";
public static string accesstoken = getaccess_token();
static void main(string[] args)
{
//网页跳转
sendtemplatemessageresult t = sendtemplateurl(accesstoken, openid, template_id);
//小程序跳转
sendtemplatemessageresult t1 = sendtemplatminiprogram(accesstoken, openid, template_id);
console.writeline(t+"\n"+t1);
console.readkey();
}
/// <summary>
/// 网页跳转
/// </summary>
/// <param name="openid"></param>
/// <param name="template_id"></param>
/// <returns></returns>
public static sendtemplatemessageresult sendtemplateurl(string accesstoken, string openid,string template_id)
{
var data = new
{
first = new templatedataitem("网页跳转"),
keyword1 = new templatedataitem("keyword1"),
keyword2 = new templatedataitem(datetime.now.tostring("yyyy年mm月dd日 hh:mm")),
remark = new templatedataitem("remark"),
};
string url = "https://baidu.com";
return senparc.weixin.mp.advancedapis.templateapi.sendtemplatemessage(accesstoken, openid, template_id, url, data,null);
}
/// <summary>
/// 小程序跳转
/// </summary>
/// <param name="openid"></param>
/// <param name="template_id"></param>
/// <returns></returns>
public static sendtemplatemessageresult sendtemplatminiprogram(string accesstoken,string openid, string template_id)
{
var data = new
{
first = new templatedataitem("小程序跳转"),
keyword1 = new templatedataitem("keyword1"),
keyword2 = new templatedataitem(datetime.now.tostring("yyyy年mm月dd日 hh:mm")),
remark = new templatedataitem("remark"),
};
// 小程序
templetemodel_miniprogram miniprogram = new templetemodel_miniprogram
{
appid = "",
pagepath = ""
};
string url = string.empty;
return senparc.weixin.mp.advancedapis.templateapi.sendtemplatemessage(accesstoken, openid, template_id, url, data, miniprogram);
}
/// <summary>
/// 获取 accesstoken 需保存 有次数限制
/// </summary>
/// <returns></returns>
public static string getaccess_token()
{
string url = "";
var request = new restrequest("", method.get);
restclient restclient = new restclient(url);
return restclient.execute(request).content;
}
}
}
上一篇: 纸刀也能伤人?揭秘我国清末时期三大妖术!