Skip to content
Gallery
Create a Custom Slackbot for your Coda Doc
Share
Explore
Video and Instructions

icon picker
Slackbot Pack Code

// This import statement gives you access to all parts of the Coda Packs SDK.
import * as coda from "@codahq/packs-sdk";

// This line creates your new Pack.
export const pack = coda.newPack();


// add the domain
pack.addNetworkDomain('slack.com')

// create a parameter for the Slack Channel
let actionParamChannel = coda.makeParameter({
type: coda.ParameterType.String,
name: "channel",
description: "the channel in Slack to send the message to",
});

// create a parameter for the timestamp (used in thread formula)
let actionParamTS = coda.makeParameter({
type: coda.ParameterType.String,
name: "timestamp",
description: "the timestamp of the thread to reply to",
});

// create a parameter for the message to send in Slack
let actionParamMessage = coda.makeParameter({
type: coda.ParameterType.String,
name: "message",
description: "the text to send in Slack",
});

// this formula will send a message as your bot to the specified channel
pack.addFormula({
name: 'SendMessage',
description: 'Send a message as the slackbot',
parameters: [
actionParamChannel,
actionParamMessage,
],
execute: async function ([channel,message],context) {
let response = await context.fetcher.fetch({
url: `https://slack.com/api/chat.postMessage`,
//send POST to API
// note: replace the [YOUR BEARER TOKEN GOES HERE] text with the bot token provided by Slack. (bot tokens begin with xoxb-)
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer [YOUR BEARER TOKEN GOES HERE]",
},
body: JSON.stringify(
{
"channel": channel,
"text": message,
"link_names": true //this parameter is required in order to @-mention by username
}
),
});
return response.body.ts; //returns the timestamp of the message, can be used by replytothread formula
},
isAction: true,
resultType: coda.ValueType.String,
});
pack.addFormula({
name: 'ReplyToThread',
description: 'Reply to a thread as the slackbot',
parameters: [
actionParamChannel,
actionParamMessage,
actionParamTS
],
execute: async function ([channel,message,timestamp],context) {
let response = await context.fetcher.fetch({
url: `https://slack.com/api/chat.postMessage`,
//send POST to API
// note: replace the [YOUR BEARER TOKEN GOES HERE] text with the bot token provided by Slack. (bot tokens begin with xoxb-)
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer [YOUR BEARER TOKEN GOES HERE]",
},
body: JSON.stringify({
"channel": channel,
"text": message,
"thread_ts": timestamp,
"link_names": true
}
),
});
return response.body.ts;
},
isAction: true,
resultType: coda.ValueType.String,
})
//

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.