Skip to content

Commit cbe2ef6

Browse files
committed
Fix QRMBot Command
1 parent 89b18db commit cbe2ef6

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ EXPOSE 5010
77
ENV ASPNETCORE_URLS=http://+:5010
88

99
RUN apt-get update && \
10-
apt-get install wget unzip -y
10+
apt-get install wget unzip curl perl perl-base perl-modules libclone-perl libdate-manip-perl libdatetime-format-strptime-perl libdatetime-perl libjson-perl libmath-bigint-perl libmath-round-perl libswitch-perl libtext-csv-perl liburi-perl -y
1111
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
1212
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
1313
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app

Services/CommandService.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public CommandService(
3434
// I choose the lazy solution, I choose... this.
3535
Commands.AddRange(Directory.EnumerateFiles("qrmbot/lib")
3636
.Select(f => new QrmBotCommand(
37-
f.Replace(".pl", string.Empty),
37+
f.Split("/").Last().Replace(".pl", string.Empty),
3838
serviceProvider.GetRequiredService<Slack>(),
3939
serviceProvider.GetRequiredService<IConfiguration>(),
4040
serviceProvider.GetRequiredService<ILogger<QrmBotCommand>>()))
@@ -54,13 +54,20 @@ public CommandService(
5454
/// <returns></returns>
5555
public async Task HandleMessage(WebhookMessage message, string commandSuffix = "")
5656
{
57-
// Remove leading forward-slash and any optional suffix
58-
var messageCommand = message.Command.Substring(1);
59-
messageCommand = messageCommand.Substring(0, messageCommand.Length - commandSuffix.Length);
57+
try
58+
{
59+
// Remove leading forward-slash and any optional suffix
60+
var messageCommand = message.Command.Substring(1);
61+
messageCommand = messageCommand.Substring(0, messageCommand.Length - commandSuffix.Length);
6062

61-
var command = Commands.Find((c) => c.CommandText == messageCommand);
63+
var command = Commands.Find((c) => c.CommandText == messageCommand);
6264

63-
await command.Execute(message);
65+
await command.Execute(message);
66+
}
67+
catch (Exception exception)
68+
{
69+
Logger.LogError(exception, "Error running command: {}", message.Command);
70+
}
6471
}
6572

6673
/// <summary>
@@ -75,6 +82,14 @@ public async Task HandleInteractive(InteractiveMessage message)
7582
{
7683
var command = Commands.Find((c) => c.CommandText == message.CallbackId);
7784

78-
await command.ExecuteInteractive(message);
85+
try
86+
{
87+
Logger.LogError("Test: {}", message.CallbackId);
88+
await command.ExecuteInteractive(message);
89+
}
90+
catch (Exception exception)
91+
{
92+
Logger.LogError(exception, "Error running command: {}", message.CallbackId);
93+
}
7994
}
8095
}

Services/QrmBotCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public QrmBotCommand(string command, Slack slack, IConfiguration configuration,
2121

2222
protected override async Task HandleMessage(WebhookMessage webhookMessage)
2323
{
24+
var slackUser = await Slack.GetUser(webhookMessage.UserId);
2425
var perlStartInfo = new ProcessStartInfo(@"perl")
2526
{
2627
// MAKE SURE WE ESCAPE DOUBLE QUOTES DON'T RUIN MY DAY I SWEAR
27-
Arguments = $"{Directory}qrmbot/lib/{CommandText} \"{webhookMessage.Text.Replace("\"", "\\\"")}\"",
28+
Arguments = $"{Directory}lib/{CommandText} \"{webhookMessage.Text.Replace("\"", "\\\"")}\"",
2829
UseShellExecute = false,
2930
RedirectStandardOutput = true,
3031
RedirectStandardError = true,
@@ -43,6 +44,8 @@ await Slack.PostMessage(new ChatMessage
4344
{
4445
Channel = webhookMessage.ChannelId,
4546
Text = output,
47+
Username = slackUser.Profile.DisplayName,
48+
IconUrl = slackUser.Profile.ImageOriginal
4649
});
4750
}
4851
}

0 commit comments

Comments
 (0)