Skip to content

Commit

Permalink
Update README + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Littlemore committed Nov 20, 2018
1 parent 554196a commit d34ab58
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# botkit-middleware-typing

BotKit middleware to enable typing indicator on platforms that support it

## WIP

Don't use this yet!
68 changes: 34 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@ const AVERAGE_CHARACTERS_PER_MINUTE = AVERAGE_WORDS_PER_MINUTE * 7;
const DEFAULT_ATTACHMENT_TEXT_LENGTH = 80;

const calculateTypingDelay = response => {
let textLength;

if (typeof response === 'string') {
textLength = response.length;
} else if (response.text) {
textLength = response.text.length;
} else {
textLength = DEFAULT_ATTACHMENT_TEXT_LENGTH;
}

return Math.min(
Math.floor(textLength / (AVERAGE_CHARACTERS_PER_MINUTE / 60)) * 1000,
5000
);
let textLength;

if (typeof response === 'string') {
textLength = response.length;
} else if (response.text) {
textLength = response.text.length;
} else {
textLength = DEFAULT_ATTACHMENT_TEXT_LENGTH;
}

return Math.min(
Math.floor(textLength / (AVERAGE_CHARACTERS_PER_MINUTE / 60)) * 1000,
5000
);
};

const typingIndicatorMessage = (bot, message) => ({
recipient: { id: message.to },
channel: message.channel,
sender_action: 'typing_on'
recipient: { id: message.to },
channel: message.channel,
sender_action: 'typing_on',
});
const botSupportsTyping = bot => bot.startTyping;

const isTypingMessage = message =>
message.sender_action && message.sender_action === 'typing_on';
message.sender_action && message.sender_action === 'typing_on';

const botkitMiddlewareTyping = (bot, message, next) => {
const send = () => {
console.log('Bot typing middleware');
if (botSupportsTyping(bot) && !isTypingMessage(message)) {
const typingIndicator = typingIndicatorMessage(bot, message);
const typingDelay = calculateTypingDelay(message);
bot.send(typingIndicator, () => {
setTimeout(() => next(), typingDelay);
});
} else {
next();
}
};

return {
send
};
const send = () => {
console.log('Bot typing middleware');
if (botSupportsTyping(bot) && !isTypingMessage(message)) {
const typingIndicator = typingIndicatorMessage(bot, message);
const typingDelay = calculateTypingDelay(message);
bot.send(typingIndicator, () => {
setTimeout(() => next(), typingDelay);
});
} else {
next();
}
};

return {
send,
};
};

module.exports = botkitMiddlewareTyping;

0 comments on commit d34ab58

Please sign in to comment.