namespace TestWeixin
{
public static class sendMessage
{
private static string accessToken = "";
private static Timer t = new Timer();//实例化Timer类
private static int expire = 0;//accsstoken有效期
static bool isGetAccessToken = true;
static string appid = "wx61bd2c8875eff596";
static string secret = "94e0d013a5afd95fff30352746d5481f";
/// <summary>
/// 发送文本信息
/// </summary>
/// <param name="openid">接受者openid</param>
/// <param name="message">要发送的信息</param>
public static void SendText(string openid, string message)
{
text txt = new text() { content = message };
Message<text> mes = new Message<text>() { msgtype = "text", text = txt, touser = openid };
string data = JsonConvert.SerializeObject(mes);
PostRequest(data);
}
/// <summary>
/// 发送语音消息
/// </summary>
public static void SendVoice()
{
}
/// <summary>
/// 发送视频信息
/// </summary>
public static void SendVideo()
{
}
/// <summary>
/// 发送音乐信息
/// </summary>
public static void SendMusic()
{
}
public static void SendAllMessage(List<string> opids,string content)
{
// string data = "{"touser": ["olHvdt0cRI3wBeB2xjKSc4EqBxCo"], "msgtype": "text", "text": { "content": "hello"}}";
AllText text = new AllText() { content=content};
MessageAll<AllText> all = new MessageAll<AllText>();
if(opids==null||opids.Count==0)
return;
all.touser = opids;
all.msgtype = "text";
string data = JsonConvert.SerializeObject(all);
PostAllMessage(data);
}
/// <summary>
/// 图文消息条数限制在10条以内,注意,如果图文数超过10,则将会无响应。
/// </summary>
/// <param name="openid">接受者openid</param>
/// <param name="articles">要发送的内容</param>
public static void SendNews(string openid, List<Articles> articles)
{
news n = new news() { articles = articles };
Message<news> m = new Message<news>() { msgtype = "news", news = n, touser = openid };
string data = JsonConvert.SerializeObject(m);
PostAllMessage(data);
}
/// <summary>
/// 执行Post请求
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
private static void PostRequest(string jsonData)
{
if (isGetAccessToken) GetAccessToken();
string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken;
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
Byte[] postData = Encoding.UTF8.GetBytes(jsonData);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = Encoding.UTF8.GetByteCount(jsonData);//获取json字符串的长度
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(postData, 0, postData.Length);//上传json参数
httpWebRequest.Timeout = 90000;
stream.Close();
}
public static void PostAllMessage(string jsonData)
{
if (isGetAccessToken) GetAccessToken();
string url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + accessToken;
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
Byte[] postData = Encoding.UTF8.GetBytes(jsonData);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = Encoding.UTF8.GetByteCount(jsonData);//获取json字符串的长度
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(postData, 0, postData.Length);//上传json参数
httpWebRequest.Timeout = 90000;
stream.Close();
}
/// <summary>
/// 获取Access_token
/// </summary>
/// <returns></returns>
public static string GetAccess_token()
{
string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
return GetRequst(uri);
}
/// <summary>
/// 发送get请求获取accesstoken
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
private static string GetRequst(Uri uri)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = 20000;
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();
return responseContent;
}
/// <summary>
/// 定时获取token
/// </summary>
/// <returns></returns>
private static void GetAccessToken()
{
string data = GetAccess_token();
GetAccessToken gat = JsonConvert.DeserializeObject<GetAccessToken>(data);
accessToken = gat.access_token;
expire = gat.expires_in;
t.Interval = (gat.expires_in - 5) * 60 * 60 * 1000;//防止access_token失效提前5秒
t.Start();
t.Elapsed += t_Elapsed;
isGetAccessToken = false;
}
static void t_Elapsed(object sender, ElapsedEventArgs e)
{
isGetAccessToken = true;//定期刷新accesstoken
}
}
class GetAccessToken
{
public string access_token { get; set; }
public int expires_in { get; set; }
}
/// <summary>
/// 消息基类
/// </summary>
class Message<T>
{
/// <summary>
/// 发送者openid
/// </summary>
public string touser { get; set; }
/// <summary>
/// 发送消息类型
/// </summary>
public string msgtype { get; set; }
public T text { get; set; }
public T voice { get; set; }
public T news { get; set; }
public T music { get; set; }
public T video { get; set; }
}
class MessageAll<T>
{
public List<string> touser { get; set; }
public string msgtype { get; set; }
public T text { get; set; }
}
/// <summary>
/// 发送文本消息类,小写方便序列化
/// </summary>
class text
{
public string content { get; set; }
public override string ToString()
{
return "text";
}
}
class voice
{
/// <summary>
/// 语音的媒体ID
/// </summary>
public string media_id { get; set; }
public override string ToString()
{
return "voice";
}
}
class video
{
/// <summary>
/// 发送的视频的媒体ID
/// </summary>
public string media_id { get; set; }
/// <summary>
/// 发送消息标题
/// </summary>
public string title { get; set; }
/// <summary>
/// 发送消息的藐视
/// </summary>
public string description { get; set; }
public override string ToString()
{
return "video";
}
}
class music
{
/// <summary>
/// 音乐标题
/// </summary>
public string title { get; set; }
/// <summary>
/// 音乐描述
/// </summary>
public string description { get; set; }
/// <summary>
/// 音乐链接
/// </summary>
public string musicurl { get; set; }
/// <summary>
/// 高品质音乐链接,wifi环境优先使用该链接播放音乐
/// </summary>
public string hqmusicurl { get; set; }
/// <summary>
/// 缩略图的媒体ID
/// </summary>
public string thumb_media_id { get; set; }
public override string ToString()
{
return "music";
}
}
/// <summary>
/// 图文消息条数限制在10条以内,注意,如果图文数超过10,则将会无响应。
/// </summary>
class news
{
public List<Articles> articles { get; set; }
public override string ToString()
{
return "news";
}
}
/// <summary>
/// 图文信息章节内容
/// </summary>
///
public class Articles
{
/// <summary>
/// 标题
/// </summary>
public string title { get; set; }
/// <summary>
/// 描述
/// </summary>
public string description { get; set; }
/// <summary>
/// 点击后跳转的链接
/// </summary>
public string url { get; set; }
/// <summary>
/// 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80
/// </summary>
public string picurl { get; set; }
}
public class AllText
{
public string content { get; set; }
}
}