super-slack-bot

0.3.1 • Public • Published

(SSB) Super Slack Bot

Short description :

The basic idea of the platform, to create a complete tool to work with bots in the environment slack.

Usually we do not want to disassemble authentication, application architecture and implementation for various protocols.

We just want to add configuration and quick to develop, so go ahead!

Requirement

Node js version 9.3.0
The app is under development(WIP)

Installation

npm install super-slack-bot

Environment variables

In the spirit The Twelve-Factor App all changing the configuration are set via environment variables.

You need create file .env in your directory and add variables.

List variables

Variable name Description
SLACK_CLIENT_ID you can find inside of your application under "Basic Information" -> "App Credentials"
SLACK_CLIENT_SECRET you can find inside of your application under "Basic Information" -> "App Credentials"
SLACK_BOT_TOKEN Token you can get a temporary or passing authentication
SLACK_BOT_NAME you can find inside of your application under "Bot User"
SLACK_VERIFICATION_TOKEN you can find inside of your application under "Basic Information" -> "App Credentials"
PORT_SERVER PORT server for conversation and command

Message

Message is of the basic concepts. There are 4 main types:

  • message.channels - for messages appearing within channels
  • message.im - for messages appearing within direct messages
  • message.groups - for messages appearing within private channels
  • message.mpim - for messages appearing within multiparty direct messages

more can be read here

Those are 4 basic concepts, they are inherited from all other subtypes.

Conversation or Interactive

In documentation you can read more.

Short, interactive messages differ from ordinary messages.

Interaction to push data in other url, which will choose in your app.

It doesn't work with web-socket, but for you it will not be quite noticeable.

Specify url
  • You must go to your app
  • Next step : Features -> Interactive Components
  • Insert your url

Example https://url/conversation

Note: For testing you can use ngrok.

He is create secure tunnel for your localhost.

// listen event 'conversation'
// in route we need to pass 'callback_id'
bot.on('conversation', async (route, response) => {
    // responseInitiator json which is refundable after the reaction
    route('welcome_button', function (responseInitiator, classConversation) {
        response.end('ok');
    });
});

Slash Commands

Read more

Commands are very similar to conversation.

They have separete url and not working with web-socket.

Specify url
  • You must go to your app
  • Next step : Features -> Slash Commands
  • Create new command and insert your url with prefix /commands

When a request comes in, it checks team_id and token verification, if not checked return response code 401.

// listen event 'command'
bot.on('command', async (route, response) => {
    route('/start', (responseInitiator, classCommand) => {
        response.end();
    });
});

Console

List commands

  • server

    Options

    • start - Start server
  • show

    Options:

    • events - A list of all available events

Tutorial on the use

Start
// include env variables
require('dotenv').config();
// require package
const SlackBot = require('./BaseBot');
// pass name and token bot
const bot = new SlackBot({
    token: process.env.SLACK_BOT_TOKEN,
    name: process.env.SLACK_BOT_NAME,
});
Listen event
// first argument pass type event 
// list event you can watch using the command : show events
 
// second callback where two arguments:
// - object Route for match regexp or string
// - object Route for match regexp or string if the message mentioned our bot
 
bot.on('message.channels', (route, routeMention) => {
    // route can take type Regexp and type String
    // if type string then expected full match
    route(/hello|hi/gi, async function (response, classMessage) {
        // reply - it means sending in response
        let res = await classMessage.reply('hello friend!');
    });
 
     routeMention('hello', async function (response, classMessage) {
         // the message will be visible only to the sender
         classMessage.replyEphemeral('hello', {
             icon_emoji: ':piggy:'
         });
     });
});

Contributions welcome!

Pull requests/forks all welcome, and please share your thoughts, questions and feature requests in Issues section.

Readme

Keywords

Package Sidebar

Install

npm i super-slack-bot

Weekly Downloads

2

Version

0.3.1

License

MIT

Unpacked Size

894 kB

Total Files

73

Last publish

Collaborators

  • agoalofalife