大家有没有这样的需求,就是正在执行某个任务的时候希望任务完成之后给个通知,或者是软件运行报错的时候每天定期收集错误日志。
平时我们工作用的飞书,所有现在用飞书机器人把消息通知出来,非常简单 好用 的消息通知。
类似这样的消息通知还有企业微信也有。
- 飞书拉群的时候添加自定义机器
- 拿到飞书机器人的webhook ,根据飞书文档自定义机器人使用指南 - 开发指南 - 开发文档 - 飞书开放平台
C#实现如下,这个webhook真实可用,大家可以拿了玩玩,消息会发到我这里
private static readonly HttpClient client = new HttpClient();private static async Task SendMassageFeishu(string content){//自定义机器人webhook地址string webhookUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/475b444e-d0e4-4b69-9cba-e2382a69bea8";var message = new{msg_type = "text",content = new{text = content}};var json = JsonConvert.SerializeObject(message);var data = new StringContent(json, Encoding.UTF8, "application/json");var response = await client.PostAsync(webhookUrl, data);var result = await response.Content.ReadAsStringAsync();}
效果这样
3. 发送通过自定义机器人发送图片,需要先创建应用,并且开通图片上传权限
1. 发送图片 需要先创建应用,开通图片文件上传的权限。 获取开通应用的appId和appSecret,获取Tokenpublic async Task<string> GetTokenAsync(){string appId = @"cli_a7a4cdbd480b";string appSecret = "qDIqT7EIr98cV8Q3ofKqRSnQqDjA";using var client = new HttpClient();var requestBody = new{app_id = appId,app_secret = appSecret};var content = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8,"application/json");var response =await client.PostAsync("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/",content);string responseContent = await response.Content.ReadAsStringAsync();if (response.IsSuccessStatusCode){var jsonResponse = JObject.Parse(responseContent);string tenantAccessToken = jsonResponse["tenant_access_token"].ToString();return tenantAccessToken;}return "";}拿到Token之后调用上传图片的方法,拿到图片的image_keyprivate static async Task<string> UploadImage(string accessToken, string imagePath){using HttpClient client = new HttpClient();client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);using var formData = new MultipartFormDataContent();using var fileStream = File.OpenRead(imagePath);var fileContent = new StreamContent(fileStream);var mimeType = GetMimeType(imagePath);fileContent.Headers.ContentType = new MediaTypeHeaderValue(mimeType);formData.Add(fileContent, "image", Path.GetFileName(imagePath));formData.Add(new StringContent("message"), "image_type"); // 使用"message"作为默认image_typevar response = await client.PostAsync("https://open.feishu.cn/open-apis/image/v4/put/", formData);string responseBody = await response.Content.ReadAsStringAsync();if (response.IsSuccessStatusCode){var jsonResponse = JObject.Parse(responseBody);var imageKey = jsonResponse["data"]["image_key"]?.ToString();return imageKey;}else{}return null;}拿到Image_key之后就可以,通过自定义机器人的Webhook地址发送图片public async Task<bool> SendImageFeishuAsync(string imagePath){var token = await GetTokenAsync();var imageKey = await UploadImage(token, imagePath);// 替换为你自己的飞书机器人Webhook地址string webhookUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/05546c09-f7e7-466c-8660-18aa";// 将图片转换为Base64编码字符串var message = new{msg_type = "image",content = new{image_key = imageKey}};var json = JsonConvert.SerializeObject(message);var data = new StringContent(json, Encoding.UTF8, "application/json");var response = await client.PostAsync(webhookUrl, data);var result = await response.Content.ReadAsStringAsync();return response.IsSuccessStatusCode;}