Slack
This library allows using Slack with Wing.
Prerequisites
Supported Features
- Events
- Direct Channel Messages
Unsupported Features (TODO)
- Distribution Management
- Interactive Components
- Slash Commands
- Workflows
- Incoming Webhooks
Installation
Use npm
to install this library:
npm i @winglibs/slack
Bring it
bring slack;
bring cloud;
let myBotToken = new cloud.Secret(name: "mybot_token");
let slackbot = new slack.App(token: myBotToken);
/// When registering events, the inflight function will be called with the context and event
/// The `ctx` is a reference to the context of the event, and provides client methods to interact with channels, threads, etc..
/// The `event` is the raw Json event data from Slack and can be used to extract information from the event
slackbot.onEvent("app_mention", inflight (ctx, event) => {
let message = new slack.Message();
message.addSection({
fields: [
{
type: slack.FieldType.mrkdwn,
text: "*Wow this is markdown!!*\ncool beans!!!"
}
]
});
ctx.channel.postMessage(message);
});
Create your Slack App
- Go to the Slack API Dashboard and create a new app.
- Select Create from Scratch.
- For the README example above, ensure you provide the following permissions:
app_mentions:read
chat:write
chat:write.public
channels:read
- Navigate to Events and subscribe to the following events:
app_mention
- Navigate to OAuth & Permissions and install the app to your workspace
- Copy the Bot User OAuth Token to your clipboard
- Navigate to Event Subscriptions and enable events, then subscribe to bot events:
app_mention
Running in the Wing Simulator
First lets configure our Slack bot token as a secret in the simulator.
wing secrets
When prompted, paste the Bot User OAuth Token you copied earlier.
Next when running in the Wing Simulator, you will need to expose the endpoint of the Slackbot API, this can be done through the simulator console by selecting Open tunnel for this endpoint
Take this URL and navigate back to your Slack App, under the Event Subscriptions section, paste the URL into the Request URL field and append /slack/events
to the end of the URL.
Running in AWS
First lets configure our Slack bot token as a secret in AWS.
wing secrets -t tf-aws
When prompted, paste the Bot User OAuth Token you copied earlier.
Next, let's run the following command to deploy our app to AWS.
wing compile -t tf-aws
terraform -chdir=target/main.tfaws init
terraform -chdir=target/main.tfaws apply
After compiling and deploying your app using tf-aws
you there will be an endpoint called Slack_Request_Url
that is part of the terraform output. The URL should end with /slack/events
. It will look something like this:
Apply complete! Resources: 11 added, 0 changed, 0 destroyed.
Outputs:
App_Api_Endpoint_Url_E233F0E8 = "https://zgl8r8wsng.execute-api.us-east-1.amazonaws.com/prod"
App_Slack_Request_Url_FF26641D = "https://zgl8r8wsng.execute-api.us-east-1.amazonaws.com/prod/slack/events"
Navigate back to your Slack App, under the Event Subscriptions section, paste the URL into the Request URL field and append /slack/events
to the end of the URL.
Post Directly to a Channel
If you want to post directly to a channel, you can do so by using the following code:
let postMessage = new cloud.Function(inflight () => {
let channel = bot.channel("NAME|ID");
channel.post("hello world!");
});
Finding a Channel ID
If you prefer to use a channel ID over a channel name, you can find the channel ID by right-clicking on the channel name and select View channel details
. The channel ID will be at the bottom of the popup modal.
API Reference
Table of Contents
- Classes
- Interfaces
- Structs
- Enums
SlackUtils (preflight class)
No description
Constructor
new(): SlackUtils
Properties
No properties
Methods
Signature | Description |
---|---|
static inflight post(body: Json, token: str): Json | No description |
Message (inflight class)
Represents a Message block see: https://api.slack.com/block-kit
Constructor
new(): Message
Properties
Name | Type | Description |
---|---|---|
sections | MutArray
| No description |
Methods
Signature | Description |
---|---|
inflight addSection(section: Section): void | No description |
inflight toJson(): Json | Returns Json representation of message |
App (preflight class)
No description
Constructor
new(props: AppProps): App
Properties
Name | Type | Description |
---|---|---|
api | Api | No description |
Methods
Signature | Description |
---|---|
inflight channel(id: str): Channel | Retrieve a channel object from a channel Id or name |
onEvent(eventName: str, handler: inflight (EventContext, Json): Json?): void | Register an event handler (for available events see: https://api.slack.com/events) |
Channel (inflight class)
Represents the context of a slack channel
Constructor
new(): Channel
Properties
Name | Type | Description |
---|---|---|
id | str | The channel id |
Methods
Signature | Description |
---|---|
inflight post(message: str): Json | Post raw text to a channel |
inflight postMessage(message: Message): Json | Post a message block to a channel |
EventContext (inflight class)
Represents the context of an event callback
Constructor
new(): EventContext
Properties
Name | Type | Description |
---|---|---|
channel | Channel | No description |
thread | Thread | No description |
Methods
No methods
EventContext_Mock (inflight class)
Internally used for mocking event context
Constructor
new(): EventContext_Mock
Properties
Name | Type | Description |
---|---|---|
channel | Channel | No description |
thread | Thread | No description |
Methods
No methods
MockChannel (inflight class)
Only used for internal testing
Constructor
new(): MockChannel
Properties
Name | Type | Description |
---|---|---|
id | str | The channel id |
Methods
Signature | Description |
---|---|
inflight post(message: str): Json | No description |
inflight postMessage(message: Message): Json | No description |
Thread (inflight class)
Represents the context of a slack thread
Constructor
new(): Thread
Properties
Name | Type | Description |
---|---|---|
channel | Channel | The channel context |
timestamp | str | The thread timestamp |
Methods
Signature | Description |
---|---|
inflight post(message: str): Json | Post raw text to a thread |
inflight postMessage(message: Message): Json | Post a message to a thread |
IThread (interface)
The bahvioral interface of a thread
Properties
No properties
Methods
Signature | Description |
---|---|
inflight post(message: str): Json | No description |
inflight postMessage(message: Message): Json | No description |
Block (struct)
No description
Properties
Name | Type | Description |
---|---|---|
fields |
| No description |
type | BlockType | No description |
Field (struct)
No description
Properties
Name | Type | Description |
---|---|---|
text | str | No description |
type | FieldType | No description |
Section (struct)
No description
Properties
Name | Type | Description |
---|---|---|
fields |
| No description |
AppProps (struct)
Properties for Slack bot
Properties
Name | Type | Description |
---|---|---|
ignoreBots | bool? | Whether events from bot users should be ignored (default: true) |
token | Secret | The token secret to use for the app |
CallbackEvent (struct)
No description
Properties
Name | Type | Description |
---|---|---|
app_id | str? | No description |
bot_id | str? | No description |
channel | str | No description |
event_ts | str | No description |
team | str? | No description |
ts | str | No description |
type | str | No description |
user | str | No description |
MessageCallbackEvent (struct)
No description
Properties
Name | Type | Description |
---|---|---|
app_id | str? | No description |
bot_id | str? | No description |
channel | str | No description |
event_ts | str | No description |
team | str? | No description |
text | str | No description |
ts | str | No description |
type | str | No description |
user | str | No description |
SlackEvent (struct)
No description
Properties
Name | Type | Description |
---|---|---|
type | str | No description |
VerificationEvent (struct)
No description
Properties
Name | Type | Description |
---|---|---|
challenge | str | No description |
token | str | No description |
type | str | No description |
BlockType (enum)
No description
Values
Name | Description |
---|---|
section | No description |
FieldType (enum)
No description
Values
Name | Description |
---|---|
plain_text | No description |
mrkdwn | No description |