···
import { createHmac, timingSafeEqual } from "node:crypto";
2
+
import Bottleneck from "bottleneck";
8
-
import Bottleneck from "bottleneck";
11
-
* Interface for mapping emoji names to their URLs
13
-
interface SlackEmoji {
14
-
[key: string]: string;
* Configuration options for initializing the SlackWrapper
···
* @throws Error if authentication fails
async testAuth(): Promise<boolean> {
65
-
const response = await fetch("https://slack.com/api/auth.test", {
67
-
Authorization: `Bearer ${this.botToken}`,
68
-
"Content-Type": "application/json",
57
+
const response = await this.limiter.schedule(() =>
58
+
fetch("https://slack.com/api/auth.test", {
60
+
Authorization: `Bearer ${this.botToken}`,
61
+
"Content-Type": "application/json",
72
-
const data = await response.json();
66
+
const data = (await response.json()) as {
68
+
error: string | null;
throw new Error(`Authentication failed: ${data.error}`);
···
* @throws Error if the API request fails
async getUserInfo(userId: string): Promise<SlackUser> {
87
-
const response = await fetch(
88
-
`https://slack.com/api/users.info?user=${userId}`,
84
+
const response = await this.limiter.schedule(() =>
85
+
fetch(`https://slack.com/api/users.info?user=${userId}`, {
Authorization: `Bearer ${this.botToken}`,
"Content-Type": "application/json",
body: JSON.stringify({ user: userId }),
99
-
const data: SlackUserInfoResponse = await response.json();
95
+
const data = (await response.json()) as SlackUserInfoResponse;
if ((!data.ok && data.error !== "user_not_found") || !data.user) {
throw new Error(data.error);
···
* @throws Error if the API request fails
async getEmojiList(): Promise<Record<string, string>> {
113
-
const response = await fetch("https://slack.com/api/emoji.list", {
115
-
Authorization: `Bearer ${this.botToken}`,
116
-
"Content-Type": "application/json",
109
+
const response = await this.limiter.schedule(() =>
110
+
fetch("https://slack.com/api/emoji.list", {
112
+
Authorization: `Bearer ${this.botToken}`,
113
+
"Content-Type": "application/json",
120
-
const data: SlackEmojiListResponse = await response.json();
118
+
const data = (await response.json()) as SlackEmojiListResponse;
if (!data.ok || !data.emoji) {
throw new Error(`Failed to get emoji list: ${data.error}`);
···
const hmac = createHmac("sha256", this.signingSecret);
const computedSignature = `v0=${hmac.update(baseString).digest("hex")}`;
140
-
Buffer.from(signature).valueOf() as Uint8Array,
141
-
Buffer.from(computedSignature).valueOf() as Uint8Array,
138
+
new Uint8Array(Buffer.from(signature)),
139
+
new Uint8Array(Buffer.from(computedSignature)),