Skip to content

Commit

Permalink
slack is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
endurance committed Jan 2, 2024
1 parent 14b12a3 commit dda2bf2
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 1,074 deletions.
1,145 changes: 96 additions & 1,049 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/mapped-types": "*",
"@slack/bolt": "^3.17.0",
"nestjs-slack-bolt": "^1.1.0",
"@nestjs/config": "^3.1.1",
"bufferutil": "^4.0.8",
"discord.js": "^14.14.1",
Expand Down
18 changes: 18 additions & 0 deletions src/interfaces/chat/slack/bolt/slackAppFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ConfigService } from '@nestjs/config';
import { App } from '@slack/bolt';
import { AppOptions } from '@slack/bolt/dist/App';

export const slackServiceFactory = {
provide: App,
inject: [ConfigService],
useFactory: (configService: ConfigService, options: AppOptions) => {
const opts: AppOptions = {
token: configService.get('SLACK_BOT_TOKEN'),
signingSecret: configService.get('SLACK_SIGNING_SECRET'),
socketMode: Boolean(configService.get<boolean>('SLACK_SOCKET_MODE')),
appToken: configService.get('SLACK_APP_TOKEN'),
...options,
};
return new App(opts);
},
};
15 changes: 15 additions & 0 deletions src/interfaces/chat/slack/bolt/slackBolt.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { slackServiceFactory } from './slackAppFactory';
import { Module, OnModuleInit } from '@nestjs/common';
import { App } from '@slack/bolt';

@Module({
providers: [slackServiceFactory],
exports: [slackServiceFactory],
})
export class SlackBoltModule implements OnModuleInit {
constructor(private readonly app: App) {}

onModuleInit() {
this.app.start();
}
}
12 changes: 8 additions & 4 deletions src/interfaces/chat/slack/decorators/discovery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DiscoveryService } from '@golevelup/nestjs-discovery';
import { Injectable } from '@nestjs/common';
import { SlackService } from 'nestjs-slack-bolt/dist/services/slack.service';
import {
SLACK_COMMAND_METADATA_KEY,
SlackCommandArgs,
Expand All @@ -9,12 +8,13 @@ import {
SLACK_MESSAGE_METADATA_KEY,
SlackMessageArgs,
} from './slackMessage.decorator';
import { App } from '@slack/bolt';

@Injectable()
export class SlackCommandMethodDiscovery {
constructor(
private readonly _discoveryService: DiscoveryService,
private readonly _client: SlackService,
private readonly _client: App,
) {}

public async bindSlackCommands() {
Expand All @@ -29,7 +29,7 @@ export class SlackCommandMethodDiscovery {
const handler = result.discoveredMethod.handler;
const that = result.discoveredMethod.parentClass.instance;
const boundHandler = handler.bind(that);
this._client.app.command(command, boundHandler);
this._client.command(command, boundHandler);
});
}

Expand All @@ -45,7 +45,11 @@ export class SlackCommandMethodDiscovery {
const handler = result.discoveredMethod.handler;
const that = result.discoveredMethod.parentClass.instance;
const boundHandler = handler.bind(that);
this._client.app.message(message, boundHandler);
if (message) {
this._client.message(message, boundHandler);
return;
}
this._client.message(boundHandler);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface SlackMessageArgs {
message: string;
}

export function SlackMessageCommand(message?: string) {
export function SlackMessage(message?: string) {
return SetMetadata(SLACK_MESSAGE_METADATA_KEY, {
message,
} as SlackMessageArgs);
Expand Down
9 changes: 2 additions & 7 deletions src/interfaces/chat/slack/slackInterface.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Module, OnModuleInit } from '@nestjs/common';
import { GcpModule } from '../../../shared/gcp/gcp.module';
import { SlackModule } from 'nestjs-slack-bolt';
import { SlackInterfaceService } from './slackInterface.service';
import { SlackCommandMethodDiscovery } from './decorators/discovery';
import { DiscoveryModule } from '@golevelup/nestjs-discovery';
import { ChatbotCoreModule } from '../../../shared/chatbot-core/chatbotCore.module';
import { SlackBoltModule } from './bolt/slackBolt.module';

@Module({
imports: [
DiscoveryModule,
ChatbotCoreModule,
GcpModule,
SlackModule.forRoot(),
],
imports: [SlackBoltModule, DiscoveryModule, ChatbotCoreModule, GcpModule],
providers: [SlackInterfaceService, SlackCommandMethodDiscovery],
})
export class SlackInterfaceModule implements OnModuleInit {
Expand Down
44 changes: 38 additions & 6 deletions src/interfaces/chat/slack/slackInterface.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,48 @@ import { Injectable } from '@nestjs/common';
import { SlackCommand } from './decorators/slackCommand.decorator';
import { ChatbotService } from '../../../shared/chatbot-core/chatbot.service';
import { v4 } from 'uuid';
import { SlackCommandMiddlewareArgs } from '@slack/bolt';
import {
App,
GenericMessageEvent,
SlackCommandMiddlewareArgs,
SlackEventMiddlewareArgs,
} from '@slack/bolt';
import { SlackMessage } from './decorators/slackMessage.decorator';

@Injectable()
export class SlackInterfaceService {
constructor(private readonly _chatbotCore: ChatbotService) {}
constructor(
private readonly _chatbotCore: ChatbotService,
private readonly _app: App,
) {}

// @SlackMessageCommand()
// async message(args: SlackEventMiddlewareArgs) {
// console.log('message received');
// }
@SlackMessage()
async handleBotMessages({
say,
message,
}: SlackEventMiddlewareArgs<'message'>) {
if (
message.subtype === undefined ||
message.subtype === 'bot_message' ||
message.subtype === 'file_share' ||
message.subtype === 'thread_broadcast'
) {
const id = message.ts;
const { text } = message as GenericMessageEvent;
await this._chatbotCore.createChatbot(id);
const stream = this._chatbotCore.streamResponse(id, text, {
topic: '',
});
const response = await say({ text: 'incoming', thread_ts: message.ts });
for await (const block of stream) {
await this._app.client.chat.update({
text: block.data,
ts: response.ts,
channel: response.channel,
});
}
}
}

@SlackCommand('/help') // handle command
async help({ command, ack, say, respond }: SlackCommandMiddlewareArgs) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/main.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common';
import { MyDiscordModule } from '../interfaces/chat/discord/myDiscord.module';
import { SlackInterfaceModule } from '../interfaces/chat/slack/slackInterface.module';
import { MainController } from './main.controller';
import { ConfigModule } from '@nestjs/config';
import { MyDiscordModule } from '../interfaces/chat/discord/myDiscord.module';

@Module({
imports: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export class ChainBuilderService {
async createPrompt() {
if (!this.promptTemplate) {
this._logger.log('Creating prompt...');
this.promptTemplate = ChatPromptTemplate.fromMessages([
const promptStructure = [
['system', 'You were having a conversation with a human about {topic}'],
['human', '{text}'],
]);
];
// @ts-ignore
this.promptTemplate = ChatPromptTemplate.fromMessages(promptStructure);
}

this._logger.log('Returning prompt: ' + this.promptTemplate);
return this.promptTemplate;
}

Expand All @@ -42,7 +42,6 @@ export class ChainBuilderService {
const model = await this.createModel(modelName);
const promptTemplate = await this.createPrompt();
const chain = promptTemplate.pipe(model);
this._logger.log('Created chain: ' + chain);
return chain;
}
}

0 comments on commit dda2bf2

Please sign in to comment.