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

@blockflow-labs/slack

v1.0.1

Published

blockflow slack wrapper

Downloads

5

Readme

SlackNotification Service

Introduction

Slack is a TypeScript class that provides an easy-to-use interface for sending rich, formatted messages to Slack channels using Slack's webhook API. It supports creating messages with multiple sections, fields, and colored attachments.

Features

  • Send simple text messages
  • Create structured messages with sections and fields
  • Support for colored attachments
  • Chainable methods for easy message construction
  • Written in TypeScript for improved type safety

Installation

npm i @blockflow-labs/slack

Usage

First, import the Slack class:

import { Slack } from "@blockflow-labs/slack";

Then, create an instance of the SlackNotification class with your Slack webhook URL:

const slack = new Slack("YOUR_SLACK_WEBHOOK_URL");

Sending a Simple Message

await slack.send("Hello, Slack!");

Creating a Message with Sections and Fields

await slack
  .addSection("New User Registered")
  .addFields([
    { title: "Name", value: "John Doe" },
    { title: "Email", value: "[email protected]" },
  ])
  .send();

Creating a Message with Colored Attachment

await slack
  .startAttachment("#36a64f")
  .addSection("Instance Started")
  .addFields([
    { title: "Instance ID", value: "i-1234567890abcdef0" },
    { title: "Type", value: "t2.micro" },
  ])
  .endAttachment()
  .send();

Full Example

Here's a more complex example that demonstrates multiple features:

import { Slack } from "@blockflow-labs/slack";

const slack = new Slack("YOUR_SLACK_WEBHOOK_URL");

interface InstanceData {
  id: string;
  type: string;
  region: string;
}

async function sendInstanceNotification(
  instance: InstanceData,
  status: "started" | "stopped"
) {
  const color = status === "started" ? "#36a64f" : "#ff0000";
  const title = `Instance ${status.charAt(0).toUpperCase() + status.slice(1)}`;

  try {
    await slack
      .startAttachment(color)
      .addSection(title)
      .addFields([
        { title: "Instance ID", value: instance.id },
        { title: "Type", value: instance.type },
        { title: "Region", value: instance.region },
      ])
      .endAttachment()
      .send(`EC2 Instance ${status}`);

    console.log(`Instance ${status} notification sent successfully`);
  } catch (error) {
    console.error(`Failed to send instance ${status} notification:`, error);
  }
}

// Example usage
const instance: InstanceData = {
  id: "i-1234567890abcdef0",
  type: "t2.micro",
  region: "us-west-2",
};

sendInstanceNotification(instance, "started");

API Reference

new SlackNotification(webhookUrl: string)

Creates a new instance of the SlackNotification class.

addSection(text: string): this

Adds a new section to the message with the given text.

addFields(fields: Array<{ title: string, value: string }>): this

Adds a new section with fields to the message.

startAttachment(color: string): this

Starts a new attachment with the specified color.

endAttachment(): this

Ends the current attachment.

send(text?: string): Promise<any>

Sends the constructed message to Slack. Optionally, you can provide text that will appear as the main message text.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.