当前位置:网站首页>. Net wechat message template push
. Net wechat message template push
2022-07-02 12:39:00 【Time chaser】
Preface :
Recently, wechat message template push function has been used in the project , That is to push the corresponding message to the corresponding user wechat , The premise is that you must have a wechat official account and pay to have this function , In addition, users who want to push must pay attention to your wechat official account .
This process is like this. First, users pay attention to your wechat official account , Get the corresponding user's oppenid, Then you can use the corresponding user oppenid Select the message template corresponding to the discount and push the message to the user .
Realization :
Add function plug-in :
Find template message :
Select the message template corresponding to the discount :
Parameter description :
Parameters | If required | explain |
---|---|---|
touser | yes | The receiver openid |
template_id | yes | Templates ID |
url | no | Template jump link ( Overseas account no jump ability ) |
miniprogram | no | Data required for skipping applets , No need to skip the applet to transfer the data |
appid | yes | Applet to jump to appid( This applet appid It must be bound to the official account of the template message. , Small games are not supported temporarily ) |
pagepath | no | The specific page path to jump to the applet , Support with parameters ,( Example index?foo=bar), Require the applet to be published , Small games are not supported temporarily |
data | yes | Template data |
color | no | Template content font color , Default to black if not filled |
Code implementation :
using System; using System.IO; using System.Net; using System.Text; using Newtonsoft.Json; namespace JJHL.Service { /// <summary> /// Wechat message push /// </summary> public class WxChatPrompt { public WxChatPrompt() { } private static WxChatPrompt _objPrompt; public static WxChatPrompt _ { get => _objPrompt ?? new WxChatPrompt(); set => _objPrompt = value; } /// <summary> /// Message push /// </summary> /// <param name="Access_token"> Web authorization certificate , Get through wechat interface </param> /// <param name="Openid"> Users to push oppenid</param> /// <returns></returns> public string MsgPush(string Access_token, string Openid) { string templateId = "";// Template number string firstContent= "";// Content string keyword1 = "";// Custom content string keyword2 = "";// Custom content string keyword3 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // Time string remark = "";// remarks string contentmsg = "{\"touser\":\"" + Openid + "\",\"template_id\":\"" + templateId + "\",\"topcolor\":\"#FF0000\",\"data\":{\"first\":{\"value\":\"" + firstContent + "\",\"color\":\"#173177\"},\"keyword1\":{\"value\":\"" + keyword1 + "\",\"color\":\"#173177\"},\"keyword2\":{\"value\":\"" + keyword2 + "\",\"color\":\"#173177\"},\"keyword3\":{\"value\":\"" + keyword3 + "\",\"color\":\"#173177\"},\"remark\":{\"value\":\"" +remark + "\",\"color\":\"#173177\"}}}"; string result = WeChatPushNotice(Access_token, contentmsg); return result; } /// <summary> /// Wechat message push /// </summary> /// <param name="accessToken"> WeChat access_token</param> /// <param name="contentMsg"> Push content </param> /// <returns></returns> public string WeChatPushNotice(string accessToken, string contentMsg) { string promat = ""; // Data to be submitted byte[] bs = Encoding.UTF8.GetBytes(contentMsg); HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken + ""); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } HttpWebResponse respon = (HttpWebResponse)req.GetResponse(); Stream stream = respon.GetResponseStream(); using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { promat = reader.ReadToEnd(); } ReturnMsg y = JsonConvert.DeserializeObject<ReturnMsg>(promat); promat = y.errmsg; return promat; } /// <summary> /// Custom model /// </summary> private class ReturnMsg { public string errcode { get; set; } public string errmsg { get; set; } public string msgid { get; set; } } } }
After calling the template message interface , Returns the JSON Data packets . Normal return JSON Packet example :
{ "errcode":0, "errmsg":"ok", "msgid":200228332 }
边栏推荐
- Shutter encapsulated button
- CDH6之Sqoop添加数据库驱动
- What data types does redis have and their application scenarios
- JDBC 预防sql注入问题与解决方法[PreparedStatement]
- IPhone 6 plus is listed in Apple's "retro products" list
- 软件测试面试题-2022年大厂面试题合集
- In development, why do you find someone who is paid more than you but doesn't write any code?
- spfa AcWing 852. spfa判断负环
- The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
- Simple understanding of ThreadLocal
猜你喜欢
趣味 面试题
Find the common ancestor of any two numbers in a binary tree
Docker-compose配置Mysql,Redis,MongoDB
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
MySQL indexes and transactions
哈希表 AcWing 840. 模拟散列表
Floyd AcWing 854. Floyd求最短路
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
async/await 异步函数
bellman-ford AcWing 853. 有边数限制的最短路
随机推荐
Go学习笔记—基于Go的进程间通信
单指令多数据SIMD的SSE/AVX指令集和API
浏览器存储方案
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
Fluent fluent library encapsulation
CDA data analysis -- Introduction and use of aarrr growth model
2.6 using recursion and stack - [tower of Hanoi problem]
Calculate the maximum path sum of binary tree
Redis introduction, scenario and data type
The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
[C language] convert decimal numbers to binary numbers
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
Drools dynamically add, modify, and delete rules
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
防抖 节流
高性能纠删码编码
Fastdateformat why thread safe
Intel 内部指令 --- AVX和AVX2学习笔记