Skip to content

Commit

Permalink
docs: update README and change config property names
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Littlemore committed Mar 16, 2019
1 parent 93fe8ec commit 0c8beec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
56 changes: 52 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,71 @@
# botkit-middleware-typing

BotKit middleware to enable typing indicator on platforms that support it
BotKit middleware to enable the typing indicator on [Facebook](#issues) for all messages. This will include those sent via [Botkit conversations](https://botkit.ai/docs/core.html#multi-message-conversations).

## Install

Add the module to your `package.json` using either `npm` or `yarn`.

```
npm install botkit-middleware-typing
```

OR

```
yarn add botkit-middleware-typing
```

## Use

Include the middleware in your Botkit application.

```
const botkitTypingMiddleware = require('botkit-middleware-typing');
```

Add this as a [Botkit send middleware](https://botkit.ai/docs/middleware.html#send-middleware):

```
const controller = Botkit.facebookbot(botConfig);
const typingMiddleware = botkitTypingMiddleware();
controller.middleware.send.use(typingMiddleware);
```

## Configuration

You can configure the typing delay for the middleware with the following optional parameters.
This will allow you to set a maximum delay or allows you to set a delay for all typing.

| Property | Description |
| -------------------- | :-------------------------------------------: |
| maximumTypingDelayMs | Sets the maximum typing delay in milliseconds |
| typingDelayMs | Sets a specified typing delay in milliseconds |

### Examples

Set the maximum typing delay to 1 second.

```
const typingMiddleware = require('botkit-middleware-typing');
const typingMiddleware = botkitTypingMiddleware({
maximumTypingDelayMs: 1000
});
controller.middleware.send.use(typingMiddleware);
```

Set all typing delays to 4.5 seconds.

```
const typingMiddleware = botkitTypingMiddleware({
typingDelayMs: 4500
});
controller.middleware.send.use(typingMiddleware);
```

## WIP
## Issues

Don't use this yet!
With the current implementation of Botkit, there's no easy way to tell which platform the bot is running on. This middleware makes an assumption that the bot supports typing if it has a `bot.startTyping` method. This is currently avaible on Facebook and Slack. Currently only Facebook is supported but I'll look to add Slack shortly.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const isTypingMessage = message =>
message.sender_action && message.sender_action === 'typing_on';

const botkitMiddlewareTyping = (config = {}) => (bot, message, next) => {
const { maximumTimeoutDelay, timeoutDelay } = config;
const { maximumTypingDelayMs, typingDelayMs } = config;

if (botSupportsTyping(bot) && !isTypingMessage(message)) {
const typingDelayMilliseconds =
timeoutDelay || typingDelay(message, maximumTimeoutDelay);
typingDelayMs || typingDelay(message, maximumTypingDelayMs);

bot.send(typingIndicatorMessage(message), () => {
setTimeout(() => next(), typingDelayMilliseconds);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('when typing is supported by the bot platform', () => {
const givenTimeoutDelay = 10;

const sendMiddleware = typingMiddleware({
maximumTimeoutDelay: givenTimeoutDelay,
maximumTypingDelayMs: givenTimeoutDelay,
});

sendMiddleware(fakeBot, fakeMessage, spyNext);
Expand All @@ -119,7 +119,7 @@ describe('when typing is supported by the bot platform', () => {
const givenTimeoutDelay = 10;

const sendMiddleware = typingMiddleware({
timeoutDelay: givenTimeoutDelay,
typingDelayMs: givenTimeoutDelay,
});

sendMiddleware(fakeBot, fakeMessage, spyNext);
Expand Down

0 comments on commit 0c8beec

Please sign in to comment.