diff --git a/src/services/slack.ts b/src/services/slack.ts index 6ca5bd8..97dfbcb 100644 --- a/src/services/slack.ts +++ b/src/services/slack.ts @@ -56,16 +56,16 @@ export type SlackBlock = { } export type SlackPostMessageParams = { - /** - * The ID of the channel to send the message to. - */ - channel: string - /** * The text of the message to send. */ text: string + /** + * The ID of the channel to send the message to. + */ + channel?: string + /** * The timestamp of a parent message to send the message as a reply to. */ @@ -165,8 +165,14 @@ export class SlackClient { * Sends a message to a channel. */ public async sendMessage(options: SlackPostMessageParams) { + if (!options.channel && !this.defaultChannel) { + throw new Error('Error: No channel specified') + } const res = await this.api.post('chat.postMessage', { - json: options + json: { + channel: this.defaultChannel, + ...options + } }) return res.json() } diff --git a/test/slack.test.ts b/test/slack.test.ts index ab3abf7..1ea7176 100644 --- a/test/slack.test.ts +++ b/test/slack.test.ts @@ -5,7 +5,7 @@ import { SlackClient } from '@/services/slack' import './_utils' test('SlackClient.sendMessage', async (t) => { - if (!process.env.SLACK_API_KEY) { + if (!process.env.SLACK_API_KEY || !process.env.SLACK_DEFAULT_CHANNEL) { return t.pass() } @@ -13,14 +13,13 @@ test('SlackClient.sendMessage', async (t) => { const client = new SlackClient() const result = await client.sendMessage({ - text: 'Hello World!', - channelId: 'D05B1AHA55L' + text: 'Hello World!' }) t.truthy(result) }) test('SlackClient.sendAndWaitForReply', async (t) => { - if (!process.env.SLACK_API_KEY) { + if (!process.env.SLACK_API_KEY || !process.env.SLACK_DEFAULT_CHANNEL) { return t.pass() } @@ -31,7 +30,6 @@ test('SlackClient.sendAndWaitForReply', async (t) => { async () => { await client.sendAndWaitForReply({ text: 'Please reply to this message with "yes" or "no"', - channelId: 'D05B1AHA55L', validate: () => false, // never validate so we timeout timeoutMs: 1000, intervalMs: 100