DEV Community

star__hoshi
star__hoshi

Posted on

2 2

Handle Slack's Incoming Webhook easily and type-safely in TypeScript

slackapi/node-slack-sdk is great slack client. However, it does not contain d.ts files. So I made a library with type definition.

typed-slack has type definitions, so you can handle slack-api easily and type-safely.

Code completion is enabled when using VSCode.

vscode\.gif \(898×298\)

Install

npm install typed-slack
Enter fullscreen mode Exit fullscreen mode

Usage

Webhook

import * as Slack from 'typed-slack'

let slack = new Slack.IncomingWebhook('https://hooks.slack.com/services/.......')
slack.send({ text: 'text' }).then(e => {
  console.log('success')
}).catch(e => {
  console.error(e)
})
Enter fullscreen mode Exit fullscreen mode

Use options

More optional parameters are here#typed-slack.d.ts

const options = <Slack.IncomingWebhookOptions>{
  text: '@star__hoshi Hi!',
  channel: 'debug',
  icon_emoji: ':smile:',
  link_names: 1,
  attachments: [
    {
      color: Slack.Color.Danger,
      fields: [
        {
          title: 'Priority',
          value: 'High',
          short: false
        }
      ],
      image_url: 'http://my-website.com/path/to/image.jpg',
      ts: 123456789
    }
  ]
}
await slack.send(options)
Enter fullscreen mode Exit fullscreen mode

starhoshi/typed-slack

Please use typed-slack !

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay