GithubHelp home page GithubHelp logo

rlugojr / node-slack-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from slackapi/node-slack-sdk

0.0 2.0 0.0 566 KB

Slack client library for node.js

License: MIT License

JavaScript 99.91% HTML 0.09%

node-slack-sdk's Introduction

Node Library for the Slack APIs

Build Status codecov npm (scoped)

Read the full documentation for all the lovely details.

So you want to build a Slack app with Node.js? We've got you covered. {{ site.product_name }} is aimed at making building Slack apps ridiculously easy. This module will help you build on all aspects of the Slack platform, from dropping notifications in channels to fully interactive bots.

This is a wrapper around the Slack RTM and Web APIs.

This library provides the low level functionality you need to build reliable apps and projects on top of Slack's APIs. It:

  • handles reconnection logic and request retries
  • provides reasonable defaults for events and logging
  • defines a basic model layer and data-store for caching Slack RTM API responses

This library does not attempt to provide application level support, e.g. regex matching and filtering of the conversation stream.

Most Slack apps are interested in posting messages into Slack channels, and generally working with our Web API. Read on to learn how to use {{ site.product_name }} to accomplish these tasks. Bots, on the other hand, are a bit more complex, so we have them covered in Building Bots.

Some Examples

All of these examples assume that you have set up a Slack app or custom integration, and understand the basic mechanics of working with the Slack Platform.

Posting a message with Incoming Webhooks

Incoming webhooks are an easy way to get notifications posted into Slack with a minimum of setup. You'll need to either have a custom incoming webhook set up, or an app with an incoming webhook added to it.

var IncomingWebhook = require('@slack/client').IncomingWebhook;

var url = process.env.SLACK_WEBHOOK_URL || ''; //see section above on sensitive data

var webhook = new IncomingWebhook(url);

webhook.send('Hello there', function(err, res) {
    if (err) {
        console.log('Error:', err);
    } else {
        console.log('Message sent: ', res);
    }
});

Posting a message with Web API

You'll need a Web API token to call any of the Slack Web API methods. For custom integrations, you'll get this from the token generator, and for apps it will come as the final part of the OAuth dance.

Your app will interact with the Web API through the WebClient object, which requires an access token to operate.

var WebClient = require('@slack/client').WebClient;

var token = process.env.SLACK_API_TOKEN || ''; //see section above on sensitive data

var web = new WebClient(token);
web.chat.postMessage('C1232456', 'Hello there', function(err, res) {
    if (err) {
        console.log('Error:', err);
    } else {
        console.log('Message sent: ', res);
    }
});

Posting a message with the Real-Time Messaging API

Starting a bot up requires a bot token (bot tokens start with xoxb-), which can be had either creating a custom bot or by creating an app with a bot user, at the end of the OAuth dance. If you aren't sure path is right for you, have a look at the Bot Users documentation.

var RtmClient = require('@slack/client').RtmClient;
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;

var bot_token = process.env.SLACK_BOT_TOKEN || '';

var rtm = new RtmClient(bot_token);

// The client will emit an RTM.AUTHENTICATED event on successful connection, with the `rtm.start` payload if you want to cache it
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, function (rtmStartData) {
  console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
});

// you need to wait for the client to fully connect before you can send messages
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function () {
  rtm.sendMessage("Hello!", channel);
});

rtm.start();

node-slack-sdk's People

Contributors

acemtp avatar adamk33n3r avatar anskaal avatar aoberoi avatar arnoldstoba avatar bertrandom avatar charliehess avatar christophehurpeau avatar crookedneighbor avatar danhaller avatar ekmartin avatar eugenijusr avatar evansolomon avatar fabianthoma avatar foiseworth avatar fvgs avatar grantmd avatar houjunchen avatar i11v avatar kachizukanne avatar pakastin avatar paulasjes avatar paulhammond avatar peernohell avatar rgbkrk avatar rharmes avatar roach avatar sillygwailo avatar skovhus avatar vanm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.