钉钉相关提醒接口

gogsController.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using ddAlter.DB;
  2. using ddAlter.Models;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.EntityFrameworkCore;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net.Http;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace ddAlter.Controllers
  14. {
  15. [Produces("application/json")]
  16. [Route("api/[controller]")]
  17. [ApiController]
  18. public class gogsController : ControllerBase
  19. {
  20. public readonly IHttpClientFactory _httpClientFactory;
  21. public gogsController(IHttpClientFactory httpClientFactory)
  22. {
  23. _httpClientFactory = httpClientFactory;
  24. }
  25. [HttpPost]
  26. [Route("gogs-msg")]
  27. async public Task<ActionResult> GogsMessage([FromBody] GogsCommits body)
  28. {
  29. if (body.commits == null || body.commits.Count < 1)
  30. {
  31. return Ok(new
  32. {
  33. commits = "",
  34. result = "body为空",
  35. });
  36. }
  37. string title = string.Format("【{0}】{1}提交了代码", body.repository.name, body.commits[0].committer.name);
  38. string text = string.Format("### 【{0}】{1}提交了代码 \n ", body.repository.name, body.commits[0].committer.name);
  39. for (int i = 0; i < body.commits.Count; i++)
  40. {
  41. text += string.Format("- [{0}]({1}) \n > {2} \n ", body.commits[i].id, body.commits[i].url.Replace("localhost", "192.168.1.222"), body.commits[i].message);
  42. }
  43. // 发送报警
  44. var msg = new
  45. {
  46. msgtype = "markdown",
  47. markdown = new
  48. {
  49. title = title,
  50. text = text,
  51. },
  52. //at = new
  53. //{
  54. // isAtAll = true
  55. //}
  56. };
  57. var sendMessage = JsonConvert.SerializeObject(msg);
  58. //https://oapi.dingtalk.com/robot/send?access_token=86ec01540e08233500a22e6a0f8a631fb4671901c6cf4d4cb80c16d59db3e675
  59. var request = new HttpRequestMessage(HttpMethod.Post, "/robot/send?access_token=86ec01540e08233500a22e6a0f8a631fb4671901c6cf4d4cb80c16d59db3e675")
  60. {
  61. Content = new StringContent(sendMessage, Encoding.UTF8, "application/json")
  62. };
  63. var client = _httpClientFactory.CreateClient("client_dd");
  64. client.Timeout = TimeSpan.FromSeconds(10);
  65. var response = await client.SendAsync(request);
  66. var result = response.Content.ReadAsStringAsync();
  67. return Ok(new
  68. {
  69. commits = body.commits,
  70. result = result,
  71. });
  72. }
  73. }
  74. }