Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
endurance committed Dec 22, 2023
1 parent a872185 commit 88c0599
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 136 deletions.
9 changes: 9 additions & 0 deletions src/appSecrets/getSlackToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SecretManagerServiceClient } from '@google-cloud/secret-manager';

export const getSlackToken = async () => {
const client = new SecretManagerServiceClient();
const [secret] = await client.accessSecretVersion({
name: 'projects/588063171007/secrets/SLACK_BOT_TOKEN/versions/latest',
});
return secret.payload.data.toString();
};
6 changes: 2 additions & 4 deletions src/frontends/discord/myDiscord.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Logger, Module } from '@nestjs/common';
import { DiscordPingService } from './services/discordPing.service';
import { NecordModule } from 'necord';
import { DiscordWowService } from './services/discordWow.service';
import { DiscordChatService } from './services/discordChat.service';
import { GcpModule } from '../../gcp/gcp.module';
import { NecordConfigService } from './services/necordConfig.service';
import { DiscordReadyService } from './services/discordReady.service';
import { DiscordReadyLoggingService } from './services/discordReadyLogging.service';
import { LangchainModule } from '../../ai/langchain /langchain/langchain.module';

@Module({
Expand All @@ -19,9 +18,8 @@ import { LangchainModule } from '../../ai/langchain /langchain/langchain.module'
],
providers: [
DiscordPingService,
DiscordWowService,
DiscordChatService,
DiscordReadyService,
DiscordReadyLoggingService,
Logger,
],
})
Expand Down
5 changes: 5 additions & 0 deletions src/frontends/discord/secrets/secrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const DISCORD_API_TOKEN =
'projects/588063171007/secrets/DISCORD_API_TOKEN/versions/latest';

export const DISCORD_API_DEV_TOKEN =
'projects/588063171007/secrets/DISCORD_API_DEV_TOKEN/versions/latest';
17 changes: 15 additions & 2 deletions src/frontends/discord/services/discordPing.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { Injectable } from '@nestjs/common';
import { Context, SlashCommand, SlashCommandContext } from 'necord';
import { DEV_GUILDS } from '../../../constants';

@Injectable()
export class DiscordPingService {
@SlashCommand({
name: 'hello',
description: 'Ping-Pong Command',
})
public async onPing(@Context() [interaction]: SlashCommandContext) {
console.log('hello');
public async handleHelloPingCommand(
@Context() [interaction]: SlashCommandContext,
) {
return interaction.reply({ content: 'Pong!' });
}

@SlashCommand({
name: 'wow',
description: 'Wow Command',
guilds: DEV_GUILDS,
})
public async handleWowSlashCommand(
@Context() [interaction]: SlashCommandContext,
) {
return interaction.reply({ content: 'Woweeeee!!!!' });
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable, Logger } from '@nestjs/common';
import { Context, On, Once, ContextOf } from 'necord';
import { Context, ContextOf, On, Once } from 'necord';
import { Client } from 'discord.js';

@Injectable()
export class DiscordReadyService {
private readonly logger = new Logger(DiscordReadyService.name);
export class DiscordReadyLoggingService {
private readonly logger = new Logger(DiscordReadyLoggingService.name);

public constructor(private readonly client: Client) {}

Expand Down
15 changes: 0 additions & 15 deletions src/frontends/discord/services/discordWow.service.ts

This file was deleted.

20 changes: 10 additions & 10 deletions src/frontends/discord/services/necordConfig.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ import { Injectable } from '@nestjs/common';
import { NecordModuleOptions } from 'necord';
import { IntentsBitField } from 'discord.js';
import { SecretsManagerService } from '../../../gcp/secretsManager.service';

const DISCORD_API_TOKEN =
'projects/588063171007/secrets/DISCORD_API_TOKEN/versions/latest';

const DISCORD_API_DEV_TOKEN =
'projects/588063171007/secrets/DISCORD_API_DEV_TOKEN/versions/latest';
import { DISCORD_API_DEV_TOKEN, DISCORD_API_TOKEN } from '../secrets/secrets';

@Injectable()
export class NecordConfigService {
constructor(private readonly sms: SecretsManagerService) {}

async createNecordOptions(): Promise<NecordModuleOptions> {
const secretName =
process.env.NODE_ENV === 'production'
? DISCORD_API_TOKEN
: DISCORD_API_DEV_TOKEN;
const secretName = this._createTokenByNodeEnv();

const [secret] = await this.sms.getManager().accessSecretVersion({
name: secretName,
Expand All @@ -28,4 +20,12 @@ export class NecordConfigService {
intents: [IntentsBitField.Flags.Guilds],
};
}

private _createTokenByNodeEnv() {
if (process.env.NODE_ENV === 'production') {
return DISCORD_API_TOKEN;
}

return DISCORD_API_DEV_TOKEN;
}
}
14 changes: 14 additions & 0 deletions src/frontends/slack/createSlackModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SlackModule } from 'nestjs-slack';
import { getSlackToken } from '../../appSecrets/getSlackToken';

export const createSlackModule = () => {
return SlackModule.forRootAsync({
useFactory: async () => {
return {
token: await getSlackToken(),
type: 'api',
defaultChannel: 'bot-playground',
};
},
});
};
15 changes: 4 additions & 11 deletions src/frontends/slack/mySlack.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { Module } from '@nestjs/common';
import { SlackModule } from 'nestjs-slack';
import { SlackConfigService } from './slackConfig.service';
import { GcpModule } from '../../gcp/gcp.module';
import { SlackTestService } from './slackTest.service';
import { createSlackModule } from './createSlackModule';

@Module({
// imports: [
// SlackModule.forRootAsync({
// imports: [GcpModule],
// useClass: SlackConfigService,
// }),
// GcpModule,
// ],
// providers: [SlackConfigService],
// exports: [SlackConfigService],
imports: [createSlackModule(), GcpModule],
providers: [SlackTestService],
})
export class MySlackModule {}
19 changes: 0 additions & 19 deletions src/frontends/slack/slackConfig.service.ts

This file was deleted.

84 changes: 12 additions & 72 deletions src/frontends/slack/slackTest.service.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,12 @@
// import {Injectable} from '@nestjs/common';
// import {Action, Command, Event, Message} from 'nestjs-slack-bolt';
// import {SlackActionMiddlewareArgs, SlackCommandMiddlewareArgs, SlackEventMiddlewareArgs,} from '@slack/bolt';
//
// @Injectable()
// export class SlackTestService {
// @Message('hi') //Handle a message event
// message({say}: SlackEventMiddlewareArgs) {
// say('Hello');
// }
//
// @Action('click') //Handle an action
// action({say}: SlackActionMiddlewareArgs) {
// say('click event received');
// }
//
// @Command('/list') // handle command
// command({say}: SlackCommandMiddlewareArgs) {
// say('/list command received');
// }
//
// @Event('app_home_opened')
// event({say}: SlackEventMiddlewareArgs) {
// say('app_open_event received');
// }
//
// @Shortcut('test_shortcut') //Handle a shortcut event
// async shortcut({shortcut, ack, client, logger}) {
// try {
// // Acknowledge shortcut request
// await ack();
//
// // Call the views.open method using one of the built-in WebClients
// const result = await client.views.open({
// trigger_id: shortcut.trigger_id,
// view: {
// type: 'modal',
// title: {
// type: 'plain_text',
// text: 'My App',
// },
// close: {
// type: 'plain_text',
// text: 'Close',
// },
// blocks: [
// {
// type: 'section',
// text: {
// type: 'mrkdwn',
// text: 'About the simplest modal you could conceive of :smile:\n\nMaybe <https://api.slack.com/reference/block-kit/interactive-components|*make the modal interactive*> or <https://api.slack.com/surfaces/modals/using#modifying|*learn more advanced modal use cases*>.',
// },
// },
// {
// type: 'context',
// elements: [
// {
// type: 'mrkdwn',
// text: 'Psssst this modal was designed using <https://api.slack.com/tools/block-kit-builder|*Block Kit Builder*>',
// },
// ],
// },
// ],
// },
// });
//
// logger.info(result);
// } catch (error) {
// logger.error(error);
// }
// }
// }
import { Injectable } from '@nestjs/common';
import { SlackService } from 'nestjs-slack';

@Injectable()
export class SlackTestService {
constructor(private service: SlackService) {}

async helloWorldMethod() {
await this.service.sendText('Hello world was sent!');
return 'hello world';
}
}
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SlackTestService } from './frontends/slack/slackTest.service';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const sts = await app.resolve(SlackTestService);
sts.helloWorldMethod();

await app.listen(process.env.PORT || 3000);
}

Expand Down

0 comments on commit 88c0599

Please sign in to comment.