微信公眾平台開發者模式自動回復功能與自定義菜單


微信開發者模式自動回復功能

1.提取POST來的數據中的信息

2.判斷微信客戶端發來的信息內容(以文本信息為例)

3.根據信息內容進行處理

4.把處理結果以XML的形式返回給微信服務器

5.微信服務器反饋內容給微信客戶端

 

 

1.獲取用戶發來的文本信息的內容

private void ExtractPostData() /*** 提取POST來的數據中的信息 ***/
{
Stream s = HttpContext.Current.Request.InputStream; //讀取post來的數據流
byte[] b = new byte[s.Length]; //定義一個和post來的數據流長度一致的空數組
s.Read(b, 0, (int)s.Length); //把這個數據流存入數組
string postStr = Encoding.UTF8.GetString(b); //把數組轉換成字符串
XmlDocument doc = new XmlDocument(); //System.Xml中的類,用於對XML文檔操作
doc.LoadXml(postStr); //加載字符串(文本)
XmlNodeList list = doc.GetElementsByTagName("xml"); //返回標簽名為“xml”的所有節點,返回的是數組
XmlNode xn = list[0]; //獲得第一個標簽名為“xml”的節點,注意類為XmlNode,這里實際是獲得“根”
ToUserName = xn.SelectSingleNode("//FromUserName").InnerText; //在“根”下找標簽為“FromUserName”的節點,並靠.InnerText獲得這個標簽里面的文本
MsgType = xn.SelectSingleNode("//MsgType").InnerText; //獲得用戶發來的信息是什么類型:text,image,location,link,event等
if (MsgType == "text")
{
Content = xn.SelectSingleNode("//Content").InnerText;//獲取用戶發來的文本消息的內容
}
}

  

 

2.對接收到的文本信息內容進行處理

string ReMess = "";//用來存儲回復給用戶的內容

private string ToUserName = null; //用戶賬號

private string MsgType = null; //接收到的信息類型

 

private string Content = null; //回復給用戶的內容

if (MsgType == "text")//如果用戶發來的是文本信息
{
if (Content == "ABC" || Content == "abc")//Content就是用戶發給公眾號的信息
{
ReMess = "123";//回復給用戶的消息 
}
else
{
ReMess = "你好,我的功能還很有限,只是測試程序。\n其他服務正在建設中...";//回復給用戶的消息 
}
//ReMess = this.ResText(ToUserName, ReMess);//這里ReMess兼做存儲XML格式字符串
}
responseMsg(ReMess);

  

 

3.把處理結果以XML的形式返回給微信服務器

private void responseMsg(string Content)
{
string strresponse = "<xml>";
strresponse = strresponse + "<ToUserName><![CDATA[" + ToUserName + "]]></ToUserName>";//回復給誰
strresponse = strresponse + "<FromUserName><![CDATA[" + FromUserName + "]]></FromUserName>";//是開發者回復
strresponse = strresponse + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";//消息創建時間
strresponse = strresponse + "<MsgType><![CDATA[text]]></MsgType>";//消息類型為“文本”
strresponse = strresponse + "<Content><![CDATA[" + Content + "]]></Content>";//回復的消息內容
strresponse = strresponse + "<FuncFlag>0<FuncFlag>";
strresponse = strresponse + "</xml>";
Response.Write(strresponse);
Response.End();
}

  

—————————————————————————————————————————————————————————————————————————————

微信開發者模式自定義菜單

1.創建自定義菜單

private void CreateMenu() 
{
string weixin1 = "";
weixin1 = @" { 
""button"":[ 
{ 
""type"":""click"", 
""name"":""你好!"", 
""key"":""Hello"" 
}, 
{ 
""type"":""view"", 
""name"":""公司簡介"", 
""url"":""http://www.baidu.com" 
}, 
{ 
""name"":""產品介紹"", 
""sub_button"":[ 
{ 
""type"":""click"", 
""name"":""產品1"", 
""key"":""P1"" 
}, 
{ 
""type"":""click"", 
""name"":""產品2"", 
""key"":""P2"" 
}] 
}] 
} 
";

string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + "    填入access_token", weixin1);
Response.Write(i);
}

  

 

 

2.翻譯自定義菜單

public string GetPage(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准備請求... 
try
{
// 設置參數 
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//發送請求並獲取相應回應數據 
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才開始向目標網頁發送Post請求 
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回結果網頁(html)代碼 
string content = sr.ReadToEnd();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
string err = ex.Message;
Response.Write(err);
return string.Empty;
}
}

  

 

 

總結一下,今天收獲還是蠻大的

1.搞定了自動回復功能。

2.創建了簡單的自定義菜單。

3.知道了發布后的*.dll文件要放在服務器根目錄的bin文件夾里面。

4.學會了斷點調試。

 

何總下午開了個會,對微信開發小組接下來的工作提出了具體的要求,要求在規定時間內做出一個簡單的BBS網站並放在菜單欄中使用,還有實現定位打卡功能。身為小組組長,瞬間感到壓力挺大,不過有壓力才會有動力,先把時間和工作安排好,一步一步來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM