C# 后端调用微信公众号推送消息
1、微信公众号文档
2、测试代码
/// <summary> /// 测试公众号发送推送消息 /// </summary> public async Task<bool> SendMessage() { //公众号的appid|secret var appid = "填写appid"; var secret = "填写secret"; //获取access_token string getTokenUrl = $@"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}"; HttpClient tokenClient = new(); string responseBody = await tokenClient.GetStringAsync(getTokenUrl); AccessTokenDto at = JsonConvert.DeserializeObject<AccessTokenDto>(responseBody); string access_token = at.access_token; //根据access_token构建推送接口 string sendUrl = $@"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={access_token}"; HttpClient sendclient = new(); //构建请求数据对象 MessageTemplateSendDto mts = new() {//必填 接收者openid touser = "填写接收者openid", //必填 模板ID template_id = "填写模板id", //必填 所需跳转到的小程序appid(该小程序 appid 必须与发模板消息的公众号是绑定关联关系,暂不支持小游戏) miniprogram = new MiniprogramDto() { //小程序id appid = "填写小程序appid", //所需跳转到小程序的具体页面路径,支持带参数,(示例index? foo = bar),要求该小程序已发布,暂不支持小游戏 //pagepath = @"/pages/basics/jobReport/MyDraftBox" pagepath = @"/pages/basics/jobReport/WaitingReport?EmpID=10007458&EmpName=张三&itemWaitingType=ReportType02&ReportTypeName=工作周报" }, //必填 模板数据 data = new MessageTemplateSendDataDto { first = new MessageTemplateSendDataContentDto() { value = "工作任务提示测试发送", color = "#173177" }, keyword1 = new MessageTemplateSendDataContentDto() { value = "张三", color = "#173177" }, keyword2 = new MessageTemplateSendDataContentDto() { value = "2022-08-24", color = "#173177" }, keyword3 = new MessageTemplateSendDataContentDto() { value = "有待办信息了,请审核操作", color = "#173177" }, remark = new MessageTemplateSendDataContentDto() { value = "请及时处理", color = "#173177" } } }; string testStr = JsonConvert.SerializeObject(mts);//查看推送内容(测试用) HttpContent content = new StringContent(JsonConvert.SerializeObject(mts), Encoding.UTF8);//创建推送连接 var postResult = await sendclient.PostAsync(sendUrl, content);//post发送推送数据 return true; }
数据对象
public class AccessTokenDto { /// <summary> /// 获取到的凭证 /// </summary> public string access_token { get; set; } /// <summary> /// 凭证有效时间。单位:秒 /// </summary> public string expires_in { get; set; } } public class MessageTemplateSendDataContentDto { /// <summary> /// 文本内容 /// </summary> public string value { get; set; } /// <summary> /// 文本颜色 /// </summary> public string color { get; set; } } public class MessageTemplateSendDataDto { public MessageTemplateSendDataContentDto first { get; set; } public MessageTemplateSendDataContentDto keyword1 { get; set; } public MessageTemplateSendDataContentDto keyword2 { get; set; } public MessageTemplateSendDataContentDto keyword3 { get; set; } public MessageTemplateSendDataContentDto remark { get; set; } } public class MessageTemplateSendDto { /// <summary> /// 必填 /// 接收者openid /// </summary> public string touser { get; set; } /// <summary> /// 必填 /// 模板ID /// </summary> public string template_id { get; set; } /// <summary> /// 模板跳转链接(海外帐号没有跳转能力) /// </summary> public string url { get; set; } /// <summary> /// 跳小程序所需数据,不需跳小程序可不用传该数据 /// </summary> public MiniprogramDto miniprogram { get; set; } /// <summary> /// 必填 /// 模板数据 /// </summary> public MessageTemplateSendDataDto data { get; set; } /// <summary> /// 模板内容字体颜色,不填默认为黑色 /// </summary> public string color { get; set; } /// <summary> /// 防重入id。对于同一个openid + client_msg_id, 只发送一条消息,10分钟有效,超过10分钟不保证效果。若无防重入需求,可不填 /// </summary> public string client_msg_id { get; set; } } public class MiniprogramDto { /// <summary> /// 必填 /// 所需跳转到的小程序appid(该小程序 appid 必须与发模板消息的公众号是绑定关联关系,暂不支持小游戏) /// </summary> public string appid { get; set; } /// <summary> /// 所需跳转到小程序的具体页面路径,支持带参数,(示例index? foo = bar),要求该小程序已发布,暂不支持小游戏 /// </summary> public string pagepath { get; set; } }
3、在测试调试的时候,需要把本机ip添加到白名单才能发送消息
记录、测试使用公众号消息推送