Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add boilerplate AprilBot code #22

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"import/prefer-default-export": "off",
"import/no-default-export": "error",
"@typescript-eslint/prefer-readonly-parameter-types": "off",
"class-methods-use-this": "off",
"@typescript-eslint/class-methods-use-this": [
"error",
{
"ignoreOverrideMethods": true
}
],
"import/order": [
"error",
{
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
},
"dependencies": {
"@slack/socket-mode": "^1.3.4",
"@slack/web-api": "^6.1.0",
"@slack/web-api": "^7.0.2",
"pino": "^8.19.0",
"pino-pretty": "^11.0.0",
"require-env-variable": "^4.0.2"
}
}
43 changes: 43 additions & 0 deletions src/AprilBot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { GenericAprilBot } from './GenericAprilBot';
import { getLogger } from './logger';
import type {
SocketModeEventPayload,
ReactionAddedEvent,
ReactionRemovedEvent,
MessageEvent,
} from './types';

const logger = getLogger();

class AprilBot extends GenericAprilBot {
constructor(userToken: string, appToken: string) {
super(userToken, appToken);
logger.debug('AprilBot is constructed');
}

protected override handleMessage = ({
ack,
event,
}: SocketModeEventPayload<MessageEvent>) => {
ack();
logger.debug(event, 'handleMessage');
};

protected override handleReactionAdded = ({
ack,
event,
}: SocketModeEventPayload<ReactionAddedEvent>) => {
ack();
logger.debug(event, 'handleReactionAdded');
};

protected override handleReactionRemoved = ({
ack,
event,
}: SocketModeEventPayload<ReactionRemovedEvent>) => {
ack();
logger.debug(event, 'handleReactionRemoved');
};
}

export { AprilBot };
66 changes: 66 additions & 0 deletions src/GenericAprilBot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { SocketModeClient } from '@slack/socket-mode';
import { WebClient } from '@slack/web-api';
import { getLogger } from './logger';
import type { Member } from '@slack/web-api/dist/types/response/UsersListResponse';
import type {
SocketModeEventPayload,
ReactionAddedEvent,
ReactionRemovedEvent,
MessageEvent,
} from './types';

const logger = getLogger();

abstract class GenericAprilBot {
private readonly userToken: string;

private readonly appToken: string;

protected readonly socketModeClient: SocketModeClient;

protected readonly webClient: WebClient;

protected users: Member[] = [];

constructor(userToken: string, appToken: string) {
this.userToken = userToken;
this.appToken = appToken;
this.socketModeClient = new SocketModeClient({
appToken: this.appToken,
});
this.webClient = new WebClient(this.userToken);
}

public async start(): Promise<void> {
this.users = await this.loadUsers();
this.socketModeClient.on('message', this.handleMessage);
this.socketModeClient.on('reaction_added', this.handleReactionAdded);
this.socketModeClient.on('reaction_removed', this.handleReactionRemoved);
await this.socketModeClient.start();
}

private async loadUsers(): Promise<Member[]> {
logger.debug('Loading users...');
const result = await this.webClient.users.list({});
if (!result.ok || result.members === undefined) {
logger.error(result.error);
throw new Error('Unable to load users.');
}
logger.debug(`${result.members?.length} Users loaded.`);
return result.members;
}

protected abstract handleMessage: (
args: SocketModeEventPayload<MessageEvent>,
) => void;

protected abstract handleReactionAdded: (
args: SocketModeEventPayload<ReactionAddedEvent>,
) => void;

protected abstract handleReactionRemoved: (
args: SocketModeEventPayload<ReactionRemovedEvent>,
) => void;
}

export { GenericAprilBot };
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { requireEnv } from 'require-env-variable';
import { AprilBot } from './AprilBot';
import { getLogger } from './logger';

const logger = getLogger();

const { SOCKET_TOKEN, V2_BOT_TOKEN } = requireEnv(
'SOCKET_TOKEN',
'V2_BOT_TOKEN',
);

const aprilBot = new AprilBot(V2_BOT_TOKEN, SOCKET_TOKEN);
aprilBot
.start()
.then(() => {
logger.info('April has started!');
})
.catch((error) => {
logger.error(error);
});
14 changes: 14 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pino from 'pino';

const getLogger = () =>
pino({
level: process.env.LOG_LEVEL ?? 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
},
});

export { getLogger };
31 changes: 31 additions & 0 deletions src/types/MessageEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
interface MessageEvent {
user: string;
type: string;
ts: string;
client_msg_id: string;
text: string;
team: string;
thread_ts?: string;
blocks: MessageBlock[];
channel: string;
event_ts: string;
channel_type: string;
}

interface MessageBlock {
type: string;
block_id: string;
elements: BlockElement[];
}

interface BlockElement {
type: string[];
elements: BlockElementElement[];
}

interface BlockElementElement {
type: string;
text: string;
}

export { MessageEvent };
14 changes: 14 additions & 0 deletions src/types/ReactionAddedEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface ReactionAddedEvent {
type: string;
user: string;
reaction: string;
item: {
type: string;
channel: string;
ts: string;
};
item_user: string;
event_ts: string;
}

export { ReactionAddedEvent };
14 changes: 14 additions & 0 deletions src/types/ReactionRemovedEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface ReactionRemovedEvent {
type: string;
user: string;
reaction: string;
item: {
type: string;
channel: string;
ts: string;
};
item_user: string;
event_ts: string;
}

export { ReactionRemovedEvent };
6 changes: 6 additions & 0 deletions src/types/SocketModeEventPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface SocketModeEventPayload<T> {
event: T;
ack: () => void;
}

export { SocketModeEventPayload };
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './MessageEvent';
export * from './ReactionAddedEvent';
export * from './ReactionRemovedEvent';
export * from './SocketModeEventPayload';
Loading