Skip to content

Commit 6268d7e

Browse files
committed
Add rollbar and error on slack response failure
1 parent e0535b9 commit 6268d7e

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

Api/v0/Controllers/HamAlertController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public async Task<IActionResult> Alert(string userId, [FromQuery] string fullCal
2020
{
2121
await Slack.PostMessage(new SlackDotNet.Payloads.ChatMessage
2222
{
23-
User = userId,
23+
Channel = userId,
2424
Text = $"HamAlert: {callSign} {frequency} {band} {mode} {time}"
2525
});
2626

Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
using Microsoft.AspNetCore.Http;
1111
using Microsoft.Extensions.Configuration;
1212
using Microsoft.Extensions.DependencyInjection;
13+
using Microsoft.Extensions.Logging;
14+
using RollbarDotNet.Configuration;
15+
using RollbarDotNet.Core;
16+
using RollbarDotNet.Logger;
1317
using SlackDotNet;
1418
using SlackDotNet.Options;
1519

@@ -31,9 +35,11 @@
3135
.AddSingleton<StallmanCommand>()
3236
.AddSingleton<Client>()
3337
.AddSingleton<ICommandService, CommandService>()
38+
.Configure<RollbarOptions>(options => configuration.GetSection("Rollbar").Bind(options))
3439
.Configure<DiscordOptions>(o => configuration.GetSection("Discord").Bind(o))
3540
.Configure<SlackSocketOptions>(o => configuration.GetSection("SlackSocket").Bind(o))
3641
.Configure<SlackOptions>(o => configuration.GetSection("Slack").Bind(o))
42+
.AddRollbarWeb()
3743
.AddHangfire(config => config.UseRedisStorage(configuration.GetConnectionString("Redis")))
3844
.AddHangfireServer()
3945
.AddAuthorization()
@@ -46,7 +52,8 @@
4652
.AddHostedService<HangfireHostedService>();
4753

4854
var app = builder.Build();
49-
55+
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
56+
loggerFactory.AddRollbarDotNetLogger(app.Services);
5057
app
5158
.UseForwardedHeaders()
5259
.UseHsts()
@@ -67,6 +74,7 @@
6774
}
6875
}
6976
})
77+
.UseRollbarExceptionHandler()
7078
.UseAuthentication()
7179
.UseAuthorization()
7280
.UseRouting()

SlackDotNet/Slack.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public async Task<bool> PostMessage(ChatMessage message, bool ephemeral = false)
5858
var response = await $"https://slack.com/api/chat.{endpoint}"
5959
.WithHeader("Authorization", "Bearer " + Options.OauthToken)
6060
.PostJsonAsync(message);
61+
if (response.StatusCode != 200)
62+
{
63+
throw new Exception($"Error posting message to Slack: {await response.GetStringAsync()}");
64+
}
6165

6266
return true;
6367
}

appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"Microsoft.Hosting.Lifetime": "Information"
1010
}
1111
},
12+
"Rollbar": {
13+
"AccessToken": "#{rollbar.accesstoken}",
14+
"Environment": "#{rollbar.environment}"
15+
},
1216
"AllowedHosts": "*",
1317
"Slack": {
1418
"OauthToken": "#{slack.oauthtoken}",

devanewbot.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.8.5" />
1515
<PackageReference Include="HtmlAgilityPack" Version="1.11.42" />
1616
<PackageReference Include="Magick.NET-Q16-HDRI-AnyCPU" Version="11.3.0" />
17+
<PackageReference Include="RollbarDotNet" Version="0.7.0" />
1718
<PackageReference Include="WebSocketExtensions" Version="2.1.9" />
1819
</ItemGroup>
1920

0 commit comments

Comments
 (0)