1. 程式人生 > >使用.net 操作 微信公眾平臺 —— 回覆使用者訊息 —— 回覆圖文訊息

使用.net 操作 微信公眾平臺 —— 回覆使用者訊息 —— 回覆圖文訊息

目錄

  1. 使用.net 操作 微信公眾平臺 —— 接入

  2. 使用.net 操作 微信公眾平臺 —— 生成微信選單

  3. 使用.net 操作 微信公眾平臺 —— 接收並回複用戶訊息

    3.1 使用.net 操作 微信公眾平臺 —— 接收使用者操作 —— 詳細解析

        3.1.1 使用.net 操作 微信公眾平臺 —— 接收使用者操作 —— 關注/取消關注 公眾號

        3.1.2 

使用.net 操作 微信公眾平臺 —— 接收使用者操作 —— 接收使用者傳送的訊息

    3.2 使用.net 操作 微信公眾平臺 —— 回覆使用者訊息

        3.2.1 使用.net 操作 微信公眾平臺 —— 回覆使用者訊息 —— 回覆文字訊息

        3.2.2 使用.net 操作 微信公眾平臺 —— 回覆使用者訊息 —— 回覆圖片訊息

        3.2.3 使用.net 操作 微信公眾平臺 —— 回覆使用者訊息 —— 回覆圖文訊息

  4. 使用.net 操作 微信公眾平臺 —— 第三方登入


 工具


回覆圖文訊息

<xml>
    <ToUserName>< ![CDATA[toUser]] ></ToUserName>
    <FromUserName>< ![CDATA[fromUser]] ></FromUserName>
    <CreateTime>12345678</CreateTime>
    <MsgType>< ![CDATA[news]] ></MsgType>
    <ArticleCount> 2 </ArticleCount>
    <Articles>
        <item>
            <Title>< ![CDATA[title1]] ></Title> 
            <Description>< ![CDATA[description1]] ></Description>
            <PicUrl>< ![CDATA[picurl1]] ></PicUrl>
            <Url>< ![CDATA[url1]] ></Url>
        </item>

        <item>
            <Title>< ![CDATA[title2]] ></Title> 
            <Description>< ![CDATA[description2]] ></Description>
            <PicUrl>< ![CDATA[picurl2]] ></PicUrl>
            <Url>< ![CDATA[url2]] ></Url>
        </item>
    </Articles>
</xml>
引數 是否必須 描述
ToUserName 接收方帳號(收到的OpenID)
FromUserName 開發者微訊號
CreateTime 訊息建立時間 (整型)
MsgType news
ArticleCount 圖文訊息個數,限制為10條以內
Articles 多條圖文訊息資訊,預設第一個item為大圖,注意,如果圖文數超過10,則將會無響應
Title 圖文訊息標題
Description 圖文訊息描述
PicUrl 圖片連結,支援JPG、PNG格式,較好的效果為大圖360*200,小圖200*200
Url 點選圖文訊息跳轉連結

 

被動回覆

/// <summary>
/// 傳送圖片訊息 - 被動訊息
/// </summary>
/// <returns></returns>
private void SendImage()
{
    int nowtime = ConvertDateTimeInt(DateTime.Now); // 檢視工具 DateTime轉為微信所需要的時間型別
    string resxml = "<xml>"
    resxml += "    <ToUserName><![CDATA[" + xmlMsg.FromUserName + "]]></ToUserName>"
    resxml += "    <FromUserName><![CDATA[" + xmlMsg.ToUserName + "]]></FromUserName>"
    resxml += "    <CreateTime>" + nowtime + "</CreateTime>"
    resxml += "    <MsgType><![CDATA[news]]></MsgType>"
    resxml += "    <Articles>"

    resxml += "    <item>"
    resxml += "        <Title>< ![CDATA[" + title1 + "] ]></Title>"
    resxml += "        <Description>< ![CDATA[" + description1 + "] ]></Description>"
    resxml += "        <PicUrl>< ![CDATA[" + picurl1 + "] ]></PicUrl>"
    resxml += "        <Url>< ![CDATA[" + url1 + "] ]></Url>"
    resxml += "    </item>"

    resxml += "    <item>"
    resxml += "        <Title>< ![CDATA[" + title2 + "] ]></Title>"
    resxml += "        <Description>< ![CDATA[" + description2 + "] ]></Description>"
    resxml += "        <PicUrl>< ![CDATA[" + picurl2 + "] ]></PicUrl>"
    resxml += "        <Url>< ![CDATA[" + url2 + "] ]></Url>"
    resxml += "    </item>"

    resxml += "    </Articles>"
    resxml += "</xml>";
    Response.Write(resxml);
    Response.End();
}

客服訊息

/// <summary>
/// 傳送圖文訊息 - 客服訊息
/// </summary>
/// <param name="token">工具 生成AccessToken</param>
/// <returns></returns>
private void SendImageCase(string token)
{
    string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", token);

    List<newsViewModel> model = new List<newsViewModel>();
    model.Add(new newsViewModel
    {
        title = "標題1",
        description = "描述1",
        url = "地址1",
        picurl = "圖片1",
    });

    model.Add(new newsViewModel
    {
        title = "標題2",
        description = "",
        url = "",
        picurl = "圖片2",
    });

    JObject data = new JObject();
    data.Add("touser", xmlMsg.FromUserName);
    data.Add("msgtype", "news");
    data.Add("news", JObject.FromObject(new
    {
        articles = model
    }));

    var res = HttpPost(data.ToString(), url);
}

/// <summary>
/// 圖文訊息模型
/// </summary>
public class newsViewModel
{
    /// <summary>
    /// 標題
    /// </summary>
    public string title { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string description { get; set; }

    /// <summary>
    /// 連結地址
    /// </summary>
    public string url { get; set; }

    /// <summary>
    /// 圖片地址
    /// </summary>
    public string picurl { get; set; }
}

/// <summary>
/// 遠端獲取資料(POST)
/// </summary>
/// <param name="param"></param>
/// <param name="url"></param>
/// <returns></returns>
private dynamic HttpPost(string param, string url)
{
    //轉換輸入引數的編碼型別,獲取bytep[]陣列 
    byte[] byteArray = Encoding.UTF8.GetBytes(param);
    //初始化新的webRequst
    //1. 建立httpWebRequest物件
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
    //2. 初始化HttpWebRequest物件
    webRequest.Method = "POST";
    webRequest.ContentType = "application/json";
    webRequest.ContentLength = byteArray.Length;
    Stream newStream = webRequest.GetRequestStream();
    newStream.Write(byteArray, 0, byteArray.Length);
    newStream.Close();
    //4. 讀取伺服器的返回資訊
    HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string srend = sr.ReadToEnd();
    sr.Close();
    response.Close();
    return srend;
}