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

@winglibs/slack

v0.1.5

Published

Slack library for Wing

Downloads

14,300

Readme

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

  1. Go to the Slack API Dashboard and create a new app.
  2. Select Create from Scratch.
  3. For the README example above, ensure you provide the following permissions:
  • app_mentions:read
  • chat:write
  • chat:write.public
  • channels:read
  1. Navigate to Events and subscribe to the following events:
  • app_mention
  1. Navigate to OAuth & Permissions and install the app to your workspace
  2. Copy the Bot User OAuth Token to your clipboard
  3. 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

Open Tunnel

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.