bolt-js icon indicating copy to clipboard operation
bolt-js copied to clipboard

Add support for remote functions

Open misscoded opened this issue 1 year ago • 1 comments

⚠️ RC / In Beta. ⚠️

Summary

This PR adds support for remote (i.e., remotely hosted) functions for use in Workflow Builder.

What's New

A shiny new listener: .function()

Support of remote functions brings a new .function() listener that features complete and fail utilities to quickly and easily signal if the function has successfully executed or not.

app.function('sample_function', async ({ inputs, complete, fail }) => {
  try {
    const { sample_input } = inputs;
    complete({ outputs: { sample_output: sample_input } });
  } catch (error) {
    console.error(error);
    fail({ error });
  }
});

Function-specific interactivity support

We've beefed up the existing action listener to automatically determine if the action/interactivity event being received is associated with a function. If so, the callback will make available the complete and fail utility functions. Note: this requires that the interactivity call was made with the incoming JIT token. See below for details.

app.action('sample_button', async ({ ack, context, complete, fail }) => {
  await ack();
  const { functionExecutionId } = context;

  try {
    complete({ function_execution_id: functionExecutionId });
  } catch (error) {
    console.error(error);
    fail({ error });
  }
});

Automatic use of JIT token (with opt-out option)

When a function-related event is received, a JIT token is included in the payload. The subsequent use of this token when making API calls is what allows for interactivity to be associated with that function (and eventually see the function through to complete or fail). Out of respect for all of our developers and their individual setups, we've provided an easy way to opt-out of this JIT token attachment with the attachFunctionToken property in AppOptions.

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  socketMode: true,
  appToken: process.env.SLACK_APP_TOKEN,
  logLevel: LogLevel.DEBUG,
  // To opt-out of using the JIT token to make `client` calls in
  // function-related callbacks, set attachFunctionToken to false.
  // attachFunctionToken: false,
});

Outstanding / To Do

  • [x] DRY-up complete/fail utility logic (App.ts / WorkflowFunction.ts)
  • [x] Shore up TS (specifically, review events)
  • [x] Tests

Requirements (place an x in each [ ])

misscoded avatar Jan 08 '24 23:01 misscoded

Codecov Report

Attention: Patch coverage is 76.14679% with 26 lines in your changes missing coverage. Please review.

Project coverage is 81.59%. Comparing base (01c174d) to head (0110bf2). Report is 1 commits behind head on main.

Files Patch % Lines
src/App.ts 18.51% 15 Missing and 7 partials :warning:
src/CustomFunction.ts 95.38% 0 Missing and 3 partials :warning:
src/middleware/builtin.ts 85.71% 0 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff            @@
##           main    #2026       +/-   ##
=========================================
+ Coverage      0   81.59%   +81.59%     
=========================================
  Files         0       19       +19     
  Lines         0     1646     +1646     
  Branches      0      464      +464     
=========================================
+ Hits          0     1343     +1343     
- Misses        0      194      +194     
- Partials      0      109      +109     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Jan 26 '24 00:01 codecov[bot]