예제 #1
0
 /// <summary>
 /// 回复单图文消息
 /// </summary>
 /// <param name="toUserName"></param>
 /// <param name="fromUserName"></param>
 /// <param name="news"></param>
 /// <returns></returns>
 public static string RepayNews(string toUserName, string fromUserName, WeixinNews news)
 {
     var builder = new StringBuilder();
     builder.Append(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName>" +
     "<FromUserName><![CDATA[{1}]]></FromUserName>" +
     "<CreateTime>{2}</CreateTime>" +
     "<MsgType><![CDATA[news]]></MsgType>" +
     "<ArticleCount>{3}</ArticleCount><Articles>",
      toUserName, fromUserName,
      Util.CreateTimestamp(),
     1));
     builder.Append(string.Format("<item><Title><![CDATA[{0}]]></Title>" +
         "<Description><![CDATA[{1}]]></Description>" +
         "<PicUrl><![CDATA[{2}]]></PicUrl>" +
         "<Url><![CDATA[{3}]]></Url>" +
         "</item>",
        news.title, news.description, news.picurl, news.url
      ));
     builder.Append("</Articles></xml>");
     return builder.ToString();
 }
예제 #2
0
 /// <summary>
 /// 回复单图文消息
 /// </summary>
 /// <param name="access_token">调用接口凭证</param>
 /// <param name="touser">普通用户openid</param>
 /// <param name="news"></param>
 /// <param name="kf_account">完整客服账号,格式为:账号前缀@公众号微信号</param>
 /// <returns></returns>
 public static bool RepayNews(string access_token, string touser, WeixinNews news, string kf_account = null)
 {
     var builder = new StringBuilder();
     builder.Append("{")
         .Append('"' + "touser" + '"' + ":").Append('"' + touser + '"').Append(",")
         .Append('"' + "msgtype" + '"' + ":").Append('"' + "news" + '"').Append(",")
         .Append('"' + "news" + '"' + ":")
         .Append("{")
            .Append('"' + "articles" + '"' + ":")
              .Append("[")
              .Append("{")
              .Append('"' + "title" + '"' + ":").Append('"' + news.title + '"').Append(",")
              .Append('"' + "description" + '"' + ":").Append('"' + news.description + '"').Append(",")
              .Append('"' + "url" + '"' + ":").Append('"' + news.url + '"').Append(",")
              .Append('"' + "picurl" + '"' + ":").Append('"' + news.picurl + '"')
              .Append("}")
            .Append("]")
         .Append("}");
     if (!string.IsNullOrEmpty(kf_account))
     {
         builder.Append(",");
         builder.Append('"' + "customservice" + '"' + ":")
                .Append("{")
                .Append('"' + "kfaccount" + '"' + ":").Append('"' + kf_account + '"')
                .Append("}");
     }
     builder.Append("}");
     var client = new HttpClient();
     return client.PostAsync(string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", access_token), new StringContent(builder.ToString())).Result.IsSuccessStatusCode;
 }