Skip to content

Commit 150da25

Browse files
committed
Added DM to bot /gpt generate
1 parent 0818839 commit 150da25

13 files changed

+622
-342
lines changed

Slack-GPT-Socket/BotInfo/SlackBotInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using SlackNet;
12
using SlackNet.WebApi;
23

34
namespace Slack_GPT_Socket;

Slack-GPT-Socket/BotInfo/SlackBotInfoApplicationBuilder.cs

+19-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,26 @@ public static IServiceCollection AddSlackBotInfo(this IServiceCollection builder
2525
/// <returns></returns>
2626
public static IApplicationBuilder UseSlackBotInfo(this IApplicationBuilder builder)
2727
{
28+
var task = UseSlackBotInfoAsync(builder);
29+
task.Wait();
30+
return builder;
31+
}
32+
33+
private static async Task UseSlackBotInfoAsync(IApplicationBuilder builder)
34+
{
35+
var logger = builder.ApplicationServices.GetRequiredService<ILogger<SlackBotInfo>>();
2836
var slackApiClient = builder.ApplicationServices.GetRequiredService<ISlackApiClient>();
2937
var slackBotInfo = builder.ApplicationServices.GetRequiredService<SlackBotInfo>();
30-
var botInfoTask = slackApiClient.Auth.Test();
31-
botInfoTask.Wait();
32-
slackBotInfo.BotInfo = botInfoTask.Result;
33-
return builder;
38+
39+
Task.WaitAll(new[]
40+
{
41+
SetBotInfo(slackApiClient, slackBotInfo, logger),
42+
});
43+
}
44+
45+
private static async Task SetBotInfo(ISlackApiClient slackApiClient, SlackBotInfo slackBotInfo, ILogger<SlackBotInfo> logger)
46+
{
47+
var botInfoTask = await slackApiClient.Auth.Test();
48+
slackBotInfo.BotInfo = botInfoTask;
3449
}
3550
}

Slack-GPT-Socket/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
builder.Services.AddOptions<ApiSettings>().Bind(builder.Configuration.GetSection("Api"));
1313
builder.Services.Configure<GptCommands>(builder.Configuration.GetSection("GptCommands"));
1414
builder.Services.Configure<GptDefaults>(builder.Configuration.GetSection("GptDefaults"));
15+
builder.Services.Configure<SlackSettings>(builder.Configuration.GetSection("Slack"));
1516

1617
builder.Services.AddSingleton<GptClient>();
1718
builder.Services.AddSingleton<GptCustomCommands>();
@@ -25,6 +26,7 @@
2526
.UseApiToken(settings.SlackBotToken)
2627
.UseAppLevelToken(settings.SlackAppToken)
2728
.RegisterEventHandler<AppMention, SlackMentionHandler>()
29+
.RegisterEventHandler<MessageEvent, SlackMessageHandler>()
2830
.RegisterSlashCommandHandler<SlackCommandHandler>("/gpt")
2931
);
3032

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Slack_GPT_Socket.Settings;
2+
3+
/// <summary>
4+
/// Settings for the Slack API
5+
/// </summary>
6+
public class SlackSettings
7+
{
8+
/// <summary>
9+
/// Only mentions will trigger the bot outside of bot's owned channel.
10+
/// Otherwise he will respond to any message, in a thread where he was mentioned.
11+
/// </summary>
12+
public bool OnlyMentionsOutsideBotChannel { get; set; } = true;
13+
}

Slack-GPT-Socket/SlackHandlers/Command/CommandManager.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using Slack_GPT_Socket.GptApi.ParameterResolvers;
33
using Slack_GPT_Socket.Settings;
44
using Slack_GPT_Socket.Utilities.LiteDB;
5+
using SlackNet;
56
using SlackNet.Interaction;
7+
using ILogger = Microsoft.Extensions.Logging.ILogger;
68

79
namespace Slack_GPT_Socket.Command;
810

@@ -13,20 +15,29 @@ public class CommandManager
1315
/// <summary>
1416
/// Initializes the command manager.
1517
/// </summary>
18+
/// <param name="slackSettings"></param>
1619
/// <param name="customCommands"></param>
1720
/// <param name="botInfo"></param>
1821
/// <param name="userCommandDb"></param>
1922
/// <param name="gptDefaults"></param>
2023
/// <param name="log"></param>
24+
/// <param name="slackApiClient"></param>
25+
/// <param name="gptClient"></param>
2126
public CommandManager(
27+
ISlackApiClient slackApiClient,
28+
GptClient gptClient,
29+
SlackSettings slackSettings,
2230
GptCustomCommands customCommands,
2331
SlackBotInfo botInfo,
2432
IUserCommandDb userCommandDb,
2533
GptDefaults gptDefaults,
2634
ILogger log)
2735
{
2836
var parameterManager = new ParameterManager(customCommands, gptDefaults, userCommandDb);
37+
var messageHandler = new SlackMessageEventBaseHandler(slackApiClient, log, gptClient, botInfo,
38+
slackSettings);
2939

40+
AddCommandStrategy(new GenerateCommandStrategy(messageHandler));
3041
AddCommandStrategy(new HelpCommandStrategy(gptDefaults, botInfo, customCommands, userCommandDb,
3142
parameterManager));
3243
AddCommandStrategy(new StatusCommandStrategy());
@@ -42,7 +53,7 @@ public Task<SlashCommandResponse> Execute(SlashCommand command)
4253
if (strategy.CanHandle(command)) return strategy.Execute(command);
4354
}
4455

45-
return Task.FromResult(CommandStrategyUtils.SlashCommandResponse("Command not found."));
56+
return Task.FromResult(CommandStrategyUtils.SlashCommandResponse("Command not found. Type /gpt help or /gpt generate [prompt] to get started."));
4657
}
4758

4859
private void AddCommandStrategy(ICommandStrategy commandStrategy)

Slack-GPT-Socket/SlackHandlers/Command/CommandsCommandStrategy.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task<SlashCommandResponse> Execute(SlashCommand command)
4242
return HelpCommand(command, restOfCommand);
4343
}
4444

45-
return CommandStrategyUtils.SlashCommandResponse("Command not found.");
45+
return CommandStrategyUtils.SlashCommandResponse("Command not found. Type /help or /generate to get started.");
4646
}
4747

4848
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using SlackNet.Interaction;
2+
3+
namespace Slack_GPT_Socket.Command;
4+
5+
public class GenerateCommandStrategy : ICommandStrategy
6+
{
7+
private SlackMessageEventBaseHandler _handler;
8+
9+
public GenerateCommandStrategy(SlackMessageEventBaseHandler handler)
10+
{
11+
_handler = handler;
12+
}
13+
14+
public string Command => "generate";
15+
public async Task<SlashCommandResponse> Execute(SlashCommand command)
16+
{
17+
command.Text = command.Text.Substring(Command.Length).Trim();
18+
19+
await _handler.CommandHandler(command);
20+
21+
return CommandStrategyUtils.SlashCommandResponse(SlackLoadingMessage.GetRandomLoadingMessage());
22+
}
23+
}

Slack-GPT-Socket/SlackHandlers/Command/HelpCommandStrategy.cs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ private string GeneralHelpText(SlashCommand command)
7575
sb.AppendLine();
7676
sb.AppendLine("You can also use the following commands:");
7777
sb.AppendLine(" - `/gpt help` - Display this help");
78+
sb.AppendLine(" - `/gpt generate <prompt>` - Generate text from the prompt. Very limited, please use chat channel or mention instead.");
7879
sb.AppendLine(" - `/gpt status` - Get the status of the bot");
7980
sb.AppendLine(" - `/gpt commands` - List all commands");
8081
sb.AppendLine(" - `/gpt whatsNew` - List the latest changes");

Slack-GPT-Socket/SlackHandlers/SlackCommandHandler.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ public class SlackCommandHandler : ISlashCommandHandler
1818
public SlackCommandHandler(
1919
GptCustomCommands customCommands,
2020
SlackBotInfo botInfo,
21+
ISlackApiClient slackApiClient,
22+
GptClient gptClient,
2123
IUserCommandDb userCommandDb,
2224
IOptions<GptDefaults> gptDefaults,
25+
IOptions<SlackSettings> slackSettings,
2326
ILogger<SlackCommandHandler> log)
2427
{
2528
_commandManager =
26-
new CommandManager(customCommands, botInfo, userCommandDb, gptDefaults.Value, log);
29+
new CommandManager(slackApiClient, gptClient, slackSettings.Value, customCommands, botInfo, userCommandDb, gptDefaults.Value, log);
2730
}
2831

2932

0 commit comments

Comments
 (0)