-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlackToWorkflow.cs
135 lines (119 loc) · 5.47 KB
/
SlackToWorkflow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Rock.Model;
namespace com.shepherdchurch.WebhookToWorkflow
{
class SlackToWorkflow : GenericWebhook
{
/// <summary>
/// The GUID that identifies the DefinedType for the Slack interface.
/// </summary>
/// <returns>A Guid object</returns>
protected override Guid DefinedTypeGuid()
{
return new Guid( "751e5eb6-a02d-4836-9382-7eac6280fa4c" );
}
/// <summary>
/// Check if this DefinedValue's filters apply to this request.
/// </summary>
/// <param name="hook">The DefinedValue to consider for filtering.</param>
/// <returns>true if the DefinedValue should be used, false if not.</returns>
protected override bool IsHookValidForRequest( DefinedValue hook )
{
if ( !base.IsHookValidForRequest( hook ) )
{
return false;
}
//
// Check if the text coming from slack matches the text filter. If the
// filter begins with ^ and ends with $ it is treated as a regex match.
//
string text = hook.GetAttributeValue( "Text" );
if ( !string.IsNullOrWhiteSpace( text ) )
{
if ( text.StartsWith( "^" ) && text.EndsWith( "^" ) )
{
if ( !Regex.IsMatch( HttpContext.Request.Form["text"], text, RegexOptions.IgnoreCase ) )
{
return false;
}
}
else
{
if ( HttpContext.Request.Form["text"].IndexOf( text, StringComparison.OrdinalIgnoreCase ) < 0 )
{
return false;
}
}
}
return true;
}
/// <summary>
/// Populate all the Slack specific attributes into the workflow.
/// </summary>
/// <param name="workflow">The workflow whose attributes need to be filled in.</param>
/// <param name="hook">The DefinedValue that matched this request.</param>
protected override void PopulateWorkflowAttributes( Workflow workflow, DefinedValue hook )
{
base.PopulateWorkflowAttributes( workflow, hook );
workflow.SetAttributeValue( "Team", HttpContext.Request.Form["team_domain"] );
workflow.SetAttributeValue( "Channel", HttpContext.Request.Form["channel_name"] );
workflow.SetAttributeValue( "Username", HttpContext.Request.Form["user_name"] );
workflow.SetAttributeValue( "Text", HttpContext.Request.Form["text"] );
workflow.SetAttributeValue( "Trigger", HttpContext.Request.Form["trigger_word"] );
}
/// <summary>
/// Send a JSON response to the Slack system if one was provided by the
/// workflow. If the Response attribute value can be convert into JSON then
/// it will be sent, otherwise it is assumed to be plain text and it is wrapped
/// in a JSON object. If the JSON does not contain a "username" property and
/// a "Username" attribute for the DefinedValue has been defined then it will
/// be inserted into the JSON object.
/// </summary>
/// <param name="workflow">The workflow to check for a Response value in.</param>
/// <param name="hook">The DefinedValue which identifies this webhook.</param>
protected override void SendWorkflowResponse( Workflow workflow, DefinedValue hook )
{
string response = workflow.GetAttributeValue( "Response" );
HttpContext.Response.ContentType = "application/json";
if ( !string.IsNullOrEmpty( response ) )
{
try
{
var json = JsonConvert.DeserializeObject<Dictionary<string, object>>( response );
AddSlackUsernameAndIcon( hook, json );
HttpContext.Response.Write( JsonConvert.SerializeObject( json ) );
}
catch
{
Dictionary<string, object> json = new Dictionary<string, object>();
json.Add( "text", response );
AddSlackUsernameAndIcon( hook, json );
HttpContext.Response.Write( JsonConvert.SerializeObject( json ) );
}
}
}
/// <summary>
/// Attach the default username and icon's to the outgoing Slack Message if they have not
/// already been defined by the workflow.
/// </summary>
/// <param name="hook">The DefinedValue which identifies this webhook.</param>
/// <param name="json">the data that contains the slack message.</param>
private void AddSlackUsernameAndIcon( DefinedValue hook, Dictionary<string, object> json )
{
if ( !json.ContainsKey( "username" ) && !string.IsNullOrWhiteSpace( hook.GetAttributeValue( "Username" ) ) )
{
json.Add( "username", hook.GetAttributeValue( "Username" ) );
}
if ( !json.ContainsKey( "icon_url" ) && !json.ContainsKey( "icon_emojoi" ) )
{
if ( !string.IsNullOrWhiteSpace( hook.GetAttributeValue( "Icon" ) ) )
{
json.Add( "icon_url", hook.GetAttributeValue( "Icon" ) );
}
}
}
}
}