.netcore 微信模板消息 实现(公众号和小程序)
推荐一个轮子给大家
Sugar.WeChat
引用工具类
Install-Package Sugar.WeChat -Version 1.1.2
在也可在项目 nuget中添加Sugar.WeChat
startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//这里添加微信类库
services.UseWeChatTemplateMessage(o => { o.AppId = "你的appid";o.AppSecret = "你的appsecret"; });
//也可以这样
//services.AddWeChatTemplateMessage(Configuration.GetSection("WeChat"));
}
控制器
public class WeChatTemplageMessageController : Controller
{
Sugar.WeChat.TemplateMsg.TemplateMessageProvider provider;
public WeChatTemplageMessageController(Sugar.WeChat.TemplateMsg.TemplateMessageProvider provider)
{
this.provider = provider;
}
public async Task<IActionResult> IndexAsync()
{
//发送公众号模板消息
await provider.SendOffiAccountMessageAsync(new OffiAccountMessage()
{
TemplateId = "JLc7M--uP751GxvVf2_Msqp1m_gne0XXiSzJISloy8g",
Url = "www.baidu.com",
Data = new MessageContent()
{
MessageTitle = new MessageContentItem("测试title"),
MessageDatas = new List<MessageContentItem>() { new MessageContentItem("数据1"), new MessageContentItem("数据2") },
Remark = new MessageContentItem("备注信息")
}
}, "o6qCa1CdDnDQhEkPmwWJynGMQ4Ho");
return View();
}
}