当前位置:网站首页>. 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
}边栏推荐
- 寻找二叉树中任意两个数的公共祖先
- Rust search server, rust quick service finding tutorial
- Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
- Drools executes string rules or executes a rule file
- Day12 control flow if switch while do While guessing numbers game
- SparkContext: Error initializing SparkContext解决方法
- This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
- LeetCode—剑指 Offer 51. 数组中的逆序对
- Docker-compose配置Mysql,Redis,MongoDB
- Lekao: 22 year first-class fire engineer "technical practice" knowledge points
猜你喜欢

AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning

"As a junior college student, I found out how difficult it is to counter attack after graduation."

堆(優先級隊列)

Lekao.com: experience sharing of junior economists and previous candidates in customs clearance

Adding database driver to sqoop of cdh6

线性DP AcWing 896. 最长上升子序列 II
![2.7 binary tree, post order traversal - [FBI tree]](/img/6b/1ded3632cc69329d7b2762ce47fdbc.jpg)
2.7 binary tree, post order traversal - [FBI tree]

Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer

This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry

arcgis js 4.x 地图中加入图片
随机推荐
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
Redis introduction, scenario and data type
接口测试面试题目,你都会了吗?
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
arcgis js 4.x 地图中加入图片
drools决策表的简单使用
BOM DOM
Intel internal instructions - AVX and avx2 learning notes
哈希表 AcWing 841. 字符串哈希
Is the neural network (pinn) with embedded physical knowledge a pit?
spfa AcWing 852. spfa判断负环
Docker compose configuration mysql, redis, mongodb
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
Performance tuning project case
趣味 面试题
模块化 CommonJS ES Module
High performance erasure code coding
What data types does redis have and their application scenarios
Tas (file d'attente prioritaire)