当前位置:网站首页>. 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 }
边栏推荐
- Bom Dom
- drools执行完某个规则后终止别的规则执行
- Writing method of then part in drools
- spfa AcWing 851. 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
- JDBC 预防sql注入问题与解决方法[PreparedStatement]
- Go learning notes - go based interprocess communication
- CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
- Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
猜你喜欢
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Is the neural network (pinn) with embedded physical knowledge a pit?
Bom Dom
CDA data analysis -- Introduction and use of aarrr growth model
Go learning notes - multithreading
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
Writing method of then part in drools
drools中then部分的写法
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
随机推荐
Calculate the maximum path sum of binary tree
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
arcgis js 4. Add pictures to x map
Use sqoop to export ads layer data to MySQL
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
LeetCode—<动态规划专项>剑指 Offer 19、49、60
drools执行完某个规则后终止别的规则执行
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
(C language) octal conversion decimal
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
Redis avalanche, penetration, breakdown
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
Performance tuning project case
BOM DOM
Distributed machine learning framework and high-dimensional real-time recommendation system
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
[FFH] little bear driver calling process (take calling LED light driver as an example)
SparkContext: Error initializing SparkContext解决方法
Leetcode - Sword finger offer 59 - I, 59 - II