Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Extend replyWithTyping() with typingDelay parameter. #1303

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/readme-facebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ bot.stopTyping(message, function () {
bot.replyWithTyping(message, 'Hello there, my friend!');
```

By default a typing delay of "user is typing" signal will be calculated based on the length of the messsage. You can also override the default typing delay with `typingDelay` parameter.

```javascript
bot.replyWithTyping(message, {
text: 'Hello there, my friend!',
typingDelay: 2000 // reply to the user after 2000 ms.
});
```

## Silent and No Notifications
When sending a user a message you can make the message have either no notification or have a notification that doesn't play a sound. Both of these features are unique to the mobile application messenger. To do this add the `notification_type` field to message. Notification type must be one of the following:
- REGULAR will emit a sound/vibration and a phone notification
Expand Down
14 changes: 12 additions & 2 deletions docs/readme-web.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,21 @@ It connects a `ws` powered websocket server to the web server, and allows the ap

[A compatible webserver is provided in the starter kit](https://github.com/howdyai/botkit-starter-web/blob/master/components/express_webserver.js).


### bot.replyWithTyping(message, reply)
```javascript
bot.replyWithTyping(message, reply)
```

This works just like the normal `bot.reply()`, but instead of sending the message immediately, sends a typing indicator first, then waits for a short period before sending the actual message.

By default typing indicator time will be calculated based on the length of the messsage. You can also override the default typing delay with `typingDelay` parameter.

```javascript
bot.replyWithTyping(message, {
text: 'Hello there, my friend!',
typingDelay: 2000 // reply to the user after 2000 ms.
});
```


## Developer & Support Community
Complete documentation for Botkit can be found on our [GitHub page](https://github.com/howdyai/botkit/blob/master/readme.md). Botkit Studio users can access the [Botkit Studio Knowledge Base](https://botkit.groovehq.com/help_center) for help in managing their Studio integration.
Expand Down
17 changes: 11 additions & 6 deletions lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var crypto = require('crypto');
var bodyParser = require('body-parser');

function Facebookbot(configuration) {

var api_version = 'v2.11';
var api_host = configuration.api_host || 'graph.facebook.com';

Expand Down Expand Up @@ -164,26 +163,32 @@ function Facebookbot(configuration) {
};

bot.replyWithTyping = function(src, resp, cb) {

var textLength;
var typingDelay;

if (typeof(resp) == 'string') {
if (typeof resp === 'string') {
textLength = resp.length;
} else if (resp.text) {
textLength = resp.text.length;
} else {
textLength = 80; //default attachement text length
}

var avgWPM = 85;
var avgCPM = avgWPM * 7;
if (resp.typingDelay && typeof resp.typingDelay === 'number') {
typingDelay = resp.typingDelay;
} else {
var avgWPM = 85;
var avgCPM = avgWPM * 7;

var typingLength = Math.min(Math.floor(textLength / (avgCPM / 60)) * 1000, 5000);
typingDelay = Math.min(Math.floor(textLength / (avgCPM / 60)) * 1000, 5000);
}

bot.startTyping(src, function(err) {
if (err) console.log(err);
setTimeout(function() {
bot.reply(src, resp, cb);
}, typingLength);
}, typingDelay);
});

};
Expand Down
23 changes: 16 additions & 7 deletions lib/Slackbot_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,22 +814,31 @@ module.exports = function(botkit, config) {
* @param {function} cb optional request callback
*/
bot.replyWithTyping = function(src, resp, cb) {
var text;
var textLength;
var typingDelay;

if (typeof(resp) == 'string') {
text = resp;
if (typeof resp === 'string') {
textLength = resp.length;
} else if (resp.text) {
textLength = resp.text.length;
} else {
text = resp.text;
textLength = 80; //default attachement text length
}

var typingLength = 1200 / 60 * text.length;
typingLength = typingLength > 2000 ? 2000 : typingLength;
if (resp.typingDelay && typeof resp.typingDelay === 'number') {
typingDelay = resp.typingDelay;
} else {
var maxTypingDelay = 2000;
typingDelay = 1200 / 60 * textLength;
typingDelay = typingDelay > maxTypingDelay ? maxTypingDelay : typingDelay;
}

bot.startTyping(src);

setTimeout(function() {
bot.reply(src, resp, cb);
}, typingLength);
}, typingDelay);

};

/**
Expand Down
25 changes: 14 additions & 11 deletions lib/Web.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,29 @@ function WebBot(configuration) {
bot.typingDelay = function(message) {

return new Promise(function(resolve, reject) {
var typingLength = 0;
if (message.typingDelay) {
typingLength = message.typingDelay;
var textLength;
var typingDelay;

if (typeof message === 'string') {
textLength = message.length;
} else if (messag.text) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a typo here in the variable name

textLength = message.text.length;
} else {
var textLength;
if (message.text) {
textLength = message.text.length;
} else {
textLength = 80; //default attachement text length
}
textLength = 80; //default attachement text length
}

if (message.typingDelay && typeof message.typingDelay === 'number') {
typingDelay = message.typingDelay;
} else {
var avgWPM = 150;
var avgCPM = avgWPM * 7;

typingLength = Math.min(Math.floor(textLength / (avgCPM / 60)) * 1000, 2000);
typingDelay = Math.min(Math.floor(textLength / (avgCPM / 60)) * 1000, 2000);
}

setTimeout(function() {
resolve();
}, typingLength);
}, typingDelay);
});

};
Expand Down
Loading