While building your Run On Slack functions, you can access and make calls to the Slack API.
To do this, you will:
token
to your function handler →The first step in making API calls in your Run On Slack functions is to import the API library:
import type { FunctionHandler } from "deno-slack-sdk/types.ts";
import { SlackAPI } from "deno-slack-api/mod.ts"; // Add this
token
to your function handler The next step is to add the token
helper to your function handler:
import type { FunctionHandler } from "deno-slack-sdk/types.ts";
import { SlackAPI } from "deno-slack-api/mod.ts";
const reverse: FunctionHandler<any, any> = async (
{ inputs, env, token }, // Add `token` here
) => {
// ...
Then, instantiate the Slack API client by passing along the token
helper:
import type { FunctionHandler } from "deno-slack-sdk/types.ts";
import { SlackAPI } from "deno-slack-api/mod.ts";
const reverse: FunctionHandler<any, any> = async (
{ inputs, env, token },
) => {
const client = SlackAPI(token, {}); // Add this
// ...
The second argument to SlackAPI
is an options object; this can be empty.
Finally, make the API call using client.apiCall(...)
:
import type { FunctionHandler } from "deno-slack-sdk/types.ts";
import { SlackAPI } from "deno-slack-api/mod.ts";
const reverse: FunctionHandler<any, any> = async (
{ inputs, env, token },
) => {
const client = SlackAPI(token, {});
// Call an API method in one of two ways.
// Option #1: use client.apiCall(<method name>)
await client.apiCall('chat.postMessage', {
channel: inputs.some_channel,
text: `You said "${inputs.some_string}"`
});
// Option #2: use client.methodGroup.method
await client.chat.postMessage({
channel: inputs.some_channel,
text: `You said "${inputs.some_string}"`
});
// ...
Most API endpoints require specific permission scopes. Add scopes to your app by listing them in the botScopes
property of your manifest.