Skip to content

Commit

Permalink
init functions integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jimray authored and srajiang committed Aug 19, 2021
1 parent 0ad0e89 commit 656cc1c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
matchConstraints,
onlyCommands,
matchCommandName,
matchFunctionCallback,
onlyOptions,
onlyShortcuts,
onlyEvents,
Expand Down Expand Up @@ -578,7 +579,12 @@ export default class App {
this.listeners.push([onlyCommands, matchCommandName(commandName), ...listeners] as Middleware<AnyMiddlewareArgs>[]);
}

public options<Source extends OptionsSource = 'block_suggestion'>(
// matches a Slack function callback specifically
public function(callbackID: string, ...listeners: Middleware<SlackEventMiddlewareArgs>[]): void {
this.listeners.push([matchFunctionCallback(callbackID), ...listeners] as Middleware<AnyMiddlewareArgs>[]);
}

public options<Source extends OptionsSource = OptionsSource>(
actionId: string | RegExp,
...listeners: Middleware<SlackOptionsMiddlewareArgs<Source>>[]
): void;
Expand Down
15 changes: 15 additions & 0 deletions src/middleware/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ function matchesPattern(pattern: string | RegExp, candidate: string): boolean {
return pattern.test(candidate);
}

/**
* Middleware that filters out any Function that doesn't match callback_id
*/
export function matchFunctionCallback(callback_id: string): Middleware<SlackEventMiddlewareArgs> {
return async ({ event, next }) => {
// Filter out any functions that are not the correct function name
if (!event || 'function_executed' !== event.type || !event.function || callback_id !== event.function.callback_id) {
return;
}

// TODO: remove the non-null assertion operator
await next!();
};
}

/*
* Middleware that filters out events that don't match pattern
*/
Expand Down
28 changes: 27 additions & 1 deletion src/types/events/base-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export type SlackEvent =
| WorkflowPublishedEvent
| WorkflowUnpublishedEvent
| WorkflowStepDeletedEvent
| WorkflowStepExecuteEvent;
| WorkflowStepExecuteEvent
| FunctionExecutedEvent;

export type EventTypePattern = string | RegExp;

Expand Down Expand Up @@ -896,3 +897,28 @@ export interface WorkflowStepExecuteEvent {

// NOTE: `user_resourced_denied`, `user_resource_granted`, `user_resourced_removed` are left out because they are
// deprecated as part of the Workspace Apps Developer Preview

export interface FunctionExecutedEvent {
type: 'function_executed';
function_execution_id: string;
function: {
id: string;
callback_id: string;
name: string;
description: string;
input_parameters: {
name: string;
description: string;
type: string;
}[];
output_parameters: {
name: string;
description: string;
type: string;
}[];
};
arguments: {
[index: string]: any;
};
event_ts: string;
}

0 comments on commit 656cc1c

Please sign in to comment.