Skip to content

Commit

Permalink
Add support for triggering webhooks via command line
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
amoeba committed May 3, 2019
1 parent 47b2bb4 commit bdd92ec
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
91 changes: 91 additions & 0 deletions PluginCore.Events.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using Decal.Adapter;
using Decal.Adapter.Wrappers;
Expand Down Expand Up @@ -111,5 +112,95 @@ private void CharacterFilter_Death(object sender, DeathEventArgs e)
Util.LogError(ex);
}
}

[BaseEvent("CommandLineText")]
void Core_CommandLineText(object sender, ChatParserInterceptEventArgs e)
{
try
{
string text = e.Text;

if (text.Length <= 0)
{
return;
}

// Split into tokens for easier processing
string[] tokens = text.Split(' ');
string command = tokens[0].ToLower();

if (command.StartsWith("@towncrier") ||
command.StartsWith("@tc") ||
command.StartsWith("/towncrier") ||
command.StartsWith("/tc"))
{
ProcessCommand(tokens);
}
}
catch (Exception ex)
{
Util.LogError(ex);
}
}

void ProcessCommand(string[] tokens)
{
if (tokens.Length == 0)
{
return;
}

// Show help if no args are passed or help is passed
if (tokens.Length < 2 || tokens[1].ToLower() == "help")
{
PrintCommandLineHelp();

return;
}

if (tokens[1].ToLower() == "trigger")
{
if (tokens.Length < 4)
{
PrintCommandLineHelp();
}

string name = tokens[2];

// Try to find the webhook first by name and warn if not found
List<Webhook> matched = Webhooks.FindAll(webhook => webhook.Name == name);

if (matched.Count == 0)
{
Util.WriteToChat("Webhook with name '" + name + "' not found.");

return;
}

// Slice the array so we can concatenate the message portion
string[] rest = new string[tokens.Length - 3];
Array.Copy(tokens, 3, rest, 0, tokens.Length - 3);
string message = string.Join(" ", rest);

if (message.Length == 0)
{
Util.WriteToChat("Can't trigger webhook '" + name + "' with an empty message.");
PrintCommandLineHelp();
}

Util.WriteToChat("Triggering webhook '" + name + "' with message '" + message + "'");

TriggerWebhook(name, message);
}
else
{
PrintCommandLineHelp();
}
}

void PrintCommandLineHelp()
{
Util.WriteToChat("Trigger webhooks via '@towncrier trigger ${webhookname} ${message}'. You can use Variables.");
}
}
}
17 changes: 17 additions & 0 deletions PluginCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ private void DisposeAllTimers()
}
}

private void TriggerWebhook(string name, string message)
{
try
{
List<Webhook> matched = Webhooks.FindAll(webhook => webhook.Name == name);

foreach (Webhook webhook in matched)
{
webhook.Send(new WebhookMessage(message));
}
}
catch (Exception ex)
{
Util.LogError(ex);
}
}

private void TriggerWebhooksForEvent(string evt, string eventMessage)
{
try
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.1")]
[assembly: AssemblyFileVersion("0.2.1")]
[assembly: AssemblyVersion("0.2.2")]
[assembly: AssemblyFileVersion("0.2.2")]
6 changes: 6 additions & 0 deletions WebhookMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class WebhookMessage
public string MessageFormatString;
public string EventMessage;

public WebhookMessage(string message)
{
MessageFormatString = message;
EventMessage = "";
}

public WebhookMessage(string message, string eventMessage)
{
MessageFormatString = message;
Expand Down

0 comments on commit bdd92ec

Please sign in to comment.