当前位置:网站首页>. 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

Sort---

Go学习笔记—多线程

Record the range of data that MySQL update will lock

区间DP AcWing 282. 石子合并

Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand

使用Sqoop把ADS层数据导出到MySQL

记录一下MySql update会锁定哪些范围的数据

AI中台技术调研

Distributed machine learning framework and high-dimensional real-time recommendation system
随机推荐
Enhance network security of kubernetes with cilium
区间DP AcWing 282. 石子合并
Shutter encapsulated button
Intel internal instructions - AVX and avx2 learning notes
BOM DOM
Docker compose configuration mysql, redis, mongodb
drools执行String规则或执行某个规则文件
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
BOM DOM
Redis sentinel mechanism and configuration
drools执行完某个规则后终止别的规则执行
Drools executes the specified rule
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
堆 AcWing 838. 堆排序
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
arcgis js 4. Add pictures to x map
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
[I'm a mound pytorch tutorial] learning notes
Simple understanding of ThreadLocal
Sse/avx instruction set and API of SIMD