当前位置:网站首页>. 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
- Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
- 1380. Lucky numbers in the matrix [two-dimensional array, matrix]
- spfa AcWing 851. spfa求最短路
- JSON序列化 与 解析
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- Adding database driver to sqoop of cdh6
- Sweetheart leader: Wang Xinling
- 分布式机器学习框架与高维实时推荐系统
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
猜你喜欢
随机推荐
区间DP AcWing 282. 石子合并
Is the neural network (pinn) with embedded physical knowledge a pit?
Introduction to CPU instruction set
分布式机器学习框架与高维实时推荐系统
WSL 2 will not be installed yet? It's enough to read this article
怎样写一篇赏心悦目的英文数学论文
ArrayList与LinkedList效率的对比
记录一下MySql update会锁定哪些范围的数据
堆 AcWing 838. 堆排序
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
CPU指令集介绍
计数类DP AcWing 900. 整数划分
[ybtoj advanced training guidance] cross the river [BFS]
Leetcode - Sword finger offer 59 - I, 59 - II
arcgis js 4. Add pictures to x map
2.6 using recursion and stack - [tower of Hanoi problem]
String palindrome hash template question o (1) judge whether the string is palindrome
[ybtoj advanced training guide] similar string [string] [simulation]
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
BOM DOM
![2.7 binary tree, post order traversal - [FBI tree]](/img/6b/1ded3632cc69329d7b2762ce47fdbc.jpg)








