Giter Site home page Giter Site logo

sebastianedwards / 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 800 KB

Slack Developer Kit for Node.js

Home Page: https://slackapi.github.io/node-slack-sdk

License: MIT License

JavaScript 99.93% HTML 0.07%

node-slack-sdk's Introduction

Node Slack SDK

Build Status codecov npm (scoped)

Visit the full documentation for all the lovely details.

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

Requirements

This package supports node starting from major version 0.12 and later. It's highly recommended to use the latest LTS version of node, and the documentation is written using syntax and features from that version.

Installation

Use npm to install the package and save it to your package.json:

$ npm install @slack/client

Features

The Slack platform offers several APIs to build apps. Each API delivers part of the capabilities from the platform, with a range of complexity and functionality, so that you can pick the one that fits for your app.

Slack API Outgoing Incoming NPM Package Documentation
Web API ⬆️ ⬜️ @slack/client Guide
RTM API ⬆️ ⬇️ @slack/client Guide
Incoming Webhooks ⬆️ ⬜️ @slack/client Guide
Events API ⬜️ ⬇️ @slack/events-api README
Interactive Messages ⬜️ ⬇️ @slack/interactive-messages README

Just starting out? We suggest starting at the Getting Started guide which will walk you through building your first Slack app using Node.js.

Not sure about which APIs are right for your app? Read our helpful blog post that explains and compares the options. If you're still not sure, reach out for help and our community can guide you.

Examples

Posting a message with Web API

Your app will interact with the Web API through the WebClient object, which a top level export from this package. At a minimum, you need to instantiate it with a token. The example below shows how to post a message into a channel, DM, MPDM, or group. This will require either the chat:user:write or chat:bot:write scopes.

const { WebClient } = require('@slack/client');

// An access token (from your Slack app or custom integration - xoxp, xoxb, or xoxa)
const token = process.env.SLACK_TOKEN;

const web = new WebClient(token);

// The first argument can be a channel ID, a DM ID, a MPDM ID, or a group ID
const channelId = 'C1232456';

// See: https://api.slack.com/methods/chat.postMessage
web.chat.postMessage(channelId, 'Hello there')
  .then((res) => {
    // `res` contains information about the posted message
    console.log('Message sent: ', res.ts);
  })
  .catch(console.error);

The WebClient object makes it simple to call any of the over 130 Web API methods. See the guide for details.

Posting a message with the Real-Time Messaging API

Your app will interact with the RTM API through the RtmClient object, which is a top level export from this package. At a minimum, you need to instantiate it with a token, usually a bot token.

const { RtmClient, CLIENT_EVENTS } = require('@slack/client');

// An access token (from your Slack app or custom integration - usually xoxb)
const token = process.env.SLACK_TOKEN;

// Cache of data
const appData = {};

// Initialize the RTM client with the recommended settings. Using the defaults for these
// settings is deprecated.
const rtm = new RtmClient(token, {
  dataStore: false,
  useRtmConnect: true,
});

// The client will emit an RTM.AUTHENTICATED event on when the connection data is avaiable
// (before the connection is open)
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (connectData) => {
  // Cache the data necessary for this app in memory
  appData.selfId = connectData.self.id;
  console.log(`Logged in as ${appData.selfId} of team ${connectData.team.id}`);
});

// The client will emit an RTM.RTM_CONNECTION_OPENED the connection is ready for
// sending and recieving messages
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, () => {
  console.log(`Ready`);
});

// Start the connecting process
rtm.start();

The RtmClient object makes it simple to listen for events from and send simple messages to a workspace. See the guide for details.

Posting a message with Incoming Webhooks

Incoming webhooks are an easy way to send notifications to a Slack channel with a minimum of setup. You'll need a webhook URL from a Slack app or a custom integration to use the IncomingWebhook class.

const { IncomingWebhook } = require('@slack/client');
const url = process.env.SLACK_WEBHOOK_URL;
const webhook = new IncomingWebhook(url);

// Send simple text to the webhook channel
webhook.send('Hello there', function(err, res) {
    if (err) {
        console.log('Error:', err);
    } else {
        console.log('Message sent: ', res);
    }
});

Getting Help

If you get stuck, we're here to help. The following are the best ways to get assistance working through your issue:

  • Issue Tracker for questions, feature requests, bug reports and general discussion related to this package.
  • Email us in Slack developer support: [email protected]
  • Bot Developers Hangout: a Slack community for developers building all types of bots. You can find the maintainers and users of this package in #sdk-node-slack-sdk.

node-slack-sdk's People

Contributors

acemtp avatar againer avatar anskaal avatar aoberoi avatar bertrandom avatar boctor avatar charliehess avatar clavin avatar crookedneighbor avatar dominikpalo avatar ekmartin avatar evansolomon avatar fabianthoma avatar fvgs avatar grantmd avatar houjunchen avatar markcarey avatar markshust avatar mauvm avatar moinism avatar mozamimy avatar mtchurch avatar natesilva avatar paulasjes avatar paulhammond avatar peernohell avatar rharmes avatar roach avatar sillygwailo 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.