@blockflow-labs/slack
v1.0.1
Published
blockflow slack wrapper
Downloads
8
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.