A Reshuffle Slack connector
npm install reshuffle-slack-connectornpm 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 a workspace
- 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:
``ts`
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/
`ts`
const app = new Reshuffle()
const slackConnector = new SlackConnector(app, {
token: '
signingSecret: '
port: '
endpoints: '
})
_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.
##### 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:
`ts`
{
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
`js
const slackConnector = new SlackConnector(app, {
token: '
signingSecret: '
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
`js
const slackConnector = new SlackConnector(app, {
token: '
signingSecret: '
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()
`
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:_
`js`
await slackConnector.postMessage('
#### 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:_
`js`
await slackConnector.scheduleMessage(
'
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:_
`js`
await slackConnector.updateMessage(
'
'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:_
`js`
await slackConnector.deleteMessage(
'
'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:_
`js`
await slackConnector.searchMessages('some text to look for')
Get Slack Web Client instance to code bespoke solutions
_Definition:_
``
() => WebClient
_Usage:_
`js
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:_
`js
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:_
`js``
const { slackApp, webClient } = await slackConnector.sdk()
Definition of WebAPICallResult
More examples on how to use this connector can be found here.