bob体育官方平台
bob体育官方平台微信开发 消息推送实现代码_javascript技巧_脚本之家
public static string IsExistAccess_Token(){string Token = string.Empty;DateTime YouXRQ;string strPath = "../../weixin/XMLFile.xml";// 读取XML文件中的数据,并显示出来//string filepath = System.Web.Hosting.HostingEnvironment.MapPath;string filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);XmlDocument xml = new XmlDocument;str.Close;Token = xml.SelectSingleNode.SelectSingleNode.InnerText;YouXRQ = Convert.ToDateTime(xml.SelectSingleNode.SelectSingleNode.InnerText); if (DateTime.Now > YouXRQ){DateTime _youxrq = DateTime.Now;Access_token mode = GetAccess_token();xml.SelectSingleNode.SelectSingleNode.InnerText = mode.access_token;_youxrq = _youxrq.AddSeconds(int.Parse;xml.SelectSingleNode.SelectSingleNode.InnerText = _youxrq.ToString;Token = mode.access_token;} object text = new{toparty = "1",agentid = "2",msgtype = "text",text = new{content = "项目名称:"+来保网+""}}; string wcr= btnSend;return wcr; }public static string btnSend(string Token, object text){ string url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + Token;WebRequest req = WebRequest.Create;JavaScriptSerializer aa = new JavaScriptSerializer();string postData = aa.Serialize;byte[] requestBytes = Encoding.UTF8.GetBytes;req.Method = "POST";req.ContentType = "application/x-www-form-urlencoded";req.ContentLength = requestBytes.Length; Stream requestStream = req.GetRequestStream();requestStream.Write(requestBytes, 0, requestBytes.Length);requestStream.Close();HttpWebResponse res = req.GetResponse();StreamReader sr = new StreamReader(res.GetResponseStream;string backstr = sr.ReadToEnd;res.Close();WeChatReturn WCR = aa.Deserialize;return WCR.errmsg; }

 

根据当前日期 判断Access_Token 是否超期 如果超期返回新的Access_Token 否则返回之前的Access_Token

res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");
}
else if (wx.MsgType == "voice")
{
res = sendTextMessage(wx, wx.Recognition);
}
else if (wx.MsgType == "location")
{
res = sendTextMessage(wx, "您发送的位置是:" + wx.Label + ";纬度是:" + wx.Location_X + ";经度是:" + wx.Location_Y + ";缩放比例为:" + wx.Scale);
}
else
{
res = sendTextMessage(wx, "你好,未能识别消息!");
}
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

 

//定义数据模型 public class Access_token{public Access_token(){//// TODO: 在此处添加构造函数逻辑//}string _access_token;string _expires_in; /// /// 获取到的凭证/// public string access_token{get { return _access_token; }set { _access_token = value; }} /// /// 凭证有效时间,单位:秒/// public string expires_in{get { return _expires_in; } set { _expires_in = value; }} }

 public ActionResult index{ GetAccess_token(); IsExistAccess_Token;} public static Access_token GetAccess_token(){string AppUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?";string AppID = "应用组的CorpID";//在设置-》权限管理-》系统管理组string AppSecret = "应用组的Secret";//在设置-》权限管理-》系统管理组WebClient webClient = new WebClient();Byte[] bytes = webClient.DownloadData(string.Format("{0}corpid={1}&corpsecret={2}", AppUrl, AppID, AppSecret));string result = Encoding.GetEncoding.GetString;JObject jObj = JObject.Parse; string token = jObj["access_token"].ToString();string expires_in = jObj["expires_in"].ToString(); Access_token mode = new Access_token();mode.access_token = token;mode.expires_in = expires_in;return mode;} /// 

string MyOpenID;
string MyContent;
protected void Page_Load(object sender, EventArgs e)
{
MyOpenID = "oV93gjl5slD3p29yS1dOijy-pqZ8";
MyContent = "这是一个客服消息";
UserName.Text = MyOpenID;
Content.Text = MyContent;
}
protected void Button1_Click(object sender, EventArgs e)
{
string res = "";
string Access_Token = IsExistAccess_Token();

最近做微信公共号的开发,有个需求是这样的消息推送,以文本的形式把编辑的消息发送给微信企业号中的某一个应用组,这里做下笔记,以下是整理内容:

WriteLog的方法是这样的:

public static Access_token GetAccess_token()
{
string appid = "wx043225275885dafd";
string secret = "cb4425b24ab79ef875029cf0bf326ae9";
string strUrl = ""

protected void Page_Load(object sender, EventArgs e)
{
wxmessage wx = GetWxMessage();
WriteLog(wx.FromUserName);
string res = "";

 

 

       下面的问题就是如何手机客户的“手机号”了,微信里面有一个OPENID,这就使手机号了,这个是关注你的用户的唯一标识,这个标识是不会变的,但是同一个用户关注不同的微信号,那么他在不同微信号中的ID是不一样的。如何取得OPENID呢?

if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")
{
string content = "";
if (!wx.EventKey.Contains("qrscene_"))
{
content = "/:rose欢迎北京永杰友信科技有限公司/:rosen直接回复“你好”";
res = sendTextMessage(wx, content);
}
else
{
content = "二维码参数:n" + wx.EventKey.Replace("qrscene_", "");
res = sendTextMessage(wx, content);
}
}

StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
XmlDocument xml = new XmlDocument();
xml.Load(str);
str.Close();
str.Dispose();
Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);

 

string Token = string.Empty;
DateTime YouXRQ;
// 读取XML文件中的数据,并显示出来 ,注意文件路径
string filepath = HttpContext.Current.Server.MapPath("XMLFile.xml");

StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

       说道这里可能有人会问,那不是群发吗?微信不是每天只能发一条吗?服务号每个月发一条吗?答案很明确微信说的是“客服消息”,也不是群发,它跟群发的区别就在于只要关注了我,那么我就可以群发给你消息,而为了不让用户感到讨厌,微信作了严格的群发条数限制。而“客服消息”则是只有关注你的人向你发送了消息,你才能主动推送。就像打电话只有人家问过你问题了,你才能回答,并且继续跟进回答。再举一个例子:你通过被人知道了我的手机号,存下来了,你突然给我打电话,我肯定是抵触的。但是如果我给你打过电话,你通过来电显示知道了我的号码,以后再给我打电话,说明我们是认识的,我最少不会抵触。这就使群发/被动响应/客服消息的本质区别。

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl);

       首先记录用户的OPENID在OnLoad函数第二行里面加上一个 WriteLog(wx.FromUserName);把它记录在一个文本文件中

req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();

 

       就像我们可以通过收集电话号码,手机自己的客户信息一样,一旦他们的信息被我们收录。那么我们就可以突破群发条数的限制了,按照微信的限制群发一个月只能一条(服务号),但是“客服消息”则可以一天50000条,足够用了。

返回顶部