溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信公眾平臺開發(fā)中如何使用.Net代碼發(fā)送文本消息

發(fā)布時間:2021-09-16 16:54:17 來源:億速云 閱讀:128 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“微信公眾平臺開發(fā)中如何使用.Net代碼發(fā)送文本消息”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“微信公眾平臺開發(fā)中如何使用.Net代碼發(fā)送文本消息”這篇文章吧。

具體內(nèi)容如下

首先建立一個微信消息類。 

 class wxmessage 
 { 
  public string FromUserName { get; set; } 
  public string ToUserName { get; set; } 
  public string MsgType { get; set; } 
  public string EventName { get; set; } 
  public string Content { get; set; }
  public string EventKey { get; set; } 
 }

 后臺代碼如下: 

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

   if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")
   {//剛關(guān)注時的時間,用于歡迎詞
    string content = "";
    content = "/:rose歡迎北京永杰友信科技有限公司/:rose\n直接回復(fù)“你好”";
    res = sendTextMessage(wx, content);
   }
   else
   {
    if (wx.MsgType == "text" && wx.Content == "你好")
    {
     res = sendTextMessage(wx, "你好,歡迎使用北京永杰友信科技有限公司公共微信平臺!");
    }
    else
    {
     res = sendTextMessage(wx, "你好,未能識別消息!");
    }
   }

   Response.Write(res);
  }

 private wxmessage GetWxMessage()
  {
   wxmessage wx = new wxmessage();
   StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
   XmlDocument xml = new XmlDocument();
   xml.Load(str);
   wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
   wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
   wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
   if (wx.MsgType.Trim() == "text")
   {
    wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
   }
   if (wx.MsgType.Trim() == "event")
   {
    wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
   }

   
   return wx;
  }

/// 
  /// 發(fā)送文字消息 
  /// 
  /// 獲取的收發(fā)者信息 
  /// 內(nèi)容 
  /// 
  private string sendTextMessage(wxmessage wx, string content)
  {
   string res = string.Format(@" ",
    wx.FromUserName, wx.ToUserName, DateTime.Now, content);
   return res;
  }

以上是“微信公眾平臺開發(fā)中如何使用.Net代碼發(fā)送文本消息”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI