当前位置:网站首页>. 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
}边栏推荐
- Drools executes string rules or executes a rule file
- drools执行String规则或执行某个规则文件
- [FFH] little bear driver calling process (take calling LED light driver as an example)
- Lombok common annotations
- js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
- 线性DP AcWing 897. 最长公共子序列
- Sweetheart leader: Wang Xinling
- Anti shake throttle
- Sub thread get request
- Redis avalanche, penetration, breakdown
猜你喜欢

ThreadLocal的简单理解

Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold

spfa AcWing 851. spfa求最短路

高性能纠删码编码

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

Embedded Software Engineer career planning

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

CDH6之Sqoop添加数据库驱动

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

Openssh remote enumeration username vulnerability (cve-2018-15473)
随机推荐
模块化 CommonJS ES Module
drools决策表的简单使用
记录一下MySql update会锁定哪些范围的数据
Simple understanding of ThreadLocal
Record the range of data that MySQL update will lock
ThreadLocal的简单理解
Simple use of drools decision table
使用Sqoop把ADS层数据导出到MySQL
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
arcgis js 4. Add pictures to x map
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Multiply LCA (nearest common ancestor)
How to write a pleasing English mathematical paper
Sse/avx instruction set and API of SIMD
分布式机器学习框架与高维实时推荐系统
WSL 2 will not be installed yet? It's enough to read this article
Mongodb redis differences
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 sentinel mechanism and configuration
Rust search server, rust quick service finding tutorial