From 2765298606a12f3c1a4ac41a43346e96607ed728 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Mon, 8 Jul 2019 23:20:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5VS=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DingtalkRobot.sln | 31 ++++++++++++ .../SendMessageTest.cs | 21 ++++++++ .../Tianyi.DingtalkRobotKit.Test.csproj | 21 ++++++++ Tianyi.DingtalkRobotKit.Test/UseXunit.cs | 14 ++++++ Tianyi.DingtalkRobotKit/MessageBase.cs | 22 +++++++++ Tianyi.DingtalkRobotKit/MessageResult.cs | 23 +++++++++ Tianyi.DingtalkRobotKit/SendMessage.cs | 46 +++++++++++++++++ Tianyi.DingtalkRobotKit/TextMessage.cs | 49 +++++++++++++++++++ Tianyi.DingtalkRobotKit/TextOption.cs | 11 +++++ .../Tianyi.DingtalkRobotKit.csproj | 12 +++++ Tianyi.DingtalkRobotKit/at.cs | 18 +++++++ 11 files changed, 268 insertions(+) create mode 100644 DingtalkRobot.sln create mode 100644 Tianyi.DingtalkRobotKit.Test/SendMessageTest.cs create mode 100644 Tianyi.DingtalkRobotKit.Test/Tianyi.DingtalkRobotKit.Test.csproj create mode 100644 Tianyi.DingtalkRobotKit.Test/UseXunit.cs create mode 100644 Tianyi.DingtalkRobotKit/MessageBase.cs create mode 100644 Tianyi.DingtalkRobotKit/MessageResult.cs create mode 100644 Tianyi.DingtalkRobotKit/SendMessage.cs create mode 100644 Tianyi.DingtalkRobotKit/TextMessage.cs create mode 100644 Tianyi.DingtalkRobotKit/TextOption.cs create mode 100644 Tianyi.DingtalkRobotKit/Tianyi.DingtalkRobotKit.csproj create mode 100644 Tianyi.DingtalkRobotKit/at.cs diff --git a/DingtalkRobot.sln b/DingtalkRobot.sln new file mode 100644 index 0000000..9336886 --- /dev/null +++ b/DingtalkRobot.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.705 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tianyi.DingtalkRobotKit", "Tianyi.DingtalkRobotKit\Tianyi.DingtalkRobotKit.csproj", "{267E50DE-B16A-41D0-888F-A5F369DCA9EB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tianyi.DingtalkRobotKit.Test", "Tianyi.DingtalkRobotKit.Test\Tianyi.DingtalkRobotKit.Test.csproj", "{B203751E-0266-4B10-934E-08944B105B58}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {267E50DE-B16A-41D0-888F-A5F369DCA9EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {267E50DE-B16A-41D0-888F-A5F369DCA9EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {267E50DE-B16A-41D0-888F-A5F369DCA9EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {267E50DE-B16A-41D0-888F-A5F369DCA9EB}.Release|Any CPU.Build.0 = Release|Any CPU + {B203751E-0266-4B10-934E-08944B105B58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B203751E-0266-4B10-934E-08944B105B58}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B203751E-0266-4B10-934E-08944B105B58}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B203751E-0266-4B10-934E-08944B105B58}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {781E339B-7C68-45CB-A43D-EEEE63AE295F} + EndGlobalSection +EndGlobal diff --git a/Tianyi.DingtalkRobotKit.Test/SendMessageTest.cs b/Tianyi.DingtalkRobotKit.Test/SendMessageTest.cs new file mode 100644 index 0000000..5999a42 --- /dev/null +++ b/Tianyi.DingtalkRobotKit.Test/SendMessageTest.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace Tianyi.DingtalkRobotKit.Test +{ + public class SendMessageTest + { + [Fact] + public void Test() + { + var textMsg = new TextMessage("明天是美好的一天"); + textMsg.at.isAtAll = true; + + var result = SendMessage.Send(textMsg); + + Assert.Equal(0, result.errcode); + } + } +} diff --git a/Tianyi.DingtalkRobotKit.Test/Tianyi.DingtalkRobotKit.Test.csproj b/Tianyi.DingtalkRobotKit.Test/Tianyi.DingtalkRobotKit.Test.csproj new file mode 100644 index 0000000..945be64 --- /dev/null +++ b/Tianyi.DingtalkRobotKit.Test/Tianyi.DingtalkRobotKit.Test.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp2.2 + + false + + + + + + + + + + + + + + + diff --git a/Tianyi.DingtalkRobotKit.Test/UseXunit.cs b/Tianyi.DingtalkRobotKit.Test/UseXunit.cs new file mode 100644 index 0000000..e76b674 --- /dev/null +++ b/Tianyi.DingtalkRobotKit.Test/UseXunit.cs @@ -0,0 +1,14 @@ +using System; +using Xunit; + +namespace Tianyi.DingtalkRobotKit.Test +{ + public class UseXunit + { + [Fact] + public void Test1() + { + Assert.True(true,"ʹXunitеԪ"); + } + } +} diff --git a/Tianyi.DingtalkRobotKit/MessageBase.cs b/Tianyi.DingtalkRobotKit/MessageBase.cs new file mode 100644 index 0000000..aa118f0 --- /dev/null +++ b/Tianyi.DingtalkRobotKit/MessageBase.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tianyi.DingtalkRobotKit +{ + /// + /// 消息基类 + /// + public abstract class MessageBase + { + public MessageBase() + { + at = new at(); + } + public abstract string msgtype { get;} + + public at at { get; set; } + + public abstract string ToJson(); + } +} diff --git a/Tianyi.DingtalkRobotKit/MessageResult.cs b/Tianyi.DingtalkRobotKit/MessageResult.cs new file mode 100644 index 0000000..103f6b0 --- /dev/null +++ b/Tianyi.DingtalkRobotKit/MessageResult.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tianyi.DingtalkRobotKit +{ + /// + /// 消息发送返回值 + /// + public class MessageResult + { + /// + /// 结果代码 + /// 0 成功,其它均失败 + /// + public int errcode { get; set; } + + /// + /// 结果说明 + /// + public string errmsg { get; set; } + } +} diff --git a/Tianyi.DingtalkRobotKit/SendMessage.cs b/Tianyi.DingtalkRobotKit/SendMessage.cs new file mode 100644 index 0000000..e7642bd --- /dev/null +++ b/Tianyi.DingtalkRobotKit/SendMessage.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using RestSharp; + +namespace Tianyi.DingtalkRobotKit +{ + public static class SendMessage + { + public static string DingtalkRobotUrl + { + get + { + return "https://oapi.dingtalk.com/robot/send?access_token=f28974dc6d79e3c75570dda86e98a770a21e406aaf885804d9e7a7b73a33240f"; + } + set + { + DingtalkRobotUrl = value; + } + } + + public static MessageResult Send(T message) where T : MessageBase + { + MessageResult result = new MessageResult() + { + errcode=-1, + errmsg="" + }; + + + var client = new RestClient("https://oapi.dingtalk.com"); + + var request = new RestRequest("robot/send?access_token=f28974dc6d79e3c75570dda86e98a770a21e406aaf885804d9e7a7b73a33240f", Method.POST); + + request.AddHeader("Content-Type", "application/json"); + request.AddJsonBody(message.ToJson()); + + var response2 = client.Execute(request); + result.errcode = response2.Data.errcode; + result.errmsg = response2.Data.errmsg; + + return result; + } + } +} diff --git a/Tianyi.DingtalkRobotKit/TextMessage.cs b/Tianyi.DingtalkRobotKit/TextMessage.cs new file mode 100644 index 0000000..2b729b7 --- /dev/null +++ b/Tianyi.DingtalkRobotKit/TextMessage.cs @@ -0,0 +1,49 @@ +using Newtonsoft.Json; +using System; + +namespace Tianyi.DingtalkRobotKit +{ + /// + /// 文本消息 + /// + public class TextMessage : MessageBase + { + public TextMessage(TextOption option) :base() + { + this.text = option; + } + + public TextMessage(string message) : base() + { + var option = new TextOption() + { + content = message + }; + this.text = option; + } + + public TextOption text { get; set; } + + /// + /// 消息类型 + /// + public override string msgtype { get => "text"; } + + /// + /// 转换为json字符串 + /// + public override string ToJson() + { + var meaasge = new + { + msgtype = this.msgtype, + text = this.text, + at = this.at + }; + + string messageJson = JsonConvert.SerializeObject(meaasge); + + return messageJson; + } + } +} diff --git a/Tianyi.DingtalkRobotKit/TextOption.cs b/Tianyi.DingtalkRobotKit/TextOption.cs new file mode 100644 index 0000000..f874e48 --- /dev/null +++ b/Tianyi.DingtalkRobotKit/TextOption.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tianyi.DingtalkRobotKit +{ + public class TextOption + { + public string content { get; set; } + } +} diff --git a/Tianyi.DingtalkRobotKit/Tianyi.DingtalkRobotKit.csproj b/Tianyi.DingtalkRobotKit/Tianyi.DingtalkRobotKit.csproj new file mode 100644 index 0000000..48c04fd --- /dev/null +++ b/Tianyi.DingtalkRobotKit/Tianyi.DingtalkRobotKit.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.0 + + + + + + + + diff --git a/Tianyi.DingtalkRobotKit/at.cs b/Tianyi.DingtalkRobotKit/at.cs new file mode 100644 index 0000000..a865dc5 --- /dev/null +++ b/Tianyi.DingtalkRobotKit/at.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tianyi.DingtalkRobotKit +{ + public class at + { + public at() + { + atMobiles = new List(); + isAtAll = false; + } + public List atMobiles { get; set; } + + public bool isAtAll { get; set; } + } +}