npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reshuffle-slack-connector

v0.0.3

Published

A Reshuffle Slack connector

Downloads

6

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 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:

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

Slack Documentation 'web API'

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.