/*--------------------------------------------------------------------------
* ReplayActiveMessageAPI.cs
*Auth:deepleo
* Date:2015.01.15
* Email:2586662969@qq.com
* Website:http://www.weixinsdk.net
*--------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using Codeplex.Data;
using LYFZ.Weixin.SDK.Entities;
namespace LYFZ.Weixin.SDK
{
///
/// 发送(主动)客服消息
///
public class ReplayActiveMessageAPI
{
///
/// 发送文本消息
///
/// 调用接口凭证
/// 普通用户openid
/// 文本消息内容
/// 完整客服账号,格式为:账号前缀@公众号微信号
///
public static bool RepayText(string access_token, string touser, string content, string kf_account = null)
{
var builder = new StringBuilder();
builder.Append("{")
.Append('"' + "touser" + '"' + ":").Append('"' + touser + '"').Append(",")
.Append('"' + "msgtype" + '"' + ":").Append('"' + "text" + '"').Append(",")
.Append('"' + "text" + '"' + ":")
.Append("{")
.Append('"' + "content" + '"' + ":").Append('"' + content + '"')
.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;
}
///
/// 发送图片消息
///
/// 调用接口凭证
/// 普通用户openid
/// 发送的图片的媒体ID
/// 完整客服账号,格式为:账号前缀@公众号微信号
///
public static bool RepayImage(string access_token, string touser, string media_id, string kf_account = null)
{
var builder = new StringBuilder();
builder.Append("{")
.Append('"' + "touser" + '"' + ":").Append('"' + touser + '"').Append(",")
.Append('"' + "msgtype" + '"' + ":").Append('"' + "image" + '"').Append(",")
.Append('"' + "image" + '"' + ":")
.Append("{")
.Append('"' + "media_id" + '"' + ":").Append('"' + media_id + '"')
.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;
}
///
/// 发送语音消息
///
/// 调用接口凭证
/// 普通用户openid
/// 发送的语音的媒体ID
/// 完整客服账号,格式为:账号前缀@公众号微信号
///
public static bool RepayVoice(string access_token, string touser, string media_id, string kf_account = null)
{
var builder = new StringBuilder();
builder.Append("{")
.Append('"' + "touser" + '"' + ":").Append('"' + touser + '"').Append(",")
.Append('"' + "msgtype" + '"' + ":").Append('"' + "voice" + '"').Append(",")
.Append('"' + "voice" + '"' + ":")
.Append("{")
.Append('"' + "media_id" + '"' + ":").Append('"' + media_id + '"')
.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;
}
///
/// 发送视频消息
///
/// 调用接口凭证
/// 普通用户openid
/// 发送的视频的媒体ID
/// 缩略图的媒体ID
/// 视频消息的标题
/// 视频消息的描述
/// 完整客服账号,格式为:账号前缀@公众号微信号
///
public static bool RepayVedio(string access_token, string touser, string media_id, string thumb_media_id, string title, string description, string kf_account = null)
{
var builder = new StringBuilder();
builder.Append("{")
.Append('"' + "touser" + '"' + ":").Append('"' + touser + '"').Append(",")
.Append('"' + "msgtype" + '"' + ":").Append('"' + "video" + '"').Append(",")
.Append('"' + "video" + '"' + ":")
.Append("{")
.Append('"' + "media_id" + '"' + ":").Append('"' + media_id + '"').Append(",")
.Append('"' + "thumb_media_id" + '"' + ":").Append('"' + thumb_media_id + '"').Append(",")
.Append('"' + "title" + '"' + ":").Append('"' + title + '"').Append(",")
.Append('"' + "description" + '"' + ":").Append('"' + description + '"')
.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;
}
///
/// 发送音乐消息
///
/// 调用接口凭证
/// 普通用户openid
/// 音乐链接
/// 高品质音乐链接,wifi环境优先使用该链接播放音乐
/// 缩略图的媒体ID
/// 音乐标题
/// 音乐描述
/// 完整客服账号,格式为:账号前缀@公众号微信号
///
public static bool RepayMusic(string access_token, string touser, string musicurl, string hqmusicurl, string thumb_media_id, string title, string description, string kf_account = null)
{
var builder = new StringBuilder();
builder.Append("{")
.Append('"' + "touser" + '"' + ":").Append('"' + touser + '"').Append(",")
.Append('"' + "msgtype" + '"' + ":").Append('"' + "music" + '"').Append(",")
.Append('"' + "music" + '"' + ":")
.Append("{")
.Append('"' + "title" + '"' + ":").Append('"' + title + '"').Append(",")
.Append('"' + "description" + '"' + ":").Append('"' + description + '"').Append(",")
.Append('"' + "musicurl" + '"' + ":").Append('"' + musicurl + '"').Append(",")
.Append('"' + "hqmusicurl" + '"' + ":").Append('"' + hqmusicurl + '"').Append(",")
.Append('"' + "thumb_media_id" + '"' + ":").Append('"' + thumb_media_id + '"').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;
}
///
/// 回复单图文消息
///
/// 调用接口凭证
/// 普通用户openid
///
/// 完整客服账号,格式为:账号前缀@公众号微信号
///
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;
}
///
/// 回复多图文消息
///
/// 调用接口凭证
/// 普通用户openid
///
/// 完整客服账号,格式为:账号前缀@公众号微信号
///
public static bool RepayNews(string access_token, string touser, List 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("[");
for (int i = 0; i < news.Count; i++)
{
var n = news[i];
builder.Append("{")
.Append('"' + "title" + '"' + ":").Append('"' + n.title + '"').Append(",")
.Append('"' + "description" + '"' + ":").Append('"' + n.description + '"').Append(",")
.Append('"' + "url" + '"' + ":").Append('"' + n.url + '"').Append(",")
.Append('"' + "picurl" + '"' + ":").Append('"' + n.picurl + '"')
.Append("}");
if (i != news.Count - 1) builder.Append(",");
}
builder.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;
}
}
}