UNPKG

@microfox/slack

Version:

This package provides a lightweight, proxy interface to the official Slack Web API, offering a curated set of the most commonly used functions for building Slack integrations. It is designed to be simple, efficient, and easy to integrate into your project

1 lines 9.9 kB
{"version":3,"sources":["../src/index.ts","../src/MicrofoxSlackClient.ts"],"sourcesContent":["export * from '@slack/web-api';\nexport * from './MicrofoxSlackClient';","import {\n WebClient,\n WebClientOptions,\n ChatPostMessageResponse,\n ConversationsCreateResponse,\n ConversationsInviteResponse,\n ConversationsKickResponse,\n FilesUploadResponse,\n ReactionsAddResponse,\n RemindersAddResponse,\n} from '@slack/web-api';\nimport { Buffer } from 'buffer';\n\nexport class MicrofoxSlackClient {\n private web: WebClient;\n\n constructor(token: string, options?: WebClientOptions) {\n this.web = new WebClient(token, options);\n }\n\n /**\n * Lists all public and private channels in a workspace.\n */\n async listChannels() {\n const result = await this.web.conversations.list({\n types: 'public_channel,private_channel',\n });\n return result.channels;\n }\n\n /**\n * Fetches information about a conversation.\n * @param channelId Conversation ID to fetch information for.\n */\n async getChannelConversationInfo(channelId: string) {\n const result = await this.web.conversations.info({\n channel: channelId,\n });\n return result.channel;\n }\n\n /**\n * Lists all users in a workspace.\n */\n async listUsers() {\n const result = await this.web.users.list({\n limit: 1000,\n });\n return result.members;\n }\n\n /**\n * Lists all users in a channel.\n * @param channelId Channel ID to get members of.\n */\n async listChannelUsers(channelId: string) {\n const result = await this.web.conversations.members({\n channel: channelId,\n });\n return result.members;\n }\n\n /**\n * Finds a user by their email address.\n * @param email The email address of the user to find.\n */\n async searchUser(email: string) {\n const result = await this.web.users.lookupByEmail({ email });\n return result.user;\n }\n\n /**\n * Finds a channel by its name. This is case-insensitive.\n * @param name The name of the channel to find.\n */\n async searchChannel(name: string) {\n const channels = await this.listChannels();\n return channels?.find((c) => c.name?.toLowerCase() === name.toLowerCase());\n }\n\n /**\n * Sends a direct message to a user.\n * @param userId The ID of the user to message.\n * @param text The text of the message to send.\n */\n async messageUser(\n userId: string,\n text: string\n ): Promise<ChatPostMessageResponse> {\n const im = await this.web.conversations.open({ users: userId });\n if (im.ok && im.channel?.id) {\n return this.web.chat.postMessage({\n channel: im.channel.id,\n text: text,\n });\n }\n throw new Error(`Could not open DM with user ${userId}`);\n }\n\n /**\n * Sends a message to a channel.\n * @param channelId The ID of the channel to message.\n * @param text The text of the message to send.\n */\n async messageChannel(\n channelId: string,\n text: string\n ): Promise<ChatPostMessageResponse> {\n return this.web.chat.postMessage({\n channel: channelId,\n text: text,\n });\n }\n\n /**\n * Sets a reminder for a user.\n * @param userId The ID of the user to set a reminder for.\n * @param text The text of the reminder.\n * @param time A string describing when the reminder should fire (e.g., \"in 5 minutes\" or a Unix timestamp).\n */\n async setReminder(\n userId: string,\n text: string,\n time: string\n ): Promise<RemindersAddResponse> {\n return this.web.reminders.add({\n user: userId,\n text: text,\n time: time,\n });\n }\n\n /**\n * Creates a new channel.\n * @param name The name of the channel to create.\n * @param isPrivate Whether the channel should be private. Defaults to false.\n */\n async createChannel(\n name: string,\n isPrivate = false\n ): Promise<ConversationsCreateResponse['channel']> {\n const result = await this.web.conversations.create({\n name: name,\n is_private: isPrivate,\n });\n return result.channel;\n }\n\n /**\n * Adds a reaction to a message.\n * @param channelId The ID of the channel where the message is.\n * @param timestamp The timestamp of the message to react to.\n * @param reaction The name of the emoji to use for the reaction.\n */\n async reactMessage(\n channelId: string,\n timestamp: string,\n reaction: string\n ): Promise<ReactionsAddResponse> {\n return this.web.reactions.add({\n channel: channelId,\n timestamp: timestamp,\n name: reaction,\n });\n }\n\n /**\n * Gets information about a user.\n * @param userId The ID of the user to get information for.\n */\n async getUserInfo(userId: string) {\n const result = await this.web.users.info({\n user: userId,\n });\n return result.user;\n }\n\n /**\n * Replies to a message in a thread.\n * @param channelId The ID of the channel where the message is.\n * @param thread_ts The timestamp of the message to reply to, establishing the thread.\n * @param text The text of the reply.\n */\n async replyMessage(\n channelId: string,\n thread_ts: string,\n text: string\n ): Promise<ChatPostMessageResponse> {\n return this.web.chat.postMessage({\n channel: channelId,\n thread_ts: thread_ts,\n text: text,\n });\n }\n\n /**\n * Adds a user to a channel.\n * @param channelId The ID of the channel to add the user to.\n * @param userId The ID of the user to add.\n */\n async addUserToChannel(\n channelId: string,\n userId: string\n ): Promise<ConversationsInviteResponse> {\n return this.web.conversations.invite({\n channel: channelId,\n users: userId,\n });\n }\n\n /**\n * Removes a user from a channel.\n * @param channelId The ID of the channel to remove the user from.\n * @param userId The ID of the user to remove.\n */\n async removeUserFromChannel(\n channelId: string,\n userId: string\n ): Promise<ConversationsKickResponse> {\n return this.web.conversations.kick({\n channel: channelId,\n user: userId,\n });\n }\n\n /**\n * Uploads a file to a channel.\n * @param channelId The ID of the channel to upload the file to. Can be a comma-separated list of strings.\n * @param file A Buffer containing the file content.\n * @param filename The name of the file.\n * @param title An optional title for the file.\n */\n async sendFile(\n channelId: string,\n file: Buffer,\n filename: string,\n title?: string\n ): Promise<FilesUploadResponse> {\n return this.web.files.upload({\n channels: channelId,\n file: file,\n filename: filename,\n title: title,\n });\n }\n\n /**\n * Gets information about a file.\n * @param fileId The ID of the file to get information for.\n */\n async getFileInfo(fileId: string) {\n const result = await this.web.files.info({\n file: fileId,\n });\n return result.file;\n }\n} "],"mappings":";AAAA,cAAc;;;ACAd;AAAA,EACE;AAAA,OASK;AAGA,IAAM,sBAAN,MAA0B;AAAA,EAG/B,YAAY,OAAe,SAA4B;AACrD,SAAK,MAAM,IAAI,UAAU,OAAO,OAAO;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,eAAe;AACnB,UAAM,SAAS,MAAM,KAAK,IAAI,cAAc,KAAK;AAAA,MAC/C,OAAO;AAAA,IACT,CAAC;AACD,WAAO,OAAO;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,2BAA2B,WAAmB;AAClD,UAAM,SAAS,MAAM,KAAK,IAAI,cAAc,KAAK;AAAA,MAC/C,SAAS;AAAA,IACX,CAAC;AACD,WAAO,OAAO;AAAA,EACh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