113 lines
3.9 KiB
C#
113 lines
3.9 KiB
C#
using System.Text;
|
|
using Microsoft.Net.Http.Headers;
|
|
|
|
namespace WorkerService1;
|
|
|
|
public class WechatRobot
|
|
{
|
|
private const string ApiUrl = "https://qyapi.weixin.qq.com";
|
|
private const int TextUtf8MaxLength = 2000;
|
|
private const int MarkdownUtf8MaxLength = 4000;
|
|
private string _key;
|
|
|
|
private static readonly MediaTypeHeaderValue ApplicationJson =
|
|
new MediaTypeHeaderValue("application/json")
|
|
{
|
|
Charset = "utf-8"
|
|
};
|
|
|
|
private readonly HttpClient _client;
|
|
private readonly ILogger<WechatRobot> _logger;
|
|
|
|
public WechatRobot(ILogger<WechatRobot> logger, HttpClient client)
|
|
{
|
|
client.BaseAddress = new Uri(ApiUrl);
|
|
this._logger = logger;
|
|
_client = client;
|
|
_key = "e68b3791-e040-4c9b-9ac0-6f424e662185";
|
|
}
|
|
|
|
public async Task SendText(string content, bool isAtAll)
|
|
{
|
|
await this.Post($"/cgi-bin/webhook/send?key={_key}", BuildTextBody(content, isAtAll));
|
|
}
|
|
|
|
public async Task SendMarkdown(string content)
|
|
{
|
|
await this.Post($"/cgi-bin/webhook/send?key={_key}", BuildMarkdownBody(content));
|
|
}
|
|
|
|
private string BuildTextBody(string content, bool isAtAll)
|
|
{
|
|
var result = new StringBuilder("{\"msgtype\": \"text\",");
|
|
result.Append("\"text\": {");
|
|
result.Append($"\"content\": \"{Substring(content, TextUtf8MaxLength)}\",");
|
|
if (isAtAll)
|
|
{
|
|
result.Append($"\"mentioned_list\":[\"@all\"]");
|
|
}
|
|
|
|
result.Append("}}");
|
|
|
|
return result.ToString();
|
|
}
|
|
|
|
private string BuildMarkdownBody(string content)
|
|
{
|
|
var result = new StringBuilder("{\"msgtype\": \"markdown\",");
|
|
result.Append("\"markdown\": {");
|
|
result.Append($"\"content\": \"{Substring(FilterSpecialChar(content), MarkdownUtf8MaxLength)}\"");
|
|
result.Append("}}");
|
|
|
|
return result.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 截取字符串
|
|
/// </summary>
|
|
/// <param name="content">字符串内容</param>
|
|
/// <param name="maxLength">字符串最大长度</param>
|
|
/// <returns>如果长度超出则按照 maxLength 截取返回,若没超出则原样返回 </returns>
|
|
private string Substring(string content, int maxLength)
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes(content);
|
|
if (bytes.Length > maxLength)
|
|
{
|
|
var temporaryBytes = bytes.Take(maxLength).ToArray();
|
|
return $"{Encoding.UTF8.GetString(temporaryBytes)}";
|
|
}
|
|
|
|
return content;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 过滤 markdown 特殊字符
|
|
/// </summary>
|
|
/// <param name="content"></param>
|
|
/// <returns></returns>
|
|
private string FilterSpecialChar(string content) => content?.Replace("\"", "''").Replace("`", "'");
|
|
|
|
private async Task Post(string url, string parameters)
|
|
{
|
|
try
|
|
{
|
|
HttpContent content = null;
|
|
if (!String.IsNullOrEmpty(parameters))
|
|
{
|
|
content = new StringContent(parameters, Encoding.UTF8);
|
|
if (content.Headers.ContentType != null)
|
|
{
|
|
content.Headers.ContentType.MediaType = "application/json";
|
|
content.Headers.ContentType.CharSet = "utf-8";
|
|
}
|
|
}
|
|
|
|
var resp = await _client.PostAsync(url, content);
|
|
await resp.Content.ReadAsStringAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError(ex, $"WechatRobot Post fail,Url:{url},Parameters:{parameters}");
|
|
}
|
|
}
|
|
} |