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

@alru/slack

v1.0.32

Published

A wrapper for the Slack Bolt SDK, making it easier to manage and populate views.

Downloads

61

Readme

@alru/slack

👉 Tutorial video

This is a wrapper for the Slack Bolt SDK for Node.js, primarily designed to make it easier to work with BlockKit and Views:

Create BlockKit markup faster using ready-made functions

const {header, divider} = slack.blocks;
const blocks = [
    header({text: 'Hello world'}),
    divider()
]

Simplify your work with Views

// Define a view with a composer function
const {section} = slack.blocks;
const view = slack.home({
 composer: (slackId) => {
    return [
        section({
            text: `Hello, <@${slackId}>`
        })
    ];
 }
});

// Compose view and publish it
await view.compose(slackId);
await view.publish(webClient, slackId);

...and speed-up production with useful utils

Note: As this is just a wrapper, you need to be familiar with the Slack API and Bolt SDK.

Installation

  npm install @alru/slack

Initialization and listening

Initialization has a very simple wrapper that allows you to pass options (such as credentials) with which to run the Bolt application.

import slack from '@alru/slack';

const app = new slack.App({
  options: {
    token: `<your-token>`,
    signingSecret: `<your-signing-secret>`,
    socketMode: true,
    appToken: `<your-app-token>`,
    port: 3000,
  },
  handlers: [
    (app) => app.message('hello', ({say}) => say('Hello yourself!')),
  ]
});

You have to specify Handlers or Listeners properties. This is essentially the same thing, just functions that will be called with the app argument, which is a wrapper over the Bolt application. Given an app, you can subscribe to events in the usual Bolt way (e.g.: app.event()). The different names are provided for convenience and clarity, depending on how you will build your architecture: using listener -> handler or handler directly.

function exampleHandler(app) {
  app.message('hello', ({slackId, say}) => say(`Hello <@${slackId}>!`));
}
function exampleListener(app) {
  app.event('app_home_opened', updateHomeHandler);
}

In this case, native methods are wrapped to make it easier for you to get some properties. For example, all your subscriptions will have the slackId property passed to them (i.e. the user ID) and you won't have to search for it in payload or body.

BlockKit

Ready-made functions for creating BlockKit parts are available as:

  • slack.blocks
  • slack.elements
  • slack.objects

Views

You can create View instances using slack.home() or slack.modal().

You can either provide static blocks as a blocks property or use a composer function to render blocks dynamically.

function myHandler(app) {
  app.action('open-modal', async ({webClient, slackId, trigger_id}) => {

    // Static View Example
    const staticView = slack.modal({
      title: 'My Modal',
      blocks: [
        slack.blocks.section({
          text: 'Hello, World!',
        }),
      ],
    });
    await staticView.open(webClient, {trigger_id});
    
    // Dynamic View Example
    const dynamicView = slack.modal({
      title: 'My Modal',
      // Yes, composer can be async if you want
      composer: async ({slackId, message}) => {
        return [
          slack.blocks.section({
            text: `Hello, <@${slackId}>!\n${message}`,
          }),
        ];
      }
    });
    await dynamicView.compose({slackId, message: 'Have a nice day!'});
    await dynamicView.open(webClient, {trigger_id});
    
  });
}

Useful utils

Format date (to user local timezone)

const {formatDate} = slack.utils;
const date = formatDate(Date.now(), { token_string: '{date_long}' });
const time = formatDate(Date.now(), { token_string: '{time}' });

Parse payload values

const {parseView, parseAction} = slack.utils;

// Parse view submission values
app.view('some-view-submit', async ({view}) => {
  const data = parseView(view, true);
})

// Parse action value
app.action('some-action', async ({action}) => {
  const value = parseAction(action);
});

Contributing

Contributions are always welcome!