Skip to content
This repository has been archived by the owner on Nov 4, 2019. It is now read-only.

v4 #38

Open
pveyes opened this issue Oct 31, 2016 · 0 comments
Open

v4 #38

pveyes opened this issue Oct 31, 2016 · 0 comments

Comments

@pveyes
Copy link
Contributor

pveyes commented Oct 31, 2016

This new update should solve most underlying problem when extending slack-robot outside basic message handling, like events, custom response handler.

Here's the changes needed in core:

  • Expose RTM and Web API as public property (should be just minor property name changes)
    This allow us to hook into custom events, while still providing easy request handling via robot.listen
const robot = new SlackRobot(token);
robot.rtm.on('message', message => {
  if (message.subtype === 'channel_join') {
    // new user joined channel
  }
});
  • Expose DataStore API
    Because we can access raw RTM events, we need to be able to get mapping between id and its object
const { store } = robot;
const channel = store.getChannelById(channelId);
  • New stateless Response object creation. This will simplify many logic inside Response class

Manually creating response object

import { Text } from 'slack-robot';

const { Response} = robot;
const text = Text('hello world', userId);
Response().send(text);

Stateless response object

// we didn't store target id anymore, you have to explicitly provide in Text factory
robot.listen('hello', (req, res) => {
  const text = Text('world', req.user.id);
  return res.send(text);
});
  • Remove robot.to?

Because we finally can create our own response object, maybe robot.to can be removed entirely

  • Remove res.async

This API is confusing and mostly useless because we can simply wrap async action using Promise ourselves. Given Promise is widely adopted, using res.async doesn't make sense anymore. Instead we use async/await

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

robot.listen('sleep', async (req, res) => {
  await sleep(1000);
  return res.send(Text('done', req.user.id));
});
  • Separate reaction action

Creating reaction action forces us to store timestamp inside response instance. With new reaction factory, we can just pass the message we want to react.

// message object has ts (timestamp) property
const message = { ts: '121341.12312' };
const reaction = Reaction('+1', message);
res.send(reaction);
  • Remove concurrency primitive

Using res.send allow us to remove awkward concurrency API. It's much more simple because we just use array and var args

res.send(
  // parallel
  [text, attachment, reaction],

  // serial
  text,
  attachment,
  reaction,
);
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant