botbuilder-helpers
Version:
helpers for microsoft botbuilder framework
71 lines (59 loc) • 1.73 kB
JavaScript
const botbuilder = require('botbuilder');
const TYPING = { type: botbuilder.ActivityTypes.Typing };
const avgWPM = 150;
const avgCPM = avgWPM * 7;
const typingDelayFactor = 1;
const getTypingLength = textLength => Math.min(Math.floor(textLength / (avgCPM / 60)) * 1000, 2000) * typingDelayFactor;
/**
* in validation are sent TurnContext
* @param {*} context
*/
const inVariantContext = (context) => {
if (!context || typeof context !== 'object' || context === null) {
throw new Error('Wrong context for sendWithTyping.');
}
return false;
};
/**
* use for sending clean texts
* @param {*} context
* @param {*} text
*/
const sendTextWithTyping = async (context, text) => {
if (typeof text !== 'string') {
throw new Error('text is required.');
}
inVariantContext(context);
return context.context.sendActivities([
TYPING,
{ type: 'delay', value: getTypingLength(text.length) },
{ text },
]);
};
exports.sendTextWithTyping = sendTextWithTyping;
/**
* for sending clean typing
* @param {*} context
* @param {*} text
*/
const sendTyping = async (context, length) => {
inVariantContext(context);
return context.context.sendActivities([
TYPING,
{ type: 'delay', value: getTypingLength(length || Math.floor(Math.random() * 2000) + 1000) },
]);
};
exports.sendTyping = sendTyping;
/**
* use for prompts etc...
* for clean text use sendTextWithTyping
* @param {*} context
* @param {*} stepActionCallback
* @param {*} length
*/
const stepActionWithTyping = async (context, stepActionCallback, length) => {
inVariantContext(context);
await sendTyping(context, length);
return stepActionCallback();
};
exports.stepActionWithTyping = stepActionWithTyping;