Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix/convert to typescript #6

Merged
merged 5 commits into from
Jul 3, 2022
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
node_modules/
dist/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A starter for creating your own Slack bot using [Bolt](https://github.com/slacka

> With an example face quiz slash command.

![/facequiz](./docs/slash-quiz.png)
![/facequiz](./slash-quiz.png)

**Related links**

Expand Down
17 changes: 0 additions & 17 deletions app.js

This file was deleted.

17 changes: 0 additions & 17 deletions commands.js

This file was deleted.

54 changes: 0 additions & 54 deletions data.js

This file was deleted.

9 changes: 0 additions & 9 deletions errors.js

This file was deleted.

27 changes: 0 additions & 27 deletions events.js

This file was deleted.

9 changes: 0 additions & 9 deletions index.js

This file was deleted.

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"name": "slackbot-starter",
"version": "0.0.1",
"description": "A starter for creating Slack bots",
"main": "index.js",
"main": "dist/index.js",
"repository": "https://github.com/tomfa/slackbot-starter",
"author": "Tomas Fagerbekk <[email protected]>",
"license": "MIT",
"scripts": {
"start": "node index.js",
"build": "tsc -p .",
"build:watch": "tsc -w -p .",
"start": "npm run build && node dist/index.js",
"prettier": "prettier --single-quote --trailing-comma all --write ."
},
"husky": {
Expand All @@ -16,13 +18,15 @@
}
},
"dependencies": {
"@slack/bolt": "^2.1.1",
"@slack/bolt": "^3.13.3",
"dotenv": "^8.2.0",
"node-fetch": "^2.6.7",
"prettier": "^2.0.5"
"typescript": "^4.7.4"
},
"devDependencies": {
"@types/node-fetch": "^2.6.2",
"husky": "^4.2.5",
"pretty-quick": "^2.0.1"
"pretty-quick": "^2.0.1",
"prettier": "^2.0.5"
}
}
58 changes: 0 additions & 58 deletions quiz.js

This file was deleted.

File renamed without changes
12 changes: 12 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require("dotenv").config();

import { ChatBot } from "./types";

export const createApp = (config: {
slackBotToken: string;
slackSigningSecret: string;
}): ChatBot =>
new ChatBot({
token: config.slackBotToken,
signingSecret: config.slackSigningSecret,
});
41 changes: 41 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { AckFn, RespondArguments, SayFn, SlashCommand } from "@slack/bolt";

import { MessageError } from "./errors";
import { ChatBot } from "./types";
import { getFaceQuiz } from "./quiz";
import { fetchUsers } from "./data";

const getFaceQuizCommand =
(app: ChatBot) =>
async ({
command,
ack,
say,
}: {
command: SlashCommand;
ack: AckFn<string | RespondArguments>;
say: SayFn;
}) => {
try {
await ack();
const slackUsers = await fetchUsers({ app });
const quiz = await getFaceQuiz({
exclude: [command.user_id],
slackUsers,
});
await app.dm({ user: command.user_id, blocks: quiz.blocks });
} catch (error) {
if (error instanceof MessageError) {
await app.dm({
user: command.user_id,
text: (error as MessageError).message,
});
} else {
throw error;
}
}
};

export const addSlashCommands = (app: ChatBot) => {
app.command("/facequiz", getFaceQuizCommand(app));
};
69 changes: 69 additions & 0 deletions src/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fetch from "node-fetch";

import { ChatBot, User } from "./types";

let userData: User[] = [];

async function isRedirected(url: string) {
const data = await fetch(url, { method: "HEAD" });
return data.redirected;
}

async function hasImage(user: User) {
if (user.profile?.image_original) {
return true;
}
const gravatarUrl = user.profile?.image_192;
return !!gravatarUrl && !(await isRedirected(gravatarUrl));
}

const hasImageLazy = (user: User) => async () => {
if (user.hasImage !== undefined) {
return user.hasImage();
}
const value = await hasImage(user);
user.hasImage = async () => value;
return value;
};

export async function fetchUsers({
app,
refresh = false,
}: {
app: ChatBot;
refresh?: false;
}): Promise<User[]> {
if (userData && !refresh) {
return userData;
}
try {
const result = await app.client.users.list({
token: process.env.SLACK_BOT_TOKEN,
});
if (!result.members) {
console.error(`Unable to list users`);
return [];
}
userData = result.members
.filter((m) => !m.deleted && !m.is_bot && !!m.profile && !!m.id)
.filter((m) => m.name !== "slackbot")
.map(
(m): User => ({
...m,
id: m.id!,
profile: m.profile!,
hasImage: hasImageLazy(m as User),
})
);

return userData;
} catch (error) {
console.error(error);
return [];
}
}

export async function getUser({ app, id }: { app: ChatBot; id: string }) {
const users = await fetchUsers({ app });
return users.find((u) => u.id === id);
}
4 changes: 4 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class MessageError extends Error {
// Sends message to slack
}
export class NoRemainingUsers extends MessageError {}
Loading