Skip to content

Commit

Permalink
fixing up example typescript integrration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
filmaj committed Sep 27, 2024
1 parent 0be635c commit d716b7e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
9 changes: 3 additions & 6 deletions examples/getting-started-typescript/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable no-console */
/* eslint-disable import/no-internal-modules */
import './utils/env';
import { App, LogLevel } from '@slack/bolt';
import { App, type BlockButtonAction, LogLevel } from '@slack/bolt';

const app = new App({
token: process.env.SLACK_BOT_TOKEN,
Expand Down Expand Up @@ -41,12 +39,11 @@ app.message('hello', async ({ message, say }) => {
}
});

app.action('button_click', async ({ body, ack, say }) => {
app.action<BlockButtonAction>('button_click', async ({ body, ack, say }) => {
// Acknowledge the action
await ack();
// we know that this event comes from a button click from a message in a channel, so `say` will be available.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await say!(`<@${body.user.id}> clicked the button`);
await say(`<@${body.user.id}> clicked the button`);
});

(async () => {
Expand Down
18 changes: 6 additions & 12 deletions examples/getting-started-typescript/src/basic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-console */
/* eslint-disable import/no-internal-modules */
import './utils/env';
import { App, type BlockAction, type BotMessageEvent, LogLevel, subtype } from '@slack/bolt';

Expand Down Expand Up @@ -77,6 +74,7 @@ app.event('team_join', async ({ event, client, logger }) => {
});

app.message(subtype('bot_message'), async ({ message, logger }) => {
// TODO: the need to cast here is due to https://github.com/slackapi/bolt-js/issues/796
const botMessage = message as BotMessageEvent;
logger.info(`The bot user ${botMessage.user} said ${botMessage.text}`);
});
Expand Down Expand Up @@ -118,11 +116,11 @@ app.action<BlockAction>(
await ack();
try {
// Make sure the event is not in a view
if (body.message) {
if (body.message && body.channel) {
await client.reactions.add({
name: 'white_check_mark',
timestamp: body.message?.ts,
channel: body.channel!.id, // if the body has a message, we know it has a channel, too.
timestamp: body.message.ts,
channel: body.channel.id, // if the body has a message, we know it has a channel, too.
});
}
} catch (error) {
Expand All @@ -132,14 +130,10 @@ app.action<BlockAction>(
);

// Your middleware will be called every time an interactive component with the action_id “approve_button” is triggered
app.action('approve_button', async ({ ack, say }) => {
app.action<BlockAction>('approve_button', async ({ ack, say }) => {
// Acknowledge action request
await ack();
// `say` is possibly undefined because an action could come from a surface where we cannot post message, e.g. a view.
// we will use a non-null assertion (!) to tell TypeScript to ignore the fact it may be undefined,
// but take care about the originating surface for these events when using these utilities!
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await say!('Request approved 👍');
await say('Request approved 👍');
});

(async () => {
Expand Down
1 change: 1 addition & 0 deletions src/types/actions/block-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export interface BlockAction<ElementAction extends BasicElementAction = BlockEle
id: string;
name: string;
};
// TODO: breaking change: this should not be optional, but should be conditionally tacked on based on the specific block_action subtype
message?: {
type: 'message';
user?: string; // undocumented that this is optional, it won't be there for bot messages
Expand Down
1 change: 1 addition & 0 deletions src/types/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type SlackActionMiddlewareArgs<Action extends SlackAction = SlackAction>
inputs?: FunctionInputs;
} & (Action extends Exclude<SlackAction, DialogSubmitAction | WorkflowStepEdit>
? // all action types except dialog submission and steps from apps have a channel context
// TODO: not exactly true: a block action could occur from a view. should improve this.
{ say: SayFn }
: unknown);

Expand Down

0 comments on commit d716b7e

Please sign in to comment.