Skip to content

Commit 7c6ae5f

Browse files
author
Kristoffer Schroeder
committed
SlashCommand parser
1 parent 2c585f9 commit 7c6ae5f

File tree

6 files changed

+100
-29
lines changed

6 files changed

+100
-29
lines changed

WooCode.Slack.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WooCode.Slack", "WooCode.Sl
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WooCode.Slack.Test", "WooCode.Slack.Test\WooCode.Slack.Test.csproj", "{16689154-B7EB-4716-8BD4-651EEE72BB9C}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WooCode.Slack.MVC", "WooCode.Slack.MVC\WooCode.Slack.MVC.csproj", "{3A004F9B-A264-4246-ABEC-917485E82564}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{16689154-B7EB-4716-8BD4-651EEE72BB9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{16689154-B7EB-4716-8BD4-651EEE72BB9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{16689154-B7EB-4716-8BD4-651EEE72BB9C}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{3A004F9B-A264-4246-ABEC-917485E82564}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{3A004F9B-A264-4246-ABEC-917485E82564}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{3A004F9B-A264-4246-ABEC-917485E82564}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{3A004F9B-A264-4246-ABEC-917485E82564}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

WooCode.Slack/SlashCommand.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.IO;
2+
using System.Text;
3+
using Newtonsoft.Json;
4+
5+
namespace WooCode.Slack
6+
{
7+
class SlashCommand
8+
{
9+
public string Token { get; set; }
10+
[JsonProperty(PropertyName = "team_id")]
11+
public string TeamId { get; set; }
12+
[JsonProperty(PropertyName = "channel_id")]
13+
public string ChannelId { get; set; }
14+
[JsonProperty(PropertyName = "channel_name")]
15+
public string ChannelName { get; set; }
16+
[JsonProperty(PropertyName = "user_id")]
17+
public string UserId { get; set; }
18+
[JsonProperty(PropertyName = "user_name")]
19+
public string UserName { get; set; }
20+
public string Command { get; set; }
21+
public string Text { get; set; }
22+
23+
public static SlashCommand From(string json)
24+
{
25+
return JsonConvert.DeserializeObject<SlashCommand>(json);
26+
}
27+
28+
public static SlashCommand From(Stream jsonStream)
29+
{
30+
using(var reader = new StreamReader(jsonStream,Encoding.UTF8))
31+
return From(reader.ReadToEnd());
32+
}
33+
}
34+
}

WooCode.Slack/WebHooks/Message.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ namespace WooCode.Slack.WebHooks
99
{
1010
public class Message
1111
{
12+
private string _channel;
13+
1214
[JsonIgnore]
1315
public string HookUrl { get; set; }
1416

1517
public string Text { get; set; }
16-
public string Channel { get; set; }
18+
19+
public string Channel
20+
{
21+
get { return _channel; }
22+
set { _channel = value.StartsWith("#") ? value : "#" + value; }
23+
}
24+
1725
public string UserName { get; set; }
1826
[JsonProperty(PropertyName = "icon_url")]
1927
public string IconUrl { get; set; }
@@ -47,28 +55,5 @@ public Message(string text, string channel = null, string userName = null) : thi
4755
Channel = channel ?? Channel;
4856
UserName = userName ?? UserName;
4957
}
50-
51-
public void AttachException<T>(T exception) where T : Exception
52-
{
53-
var attachment = new Attachment
54-
{
55-
Color = AttachmentColor.Danger
56-
};
57-
attachment.Fields.Add(new AttachmentField() { Title = "Type", Value = exception.GetType().FullName, Short = true });
58-
attachment.Fields.Add(new AttachmentField() { Title = "Message", Value = exception.Message, Short = true });
59-
60-
if (exception.StackTrace != null)
61-
attachment.Fields.Add(new AttachmentField() { Title = "StackTrace", Value = exception.StackTrace, Short = false });
62-
63-
if (exception.InnerException != null)
64-
{
65-
attachment.Fields.Add(new AttachmentField() { Title = "InnerException Type", Value = exception.InnerException.GetType().FullName, Short = true });
66-
attachment.Fields.Add(new AttachmentField() { Title = "InnerException Message", Value = exception.InnerException.Message, Short = true });
67-
if (exception.InnerException.StackTrace != null)
68-
attachment.Fields.Add(new AttachmentField() { Title = "InnerException StackTrace", Value = exception.InnerException.StackTrace, Short = false });
69-
}
70-
71-
Attachments.Add(attachment);
72-
}
7358
}
7459
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace WooCode.Slack.WebHooks
7+
{
8+
public static class MessageExtensions
9+
{
10+
public static void AttachException<T>(this Message @this, T exception) where T : Exception
11+
{
12+
var attachment = new Attachment
13+
{
14+
Color = AttachmentColor.Danger
15+
};
16+
attachment.Fields.Add(new AttachmentField() { Title = "Type", Value = exception.GetType().FullName, Short = true });
17+
attachment.Fields.Add(new AttachmentField() { Title = "Message", Value = exception.Message, Short = true });
18+
19+
if (exception.StackTrace != null)
20+
attachment.Fields.Add(new AttachmentField() { Title = "StackTrace", Value = exception.StackTrace, Short = false });
21+
22+
if (exception.InnerException != null)
23+
{
24+
attachment.Fields.Add(new AttachmentField() { Title = "InnerException Type", Value = exception.InnerException.GetType().FullName, Short = true });
25+
attachment.Fields.Add(new AttachmentField() { Title = "InnerException Message", Value = exception.InnerException.Message, Short = true });
26+
if (exception.InnerException.StackTrace != null)
27+
attachment.Fields.Add(new AttachmentField() { Title = "InnerException StackTrace", Value = exception.InnerException.StackTrace, Short = false });
28+
}
29+
30+
@this.Attachments.Add(attachment);
31+
}
32+
33+
}
34+
}

WooCode.Slack/WebHooks/WebHook.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Net;
35
using Newtonsoft.Json;
46

57
namespace WooCode.Slack.WebHooks
68
{
79
public static class WebHook
810
{
9-
static readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings();
11+
static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings();
12+
static readonly Dictionary<string, string> EscapeDictionary = new Dictionary<string, string>
13+
{
14+
{"&", "&amp;"},
15+
{"<", "&lt;"},
16+
{">", "&gt;"}
17+
};
18+
1019

1120
static WebHook()
1221
{
13-
_jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
14-
_jsonSerializerSettings.ContractResolver = new LowercaseContractResolver();
22+
JsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
23+
JsonSerializerSettings.ContractResolver = new LowercaseContractResolver();
1524
}
1625

1726
public static void Send(Message message)
1827
{
19-
var data = JsonConvert.SerializeObject(message, _jsonSerializerSettings);
20-
28+
var data = JsonConvert.SerializeObject(message, JsonSerializerSettings);
29+
data = EscapeDictionary.Aggregate(data, (current, escapeChar) => current.Replace(escapeChar.Key, escapeChar.Value));
30+
2131
using(var client = new WebClient())
2232
client.UploadStringAsync(new Uri(message.HookUrl), data);
2333
}

WooCode.Slack/WooCode.Slack.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
<Reference Include="System.Xml" />
4545
</ItemGroup>
4646
<ItemGroup>
47+
<Compile Include="SlashCommand.cs" />
4748
<Compile Include="WebHooks\Attachment.cs" />
4849
<Compile Include="WebHooks\AttachmentColors.cs" />
4950
<Compile Include="WebHooks\AttachmentField.cs" />
5051
<Compile Include="WebHooks\Message.cs" />
52+
<Compile Include="WebHooks\MessageExtensions.cs" />
5153
<Compile Include="WebHooks\WebHook.cs" />
5254
<Compile Include="LowercaseContractResolver.cs" />
5355
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)