reshuffle-slack-connector
v0.0.3
Published
A Reshuffle Slack connector
Downloads
230
Readme
Reshuffle Slack Connector
npm install reshuffle-slack-connector
Commonjs: const { SlackConnector } = require('reshuffle-slack-connector')
ES6 import: import { SlackConnector } from 'reshuffle-slack-connector'
This connector is designed to interact with Slack API and allow you to post/amend/delete/search messages directly into Slack.
This connector required a Slack App to be configured:
- Go to https://api.slack.com/apps
- Click on
Create New App
- Enter a
name
and select aworkspace
- Click on
Create App
- Click on your new app
- The signing secret is under
Basic Information
>App Credentials
- The token is under
Settings
>Install App
> OAuth Access Token
To create a Slack connector, you need to provide configuration options like this:
interface SlackConnectorConfigOptions {
token: string
signingSecret: string
port?: number
endpoints?:
| string
| {
[endpointType: string]: string
}
}
Create a new Slack Connector: Token and signingSecret are found at https://api.slack.com/apps/<your_slack_app_id>/event-subscriptions
const app = new Reshuffle()
const slackConnector = new SlackConnector(app, {
token: '<starts with xox>',
signingSecret: '<signing_secret>',
port: '<slack_app_port>', // default port is 3000
endpoints: '<slack_app_endpoints>' // default endpoints is'/'
})
Events:
*event Listening to Slack events such as new messages or new users joining
Actions:
*postMessage Post a text message to a specified Slack channel or conversation.
*scheduleMessage Schedule a text message to be posted on a specified Slack channel or conversation at a given time.
*updateMessage Update an existing Slack message.
*deleteMessage Delete an existing Slack message.
*searchMessages Search Slack for a message matching a specified query string.
*getWebClient Get Slack Web Client.
*getSlackApp Get Slack Application instance.
*sdk Returns a Slack Application instance, and a Slack web client instance.
Connector Events
Listening to Slack event
To listen to any kind of events happening in Slack,
use type SlackEventType.EVENT
as event option (see example below)
As event option values, pass the Slack Event API type (e.g. 'message') for firing an event on a new message.
'Full list of Slack Event API type'
Your handler receives an event object and Reshuffle app. Example of event object received:
{
payload: {
client_msg_id: "d1f6d102-69a6-49a0-aa35-f6a9f4ffcf04"
type: "message"
text: "my message in Slack"
user: "U01APN2NLVD"
ts: "1601003324.000400"
team: "T01B4LHKJ3D"
channel: "C01AXNBH0QN"
event_ts: "1601003324.000400"
channel_type: "channel"
},
say: SayFn,
context: {
botToken: "xoxb-..."
botUserId: "U01BU9WD5S4"
botId: "B01BAL52SP6"
},
client: WebClient
}
Example
const slackConnector = new SlackConnector(app, {
token: '<starts with xox>',
signingSecret: '<signing_secret>',
port: '<slack_app_port>',
})
// Events
slackConnector.on(
{
type: SlackEventType.EVENT,
values: {
type: SlackEvents.MESSAGE,
},
},
(event, app) => {
console.log('new message posted on Slack')
console.log(JSON.stringify(event))
},
)
app.start()
To listen to any kind of events happening in Slack,
use type SlackEventType.EVENT
as event option (see example below)
As event option values, pass the Slack Event API type (e.g. 'message') for firing an event on a new message.
'Full list of Slack Event API type'
Example
const slackConnector = new SlackConnector(app, {
token: '<starts with xox>',
signingSecret: '<signing_secret>',
port: '<slack_app_port>',
})
// Events
slackConnector.on(
{
type: SlackEventType.EVENT,
values: {
type: SlackEvents.MESSAGE,
},
},
(event, app) => {
console.log('new message posted on Slack')
console.log(JSON.stringify(event))
},
)
app.start()
Connector actions
Post a message to Slack
Slack Documentation 'chat.postMessage'
Required Slack Permissions:
bot: chat:write
Definition:
(
channelId: string,
text: string,
msgOptions?: MsgOpts,
) => WebAPICallResult
MsgOpts
{
as_user?: boolean;
attachments?: MessageAttachment[];
blocks?: (KnownBlock | Block)[];
icon_emoji?: string; // if specified, as_user must be false
icon_url?: string;
link_names?: boolean;
mrkdwn?: boolean;
parse?: 'full' | 'none';
reply_broadcast?: boolean; // if specified, thread_ts must be set
thread_ts?: string;
unfurl_links?: boolean;
unfurl_media?: boolean;
username?: string; // if specified, as_user must be false
}
Usage:
await slackConnector.postMessage('<channel_id>', 'Some message!', { mrkdwn: true })
Schedule a message to post on Slack
Slack Documentation 'chat.scheduleMessage'
Required Slack Permissions:
bot: chat:write
Definition:
(
channelId: string,
postAt: Date,
text: string,
) => WebAPICallResult
ScheduleMsgOpts
{
as_user?: boolean;
attachments?: MessageAttachment[];
blocks?: (KnownBlock | Block)[];
link_names?: boolean;
parse?: 'full' | 'none';
reply_broadcast?: boolean; // if specified, thread_ts must be set
thread_ts?: string;
unfurl_links?: boolean;
unfurl_media?: boolean;
}
Usage:
await slackConnector.scheduleMessage(
'<channel_id>',
new Date(Date.now() + 10000), // 10 seconds from current time
'Scheduled message!',
{ mrkdwn: true },
)
Update an existing Slack message
Slack Documentation 'chat.update'
Required Slack Permissions:
bot: chat:write
Definition:
(
channelId: string,
text: string,
timestamp: string,
msgOptions?: MsgOpts,
) => WebAPICallResult
MsgOpts
{
as_user?: boolean;
attachments?: MessageAttachment[];
blocks?: (KnownBlock | Block)[];
icon_emoji?: string; // if specified, as_user must be false
icon_url?: string;
link_names?: boolean;
mrkdwn?: boolean;
parse?: 'full' | 'none';
reply_broadcast?: boolean; // if specified, thread_ts must be set
thread_ts?: string;
unfurl_links?: boolean;
unfurl_media?: boolean;
username?: string; // if specified, as_user must be false
}
Usage:
await slackConnector.updateMessage(
'<channel_id>',
'Some message!',
'1405894322.002768',
{ mrkdwn: true },
)
Delete an existing Slack message
Slack Documentation 'chat.delete'
Required Slack Permissions:
bot: chat:write
Definition:
(channelId: string, timestamp: string) => WebAPICallResult
Usage:
await slackConnector.deleteMessage(
'<channel_id>',
'1405894322.002768',
)
Search through messages on Slack
Slack Documentation 'search.messages'
Required Slack Permissions:
user: search:read
Multiple messages will be returned in the case that multiple matches are found for your given query.
Definition:
(query: string) => WebAPICallResult
Usage:
await slackConnector.searchMessages('some text to look for')
Get Slack Web Client
Get Slack Web Client instance to code bespoke solutions
Definition:
() => WebClient
Usage:
const webClient = await slackConnector.getWebClient()
// Example:
await webClient.chat.delete({options})
Get Slack Application instance
Slack Documentation 'Creating a Slack App'
Get Slack Application instance for coding advanced features
Definition:
() => App
Usage:
const slackApp = await slackConnector.getSlackApp()
// Example:
await slackApp.client.chat.update({options})
Returns Reshuffle Slack sdk
Returns a Slack Application instance, and a Slack web client instance
Definition:
() => { slackApp: App; webClient: WebClient }
Usage:
const { slackApp, webClient } = await slackConnector.sdk()
API definitions
Definition of WebAPICallResult
More examples on how to use this connector can be found here.