using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Talk.Extensions;
namespace Utility.Tools
{
///
/// 企业微信
///
public class WeComTools
{
private static string access_token = "";
public WeComTools()
{
access_token = GetToken();
}
public string GetToken()
{
Dictionary dic = new Dictionary();
dic.Add("corpid", WeComConfig.corpid);
dic.Add("corpsecret", WeComConfig.corpsecret);
var response = HttpHelper.DoGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken", dic);
var modResponse = JsonConvert.DeserializeObject(response);
if (modResponse.errcode == 0)
{
return modResponse.access_token;
}
else
{
throw Oops.Bah(modResponse.errmsg);
}
}
public bool SendNewsMessage(string title, string content, string touser = "@all")
{
MessageRequest request = new MessageRequest()
{
touser = touser,
agentid = WeComConfig.agentid,
msgtype = "mpnews",
mpnews = new Mpnews()
{
articles = new List()
{
new ArticlesItem()
{
title = title,
thumb_media_id = WeComConfig.thumb_media_id,
content = content
}
}
}
};
var response = HttpHelper.DoPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+ access_token, JsonConvert.SerializeObject(request), "WeCom", "WMS");
var modResponse = JsonConvert.DeserializeObject(response);
if (modResponse.errcode == 0)
{
return true;
}
else
{
return false;
}
}
public bool SendTextMessage(string content, string touser = "@all")
{
MessageRequest request = new MessageRequest()
{
touser = touser,
agentid = WeComConfig.agentid,
msgtype = "text",
text = new Text() { content = content }
};
var response = HttpHelper.DoPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+ access_token, JsonConvert.SerializeObject(request), "WeCom", "WMS");
var modResponse = JsonConvert.DeserializeObject(response);
if (modResponse.errcode == 0)
{
return true;
}
else
{
return false;
}
}
}
public class WeComConfig
{
public static string corpid { get; set; }
public static int agentid { get; set; }
public static string corpsecret { get; set; }
public static string thumb_media_id { get; set; }
}
public class TokenResponse
{
///
///
///
public int errcode { get; set; }
///
///
///
public string errmsg { get; set; }
///
///
///
public string access_token { get; set; }
///
///
///
public int expires_in { get; set; }
}
public class ArticlesItem
{
///
/// 标题
///
public string title { get; set; }
///
///
///
public string thumb_media_id { get; set; }
///
///
///
public string content { get; set; }
}
public class Mpnews
{
///
///
///
public List articles { get; set; }
}
public class Text
{
public string content { get; set; }
}
public class MessageRequest
{
///
///
///
public string touser { get; set; }
///
///
///
public string msgtype { get; set; } = "mpnews";
///
///
///
public int agentid { get; set; }
///
///
///
public Mpnews mpnews { get; set; }
public Text text { get; set; }
}
}