diff --git a/Samples/HelloWorld/test.http b/Samples/HelloWorld/test.http index 54c5478..6a469e9 100644 --- a/Samples/HelloWorld/test.http +++ b/Samples/HelloWorld/test.http @@ -1,5 +1,5 @@ # Mimicks a payload slack would send for app_mention events, like "@yourbot dostuff" in this case: -GET http://localhost:5000/ +GET http://localhost:1337/ X-Slack-Request-Timestamp: 12331231 X-Slack-Signature: v0:abc123etcetc @@ -12,3 +12,13 @@ X-Slack-Signature: v0:abc123etcetc "channel": "C92QZTVEF" } } + +### +# Verification request +GET http://localhost:1337 + +{ + "token": "Jhj5dZrVaK7ZwHHjRyZWjbDl", + "challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P", + "type": "url_verification" +} diff --git a/source/src/Slackbot.Net.Endpoints/Middlewares/Challenge.cs b/source/src/Slackbot.Net.Endpoints/Middlewares/Challenge.cs index d883c18..882e1f0 100644 --- a/source/src/Slackbot.Net.Endpoints/Middlewares/Challenge.cs +++ b/source/src/Slackbot.Net.Endpoints/Middlewares/Challenge.cs @@ -1,7 +1,8 @@ +using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; + namespace Slackbot.Net.Endpoints.Middlewares { @@ -20,7 +21,7 @@ public async Task Invoke(HttpContext context) _logger.LogInformation($"Handling challenge request. Challenge: {challenge}"); context.Response.StatusCode = 200; context.Response.ContentType = "application/json"; - await context.Response.WriteAsync(JsonConvert.SerializeObject(new {challenge})); + await context.Response.WriteAsync(JsonSerializer.Serialize(new {challenge})); } public static bool ShouldRun(HttpContext ctx) diff --git a/source/src/Slackbot.Net.Endpoints/Middlewares/HttpItemsManager.cs b/source/src/Slackbot.Net.Endpoints/Middlewares/HttpItemsManager.cs index 89f0a06..8669bb8 100644 --- a/source/src/Slackbot.Net.Endpoints/Middlewares/HttpItemsManager.cs +++ b/source/src/Slackbot.Net.Endpoints/Middlewares/HttpItemsManager.cs @@ -1,10 +1,9 @@ using System.IO; using System.Text; +using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using Slackbot.Net.Endpoints.Models.Events; using Slackbot.Net.Endpoints.Models.Interactive; using Slackbot.Net.Endpoints.Models.Interactive.BlockActions; @@ -32,21 +31,23 @@ public async Task Invoke(HttpContext context) if (body.StartsWith("{")) { - var jObject = JObject.Parse(body); + var jObject = JsonDocument.Parse(body); _logger.LogTrace(body); - if (jObject.ContainsKey("challenge")) + JsonElement challengeValue; + bool isChallenge = jObject.RootElement.TryGetProperty("challenge", out challengeValue); + if (isChallenge) { - context.Items.Add(HttpItemKeys.ChallengeKey, jObject["challenge"]); + context.Items.Add(HttpItemKeys.ChallengeKey, challengeValue); } else { - var metadata = JsonConvert.DeserializeObject(body); - if (jObject["event"] is JObject @event) + var metadata = JsonSerializer.Deserialize(body, WebOptions); + if (jObject.RootElement.GetProperty("event") is JsonElement @event) { var slackEvent = ToEventType(@event, body); context.Items.Add(HttpItemKeys.EventMetadataKey, metadata); context.Items.Add(HttpItemKeys.SlackEventKey, slackEvent); - context.Items.Add(HttpItemKeys.EventTypeKey, @event["type"]); + context.Items.Add(HttpItemKeys.EventTypeKey, @event.GetProperty("type")); } } } @@ -58,7 +59,7 @@ public async Task Invoke(HttpContext context) _logger.LogTrace(body); var payloadJsonUrlEncoded = body.Remove(0,8); var decodedJson = System.Net.WebUtility.UrlDecode(payloadJsonUrlEncoded); - var payload = JObject.Parse(decodedJson); + var payload = JsonDocument.Parse(decodedJson).RootElement; var interactivePayloadTyped = ToInteractiveType(payload, body); context.Items.Add(HttpItemKeys.InteractivePayloadKey, interactivePayloadTyped); } @@ -69,51 +70,54 @@ public async Task Invoke(HttpContext context) await _next(context); } - private static SlackEvent ToEventType(JObject eventJson, string raw) + private static JsonSerializerOptions WebOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web); + + private static SlackEvent ToEventType(JsonElement eventJson, string raw) { var eventType = GetEventType(eventJson); + string json = eventJson.ToString(); switch (eventType) { case EventTypes.AppMention: - return eventJson.ToObject(); + return JsonSerializer.Deserialize(json, WebOptions); case EventTypes.MemberJoinedChannel: - return eventJson.ToObject(); + return JsonSerializer.Deserialize(json, WebOptions); case EventTypes.AppHomeOpened: - return eventJson.ToObject(); + return JsonSerializer.Deserialize(json, WebOptions); default: - UnknownSlackEvent unknownSlackEvent = eventJson.ToObject(); + UnknownSlackEvent unknownSlackEvent = JsonSerializer.Deserialize(json, WebOptions); unknownSlackEvent.RawJson = raw; return unknownSlackEvent; } } - private static Interaction ToInteractiveType(JObject payloadJson, string raw) + private static Interaction ToInteractiveType(JsonElement payloadJson, string raw) { var eventType = GetEventType(payloadJson); switch (eventType) { case InteractionTypes.ViewSubmission: - var viewSubmission = payloadJson.ToObject(); + var viewSubmission = JsonSerializer.Deserialize(payloadJson.GetString(), WebOptions); - var view = payloadJson["view"] as JObject; - var viewState = view["state"] as JObject;; - viewSubmission.ViewId = view.Value("id"); + var view = payloadJson.GetProperty("view"); + var viewState = view.GetProperty("state"); + viewSubmission.ViewId = view.GetProperty("id").GetString(); viewSubmission.ViewState = viewState; return viewSubmission; case InteractionTypes.BlockActions: - return payloadJson.ToObject(); + return JsonSerializer.Deserialize(payloadJson.GetString(), WebOptions); default: - var unknownSlackEvent = payloadJson.ToObject(); + var unknownSlackEvent = JsonSerializer.Deserialize(payloadJson.GetString(), WebOptions); unknownSlackEvent.RawJson = raw; return unknownSlackEvent; } } - public static string GetEventType(JObject eventJson) + public static string GetEventType(JsonElement eventJson) { - if (eventJson != null) + if (eventJson.ValueKind != JsonValueKind.Null) { - return eventJson["type"].Value(); + return eventJson.GetProperty("type").GetString(); } return "unknown"; diff --git a/source/src/Slackbot.Net.Endpoints/Models/Interactive/Team.cs b/source/src/Slackbot.Net.Endpoints/Models/Interactive/Team.cs index ecb7e94..2a9cc87 100644 --- a/source/src/Slackbot.Net.Endpoints/Models/Interactive/Team.cs +++ b/source/src/Slackbot.Net.Endpoints/Models/Interactive/Team.cs @@ -1,10 +1,7 @@ -using Newtonsoft.Json; - namespace Slackbot.Net.Endpoints.Models.Interactive { public class Team { - [JsonProperty("id")] public string Id { get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.Endpoints/Models/Interactive/User.cs b/source/src/Slackbot.Net.Endpoints/Models/Interactive/User.cs index ab7c08c..42171a9 100644 --- a/source/src/Slackbot.Net.Endpoints/Models/Interactive/User.cs +++ b/source/src/Slackbot.Net.Endpoints/Models/Interactive/User.cs @@ -1,10 +1,7 @@ -using Newtonsoft.Json; - namespace Slackbot.Net.Endpoints.Models.Interactive { public class User { - [JsonProperty("user_id")] public string User_Id { get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.Endpoints/Models/Interactive/ViewSubmissions/ViewSubmission.cs b/source/src/Slackbot.Net.Endpoints/Models/Interactive/ViewSubmissions/ViewSubmission.cs index 4075720..6454edc 100644 --- a/source/src/Slackbot.Net.Endpoints/Models/Interactive/ViewSubmissions/ViewSubmission.cs +++ b/source/src/Slackbot.Net.Endpoints/Models/Interactive/ViewSubmissions/ViewSubmission.cs @@ -1,5 +1,4 @@ - -using Newtonsoft.Json.Linq; +using System.Text.Json; namespace Slackbot.Net.Endpoints.Models.Interactive.ViewSubmissions { @@ -9,6 +8,6 @@ public class ViewSubmission : Interaction public User User { get; set; } public string ViewId { get; set; } - public JObject ViewState { get; set; } + public JsonElement ViewState { get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.Endpoints/Slackbot.Net.Endpoints.csproj b/source/src/Slackbot.Net.Endpoints/Slackbot.Net.Endpoints.csproj index a6cd551..b19fbf4 100644 --- a/source/src/Slackbot.Net.Endpoints/Slackbot.Net.Endpoints.csproj +++ b/source/src/Slackbot.Net.Endpoints/Slackbot.Net.Endpoints.csproj @@ -20,8 +20,7 @@ - - + diff --git a/source/src/Slackbot.Net.SlackClients.Http/Extensions/HttpClientExtensions.cs b/source/src/Slackbot.Net.SlackClients.Http/Extensions/HttpClientExtensions.cs index bd39d34..a0a0e3f 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Extensions/HttpClientExtensions.cs +++ b/source/src/Slackbot.Net.SlackClients.Http/Extensions/HttpClientExtensions.cs @@ -3,9 +3,10 @@ using System.Linq; using System.Net.Http; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; + using Slackbot.Net.SlackClients.Http.Exceptions; using Slackbot.Net.SlackClients.Http.Models.Responses; @@ -13,15 +14,13 @@ namespace Slackbot.Net.SlackClients.Http.Extensions { internal static class HttpClientExtensions { - private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings + private static readonly JsonSerializerOptions JsonSerializerSettings = new JsonSerializerOptions(JsonSerializerDefaults.Web) { - ContractResolver = new CamelCasePropertyNamesContractResolver(), - NullValueHandling = NullValueHandling.Ignore + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; - public static async Task PostJson(this HttpClient httpClient, object payload, string api, Action logger = null) where T:Response { - var serializedObject = JsonConvert.SerializeObject(payload, JsonSerializerSettings); + var serializedObject = JsonSerializer.Serialize(payload, JsonSerializerSettings); var httpContent = new StringContent(serializedObject, Encoding.UTF8, "application/json"); var request = new HttpRequestMessage(HttpMethod.Post, api) { @@ -40,7 +39,7 @@ public static async Task PostJson(this HttpClient httpClient, object paylo response.EnsureSuccessStatusCode(); - var resObj = JsonConvert.DeserializeObject(responseContent, JsonSerializerSettings); + var resObj = JsonSerializer.Deserialize(responseContent, JsonSerializerSettings); if (!resObj.Ok) { @@ -75,7 +74,7 @@ public static async Task PostParametersAsForm(this HttpClient httpClient, response.EnsureSuccessStatusCode(); - var resObj = JsonConvert.DeserializeObject(responseContent, JsonSerializerSettings); + var resObj = JsonSerializer.Deserialize(responseContent, JsonSerializerSettings); if(!resObj.Ok) throw new WellKnownSlackApiException(error: $"{resObj.Error}", responseContent:responseContent); diff --git a/source/src/Slackbot.Net.SlackClients.Http/Models/Requests/ChatPostMessage/ChatPostMessageRequest.cs b/source/src/Slackbot.Net.SlackClients.Http/Models/Requests/ChatPostMessage/ChatPostMessageRequest.cs index d11d828..48851bf 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Models/Requests/ChatPostMessage/ChatPostMessageRequest.cs +++ b/source/src/Slackbot.Net.SlackClients.Http/Models/Requests/ChatPostMessage/ChatPostMessageRequest.cs @@ -21,38 +21,38 @@ public class ChatPostMessageRequest public class Attachment { - public string callback_id; - public string fallback; - public string color; - public string pretext; - public string author_name; - public string author_link; - public string author_icon; - public string title; - public string title_link; - public string text; - public Field[] fields; - public string image_url; - public string thumb_url; - public string[] mrkdwn_in; - public AttachmentAction[] actions; - public string footer; - public string footer_icon; + public string callback_id{ get; set; } + public string fallback { get; set; } + public string color { get; set; } + public string pretext { get; set; } + public string author_name { get; set; } + public string author_link { get; set; } + public string author_icon { get; set; } + public string title { get; set; } + public string title_link { get; set; } + public string text { get; set; } + public Field[] fields { get; set; } + public string image_url { get; set; } + public string thumb_url { get; set; } + public string[] mrkdwn_in { get; set; } + public AttachmentAction[] actions { get; set; } + public string footer { get; set; } + public string footer_icon { get; set; } } public class Field { - public string title; - public string value; - public bool @short; + public string title { get; set; } + public string value { get; set; } + public bool @short { get; set; } } public class AttachmentAction { public string type = "button"; - public string style; - public string value; - public ActionConfirm confirm; + public string style { get; set; } + public string value { get; set; } + public ActionConfirm confirm { get; set; } public AttachmentAction(string name, string text) { @@ -67,9 +67,9 @@ public AttachmentAction(string name, string text) public class ActionConfirm { - public string title; - public string text; - public string ok_text; - public string dismiss_text; + public string title { get; set; } + public string text { get; set; } + public string ok_text { get; set; } + public string dismiss_text { get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.SlackClients.Http/Models/Requests/ViewPublish/ViewPublishRequest.cs b/source/src/Slackbot.Net.SlackClients.Http/Models/Requests/ViewPublish/ViewPublishRequest.cs index 9e799f7..a1c69ae 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Models/Requests/ViewPublish/ViewPublishRequest.cs +++ b/source/src/Slackbot.Net.SlackClients.Http/Models/Requests/ViewPublish/ViewPublishRequest.cs @@ -1,5 +1,3 @@ -using Newtonsoft.Json; - namespace Slackbot.Net.SlackClients.Http.Models.Requests.ViewPublish { public class ViewPublishRequest @@ -8,8 +6,7 @@ public ViewPublishRequest(string userId) { User_Id = userId; } - - [JsonProperty("user_id")] + public string User_Id { get; } public View View { get; set; } diff --git a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/ChatPostMessage/ChatPostMessageResponse.cs b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/ChatPostMessage/ChatPostMessageResponse.cs index 08d2fe9..fd185f0 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/ChatPostMessage/ChatPostMessageResponse.cs +++ b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/ChatPostMessage/ChatPostMessageResponse.cs @@ -2,18 +2,18 @@ namespace Slackbot.Net.SlackClients.Http.Models.Responses.ChatPostMessage { public class ChatPostMessageResponse : Response { - public string ts; - public string channel; - public Message message; + public string ts { get; set; } + public string channel { get; set; } + public Message message{ get; set; } } public class Message { - public string text; - public string user; - public string username; - public string type; - public string subtype; - public string ts; + public string text { get; set; } + public string user { get; set; } + public string username { get; set; } + public string type { get; set; } + public string subtype{ get; set; } + public string ts{ get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/ConversationsList/ConversationsListResponse.cs b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/ConversationsList/ConversationsListResponse.cs index c7d60ab..0e5fbe6 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/ConversationsList/ConversationsListResponse.cs +++ b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/ConversationsList/ConversationsListResponse.cs @@ -16,12 +16,12 @@ public class ResponseMetadata public class Conversation { - public string Name; - public string Id; - public bool Is_Channel; - public bool Is_Group; - public bool Is_Im; - public bool Is_General; - public bool Is_Archived; + public string Name { get; set; } + public string Id { get; set; } + public bool Is_Channel { get; set; } + public bool Is_Group { get; set; } + public bool Is_Im { get; set; } + public bool Is_General { get; set; } + public bool Is_Archived { get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/Response.cs b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/Response.cs index 3e950d1..0a7ae3d 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/Response.cs +++ b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/Response.cs @@ -2,7 +2,7 @@ namespace Slackbot.Net.SlackClients.Http.Models.Responses { public class Response { - public bool Ok; - public string Error; + public bool Ok { get; set; } + public string Error { get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/SearchMessages/SearchMessagesResponse.cs b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/SearchMessages/SearchMessagesResponse.cs index 27fc185..e62c38f 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/SearchMessages/SearchMessagesResponse.cs +++ b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/SearchMessages/SearchMessagesResponse.cs @@ -2,36 +2,36 @@ namespace Slackbot.Net.SlackClients.Http.Models.Responses.SearchMessages { public class SearchMessagesResponse : Response { - public SearchResponseMessagesContainer Messages; + public SearchResponseMessagesContainer Messages { get; set; } } public class SearchResponseMessagesContainer { - public ContextMessage[] Matches; + public ContextMessage[] Matches { get; set; } } public class ContextMessage : Message { - public Message Previous_2; - public Message Previous; - public Message Next; - public Message Next_2; + public Message Previous_2{ get; set; } + public Message Previous { get; set; } + public Message Next { get; set; } + public Message Next_2 { get; set; } } public class Error { - public int Code; - public string Msg; + public int Code { get; set; } + public string Msg { get; set; } } public class SlackSocketMessage { - public bool Ok = true; - public int Id; - public int Reply_To; - public string Type; - public string SubType; - public Error Error; + public bool Ok{ get; set; } = true; + public int Id { get; set; } + public int Reply_To { get; set; } + public string Type { get; set; } + public string SubType { get; set; } + public Error Error { get; set; } } public class Reaction @@ -43,19 +43,19 @@ public class Reaction public class Message : SlackSocketMessage { - public Channel Channel; - public string Ts; //epoch - public string User; - public string Username; - public string Text; - public bool Is_Starred; - public string Permalink; - public Reaction[] Reactions; + public Channel Channel { get; set; } + public string Ts{ get; set; } //epoch + public string User{ get; set; } + public string Username{ get; set; } + public string Text{ get; set; } + public bool Is_Starred{ get; set; } + public string Permalink{ get; set; } + public Reaction[] Reactions{ get; set; } } public class Channel { - public string Name; - public string Id; + public string Name{ get; set; } + public string Id{ get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/UsersList/UsersListResponse.cs b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/UsersList/UsersListResponse.cs index 9aae491..16201dd 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/UsersList/UsersListResponse.cs +++ b/source/src/Slackbot.Net.SlackClients.Http/Models/Responses/UsersList/UsersListResponse.cs @@ -2,53 +2,53 @@ namespace Slackbot.Net.SlackClients.Http.Models.Responses.UsersList { public class UsersListResponse : Response { - public User[] Members; + public User[] Members { get; set; } } public class User { - public string Id; - public string Name; - public bool Deleted; - public string Color; - public UserProfile Profile; - public bool Is_Admin; - public bool Is_Owner; - public bool Is_Primary_Owner; - public bool Is_Restricted; - public bool Is_Ultra_restricted; - public bool Has_2fa; - public string Two_factor_type; - public bool Has_files; - public string Presence; - public bool Is_Bot; - public string Tz; - public string Tz_Label; - public int Tz_Offset; - public string Team_Id; - public string Real_name; + public string Id { get; set; } + public string Name { get; set; } + public bool Deleted { get; set; } + public string Color { get; set; } + public UserProfile Profile { get; set; } + public bool Is_Admin { get; set; } + public bool Is_Owner { get; set; } + public bool Is_Primary_Owner { get; set; } + public bool Is_Restricted { get; set; } + public bool Is_Ultra_restricted { get; set; } + public bool Has_2fa { get; set; } + public string Two_factor_type { get; set; } + public bool Has_files { get; set; } + public string Presence { get; set; } + public bool Is_Bot{ get; set; } + public string Tz { get; set; } + public string Tz_Label { get; set; } + public int Tz_Offset { get; set; } + public string Team_Id { get; set; } + public string Real_name { get; set; } } public class UserProfile : ProfileIcons { - public string First_Name; - public string Last_Name; - public string Real_Name; - public string Email; - public string Skype; - public string Status_Emoji; - public string Status_Text; - public string Phone; + public string First_Name { get; set; } + public string Last_Name { get; set; } + public string Real_Name { get; set; } + public string Email { get; set; } + public string Skype { get; set; } + public string Status_Emoji { get; set; } + public string Status_Text { get; set; } + public string Phone { get; set; } } public class ProfileIcons { - public string image_24; - public string image_32; - public string image_48; - public string image_72; - public string image_192; - public string image_512; + public string image_24 { get; set; } + public string image_32 { get; set; } + public string image_48 { get; set; } + public string image_72 { get; set; } + public string image_192 { get; set; } + public string image_512 { get; set; } } } \ No newline at end of file diff --git a/source/src/Slackbot.Net.SlackClients.Http/Slackbot.Net.SlackClients.Http.csproj b/source/src/Slackbot.Net.SlackClients.Http/Slackbot.Net.SlackClients.Http.csproj index dd85b6d..de85e06 100644 --- a/source/src/Slackbot.Net.SlackClients.Http/Slackbot.Net.SlackClients.Http.csproj +++ b/source/src/Slackbot.Net.SlackClients.Http/Slackbot.Net.SlackClients.Http.csproj @@ -18,11 +18,6 @@ git - - - - - @@ -33,10 +28,6 @@ - - - -