.NET微信公众号选择模板给粉丝推送消息
模板是只能选择微信官方的,不能自定义
#region 微信公众号推送消息
//官方文档:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
private void WeChatMsg(int id)
{
if (id>0)
{
string resultVal = "默认返回内容";
try
{
BLL.MyPlan myplanBLL = new BLL.MyPlan();
Model.MyPlan dtyuyue = myplanBLL.GetModel(id);//
string strnr = dtyuyue.Plans.ToString();//要改变的内容,要推送的
string yanzCode = SmsReader.BuildVerCode();
Session["GetCode"] = yanzCode;
string OpenId = "";
string sqlStr = @" SELECT 列 from 表 + " ";//查出要发送的粉丝的OPENID
DataTable dt = MyWeb.DAL.DbHelperSQL.ExecuteDataTable(sqlStr);
if (dt == null || dt.Rows.Count <= 0)
{
throw new Exception("抱歉,您的账号还未绑定微信公众号");
}
OpenId = dt.Rows[0][0].ToString();//关注公众号的用户的微信OPENID
//template_id是公众号用的模板ID。是公众号后台选择的微信提供的模板,按行业选择
string json = "{\"touser\":\"" + OpenId + "\",\"template_id\":\"nRlWu2Lf-hMS8cXfDGIFpzUxDOxuPHx4pGTXlQtzvEg\",\"url\":\"\",\"topcolor\":\"#FF0000\",\"data\":{\"first\":{\"value\":\"这里是标题\",\"color\":\"#173177\"},\"keyword1\":{\"value\":\"" + 这里是正文内容+ "\"},\"keyword2\":{\"value\":\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\"},\"remark\":{\"value\":\"这里是备注!\"}}}";
//string json = "{\"touser\":\"" + OpenId + "\",\"template_id\":\"nRlWu2Lf-hMS8cXfDGIFpzUxDOxuPHx4pGTXlQtzvEg\",\"topcolor\":\"#FF0000\",\"data\":{\"first\":{\"value\":\"这里是标题\",\"color\":\"#173177\"},\"keyword1\":{\"value\":\"" + 这里是正文内容 + "\"},\"keyword2\":{\"value\":\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\"},\"remark\":{\"value\":\"这里是备注!\"}}}";
//string tocken = GetWeiXinAccessToken(WXmpAppID, WXmpAppSecret);
WxMPConfigInfo wxMPConfigInfo = GeWxAccessToken();
string tocken = wxMPConfigInfo.Access_token;//获取微信公众号token
string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + tocken;
string msgVal = HttpPostMP(url, json);
AddLgoToTXT("【" + json + ", yanzCode:" + yanzCode + ",sqlStr:" + sqlStr + "】" + msgVal + "");//这句是写日志用的
}
catch (Exception ex)
{
resultVal = ex.Message;
}
}
}
private void AddLgoToTXT(string msg)
{
try
{
string path = AppDomain.CurrentDomain.BaseDirectory + "operalog.txt";
if (!System.IO.File.Exists(path))
{
FileStream stream = System.IO.File.Create(path);
stream.Close();
stream.Dispose();
}
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(msg);
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 获取微信公众号access_token
/// </summary>
/// <returns></returns>
public static WxMPConfigInfo GeWxAccessToken()
{
string sqlStr = @"select 列 from 列";//查询或者获取公众号的Token
DataTable dt = MyWeb.DAL.DbHelperSQL.ExecuteDataTable(sqlStr);
List<WxMPConfigInfo> tempList = ExtendMethod.ToDataList<WxMPConfigInfo>(dt);
WxMPConfigInfo tempWxMPConfigInfo = null;
if (tempList != null && tempList.Count > 0)
{
tempWxMPConfigInfo = tempList.First();
}
return tempWxMPConfigInfo;
}
/// <summary>
/// 微信公众号配置表
/// </summary>
public class WxMPConfigInfo
{
public string WeiXinOpenId { get; set; }
public string WeixinAvatar { set; get; }
public string AppId { get; set; }
public string Appsecret { get; set; }
public string Access_token { get; set; }
public string Token_Time { get; set; }
}
public static string HttpPostMP(string url, string param)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.Timeout = 15000;
request.AllowAutoRedirect = false;
StreamWriter requestStream = null;
WebResponse response = null;
string responseStr = null;
try
{
requestStream = new StreamWriter(request.GetRequestStream());
requestStream.Write(param);
requestStream.Close();
response = request.GetResponse();
if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseStr = reader.ReadToEnd();
reader.Close();
}
}
catch (Exception)
{
throw;
}
finally
{
request = null;
requestStream = null;
response = null;
}
return responseStr;
}
#endregion

浙公网安备 33010602011771号