0.0.6 • Published 6 years ago

koack v0.0.6

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

koack NPM version

Core brick to build Slack bots

Build Status Travis Status Dependency ci Status Dependency Status Coverage percentage

Nightingale

To read the full documentation on nightingale, go to nightingale

Install

npm install --save koack

API

https://christophehurpeau.github.io/koack/docs

Usages

The bot

bot.js

import { RTM_EVENTS } from 'koack/bot';
import messageRouter from 'koack/message-router';
import type { Bot } from 'koack/bot';

const loggerMiddleware = ({ event }, next) => {
  console.log(event);
  next();
};

export default (bot: Bot) => {
  bot.on(
    RTM_EVENTS.CHANNEL_JOINED,
    loggerMiddleware,
    (ctx) => console.log(ctx),
    async (ctx) => Promise.resolve(console.log(ctx)),
  );

  bot.on(
    RTM_EVENTS.MESSAGE,
    messageRouter([
      {
        commands: ['like'], // @mybot like: something
        where: ['dm', 'channel', 'group'], // default to everywhere
        mention: ['channel', 'group'], // default to everywhere except dm
        handler: (ctx) => {},
      },
      {
        commands: ['like'], // @mybot like: something
        middlewares: [
          (ctx) => {},
        ],
      },
      {
        regexp: /hello/,
        handler: (ctx) => {
          ctx.startConversation(async (say, waitResponse) => {
            say('What is your first name ?');
            const firstName = await waitResponse();
            say('And your last name ?');
            const lastName = await waitResponse();
            say(`Hello ${firstName} ${lastName}`);
          });
        }
      },
      {
        handler: (ctx) => ctx.reply('Sorry, I didn\'t understood you'),
      }
    ]),
  );
}

Serving with pool

pool.js

import { Pool } from 'koack';
import memoryStorage from 'koack/storages/memory';

const pool = new Pool({
  size: 100,
  path: require.resolve('./bot.js'),
});

const storage = memoryStorage();

storage.forEach(team => pool.addTeam(team));

const close = async () => {
  await pool.close();
  process.exit(0);
};
process.on('SIGINT', close);
process.on('SIGTERM', close);

Note: with a server, teamsIterator is handled by the storage.

Serving without pool

index.js

import { createBot } from 'koack';
import initBot from './bot';

const bot = createBot({ token });
initBot(bot);

const close = async () => {
  await bot.close();
  process.exit(0);
};
process.on('SIGINT', close);
process.on('SIGTERM', close);

Serving with a web server

A server allows:

  • to register new apps with slack button
  • to handle slash commands
  • to handle interactive buttons
import { Pool, Server } from 'koack';
import memoryStorage from 'koack/storages/memory';

const pool = new Pool({ size: 100, path: require.resolve('./bot') });

const server = new Server({
  pool: pool,
  scopes: ['bot'],
  slackClient: { clientID: ..., clientSecret: ... },
  storage: memoryStorage(),
});

server.listen({ port: Number(process.env.PORT) || 3000 });

process.on('SIGINT', () => server.stop());
process.on('SIGTERM', () => server.stop());

How to...

Extends the context

bot.context.myOwnContextMethod = () => console.log('Hello !');

Use message-events-router

import { RTM_EVENTS, RTM_MESSAGE_SUBTYPES } from 'koack/bot';
import messageEventsRouter from 'koack/message-events-router';

bot.on(
  RTM_EVENTS.MESSAGE,

  messageEventsRouter({
    events: [RTM_MESSAGE_SUBTYPES.CHANNEL_JOIN, RTM_MESSAGE_SUBTYPES.GROUP_JOIN],
    handler: ctx => {

    },
  }),
);

Interactive messages

server.js

import { Pool, Server } from 'koack';
import interactiveMessage from 'koack/interactive-messages';

const pool = new Pool({ ... });
const server = new Server({ pool, ... });

server.use(interactiveMessage({
  pool,
  url: '/interactive-message', // optional
  token: 'xxxx',
}));

bot.js

import { INTERACTIVE_MESSAGE_REPONSE } from 'koack/interactive-messages';

bot.on(
  INTERACTIVE_MESSAGE_REPONSE,

  (ctx) => {
    ctx.replyEphemeral('text', { replace: false });
    // or
    ctx.reply('text', { replace: false })
  },
);

Dev with HTTPS

For some features like interactives messages, you need to use https. They are many way to achieve it, here the most simple we know:

  • install globally localltunnel with npm install -g localtunnel
  • start your bot
  • bind your localtunnel in another console lt --port 4321 (put the port you use in your .env file)

See localtunnel for more details