예제 #1
0
 /// <summary>
 /// 测试初始化
 /// </summary>
 public WHLogsServiceTest()
 {
     _scope            = Ioc.BeginScope();
     _wHLogsRepository = _scope.Create <IWHLogsRepository>();
     _wHLogsService    = _scope.Create <IWHLogsService>();
     _wHLogsDto        = WHLogsDtoTest.Create();
 }
예제 #2
0
 /// <summary>
 /// 转换为网络勾子推送内容日志实体
 /// </summary>
 /// <param name="dto">网络勾子推送内容日志数据传输对象</param>
 public static WHLogs ToEntity(this WHLogsDto dto)
 {
     if (dto == null)
     {
         return(new WHLogs());
     }
     return(dto.MapTo(new WHLogs(dto.Id.ToGuid())));
 }
예제 #3
0
 /// <summary>
 /// 转换为网络勾子推送内容日志实体
 /// </summary>
 /// <param name="dto">网络勾子推送内容日志数据传输对象</param>
 public static WHLogs ToEntity2(this WHLogsDto dto)
 {
     if (dto == null)
     {
         return(new WHLogs());
     }
     return(new WHLogs(dto.Id.ToGuid())
     {
         WhId = dto.WhId,
         WHTypes = dto.WHTypes.SafeValue(),
         RequestTop = dto.RequestTop,
         Content = dto.Content,
         ResponseTop = dto.ResponseTop,
         ResponseContent = dto.ResponseContent,
         ResponseBody = dto.ResponseBody,
         CreationTime = dto.CreationTime,
         IsDeleted = dto.IsDeleted.SafeValue(),
         Version = dto.Version,
     });
 }
예제 #4
0
        /// <summary>
        /// 调用钉钉机器人发送文本内容
        /// </summary>
        /// <param name="Access_Token">来源于</param>
        /// <returns></returns>
        public async Task <IActionResult> Send([FromQuery] string Access_Token)
        {
            var WHMiddlewareService = Ioc.Create <IWHMiddlewareService>();
            var WHLogsService       = Ioc.Create <IWHLogsService>();

            var model = await WHMiddlewareService.GetByToken(Access_Token);

            if (model == null)
            {
                return(Fail("指定的数据不存在"));
            }

            WebHook_Token = model.PushUrl;
            if (!model.PushUrl.Contains("access_token") && !model.PushToken.IsNullOrEmpty())
            {
                WebHook_Token += $"?access_token={model.PushToken}";
            }

            var dic = new Dictionary <string, string>();

            foreach (var row in HttpContext.Request.Headers)
            {
                dic.Add(row.Key, row.Value);
            }

            var content = await Web.GetBodyAsync();

            var Id = "";

            if (!content.IsNullOrEmpty())
            {
                var modelwhlogs = new WHLogsDto();
                modelwhlogs.WhId         = model.Id.ToString();
                modelwhlogs.WHTypes      = true;
                modelwhlogs.RequestTop   = dic.ToJson();
                modelwhlogs.Content      = content;
                modelwhlogs.CreationTime = DateTime.Now;

                Id = await WHLogsService.CreateAsync(modelwhlogs);
            }

            switch (model.Source)
            {
            case 1:
            {
                //消息类型
                var msgtype = MsgTypeEnum.actionCard.ToString();

                var modelContent = JObject.Parse(content);
                var repository   = modelContent["repository"];
                var pusher       = modelContent["pusher"];
                var Ref          = modelContent["ref"].ToString();
                var commits      = JArray.Parse(modelContent["commits"].ToString());

                var repositorychild = repository.Children().OfType <JProperty>()
                                      .ToDictionary(p => p.Name, p => p.Value);
                var pushchild = pusher.Children().OfType <JProperty>()
                                .ToDictionary(p => p.Name, p => p.Value);

                var parser   = new HtmlParser();
                var document = await parser.ParseDocumentAsync(repositorychild["html_url"].ToString());

                var html_url = document.QuerySelector("a").GetAttribute("href");

                var build = Pool.StringBuilder.Get();
                var title = "";

                if (dic.ContainsKey("X-Coding-Event"))
                {
                    if (dic["X-Coding-Event"] == "push")
                    {
                        build.Append("# Repo Push Event\n- ");
                        title = $"{pushchild["name"]}提交源码";
                    }
                }

                build.Append($"Repo: **[{repositorychild["name"].ToString()}]({html_url})**\n- Ref: **[{Ref.Substring(Ref.LastIndexOf("/") + 1)}]({html_url}/git/tree/{Ref.Substring(Ref.LastIndexOf("/") + 1)})**\n- Pusher: **{pushchild["name"]}**\n");
                build.Append($"## Total {commits.Count} commits(s)\n\u003e ");
                var i = 0;
                foreach (var item in commits)
                {
                    var author = JObject.Parse(item["author"].ToString());
                    build.Append($"{i}. [{item["id"].ToString().Substring(0, 7)}]({html_url}/{Ref.Substring(Ref.LastIndexOf("/") + 1)}/commit/{item["id"].ToString()}) {author["name"].ToString()} - {item["message"].ToString()} ");
                    if (i < commits.Count)
                    {
                        build.Append("\u003e ");
                    }
                    i++;
                }

                //actionCard内容
                var actionCard = new ActionCard
                {
                    Text           = build.Put(true),
                    Title          = title,
                    HideAvatar     = "0",
                    BtnOrientation = "0",
                    SingleTitle    = "查看详情",
                    SingleURL      = html_url
                };

                //指定目标人群
                var at = new At()
                {
                    AtMobiles = new List <string>()
                    {
                        "18307555593"
                    },
                    IsAtAll = false
                };

                var response = await SendDingTalkMessage(new { msgtype, actionCard, at });

                var modelwhlogs = await WHLogsService.GetByIdAsync(Id);

                var dicResponse = new Dictionary <string, string>();
                foreach (var row in response.Headers)
                {
                    dicResponse.Add(row.Key, row.Value.Join());
                }
                modelwhlogs.ResponseTop     = dicResponse.ToJson();
                modelwhlogs.ResponseContent = await response.Content.ReadAsStringAsync();

                modelwhlogs.ResponseBody = response.ToJson();

                await WHLogsService.UpdateAsync(modelwhlogs);

                return(Ok(response));
            }

            case 2:

            case 3:

            default:
            {
                //消息类型
                var msgtype = MsgTypeEnum.markdown.ToString();

                var modelContent = JObject.Parse(content);
                var text         = modelContent["text"].ToString();
                if (text.IsNullOrEmpty())
                {
                    return(Fail("内容为空"));
                }

                var title = text.Split("[")[0];

                //markdown内容
                var markdown = new MarkDown
                {
                    Text  = text,
                    Title = title
                };

                //指定目标人群
                var at = new At()
                {
                    AtMobiles = new List <string>()
                    {
                        "18307555593"
                    },
                    IsAtAll = false
                };

                var response = await SendDingTalkMessage(new { msgtype, markdown, at });

                return(Ok(response));
            }
            }
        }