Comments (4)
I'd try using a library like async, which allows you to do operations over an iterable like an array, but with callbacks so you can guarantee an order. For your use case, I'd do something like:
let answers = currentQuestion.answer.split('\n');
async.eachSeries(answers, (answer, cb) => {
reply({ text: answer }, cb);
}, (err) => {
// this function is called either when all the answers have been sent, or if an error is thrown
if (err) {
// deal with err
}
})
from messenger-bot.
I did the same with the Promise API, But it seems that the bug is coming from Facebook :) there is no queues in sending messages.
const sendingInOrder = (message,reply) => { return new Promise( function (resolve, reject) { reply({text:message},error =>{ if(!error) { setTimeout(()=>{ resolve({success: "Message sent"}); },6000) } else reject(error); // failure }) }); }
from messenger-bot.
I prepare what I want to send in an array of strings, then call this:
const maxAnswerLength = 319;
function sendAllAnswers(answers, profileId, originalAnswers) {
if (!originalAnswers) {
originalAnswers = JSON.parse(JSON.stringify(answers));
}
var keepGoing = true;
if (answers.length) {
// assume no single text is too long
var answerText = answers.shift();
for (var i = 0; keepGoing; i++) {
if (!answers.length // past the end
|| (answerText && (answerText + answers[0]).length > maxAnswerLength)
|| answers[0] === '') {
bot.sendMessage(profileId, { text: answerText }, (err) => {
if (err) {
console.log(err);
console.log(answerText);
} else {
console.log(`Sent: ${answerText}`)
setTimeout(function () {
sendAllAnswers(answers, profileId, originalAnswers);
}, 500);
}
});
keepGoing = false;
} else {
answerText = [answerText, answers.shift()].join('\n');
}
}
}
}
This basically combines as many short strings from the array as possible and sends them out, pausing for 1/2 second between each. I usually send out between 1 and 3 messages. So far this has worked well.
from messenger-bot.
I am not good with Javascript but you can try this. Works well and also gives you the typing bubble so the user knows you are still typing
var messages = ['Hello','How are you'];
bot.sendSenderAction(payload.sender.id, 'typing_on');
async.eachSeries(messages,(message,cb)=>{
setTimeout(
function(){
reply({text:message},cb);
},2000);
bot.sendSenderAction(payload.sender.id, 'typing_on');
},(err)=>{
if(err){
console.log(err);
}
});
from messenger-bot.
Related Issues (20)
- Your test middleware_post has a bug in it
- Temporary Error
- referral event handler is not working HOT 3
- Trying to get setGetStartedButton working HOT 2
- Undefined Error on like
- Updating Persistent Menu for Platform 1.4 HOT 4
- npm package on package.json HOT 2
- Cannot read property 'forEach' of undefined HOT 2
- Support for messages with (image) attachments
- Greeting Text
- Bot throwing undefined 'forEach' on functions without parsed.entry[0].messaging[0] HOT 1
- built-in NLP support? HOT 2
- getProfile() return undefined HOT 1
- An in-range update of request is breaking the build 🚨 HOT 4
- The package in NPM is not updated HOT 2
- Version 10 of node.js has been released HOT 1
- Getting Cannot read property 'forEach' of undefined HOT 1
- Facebook Graph API 2.6v will be deprecated. HOT 4
- More examples for bots HOT 1
- Insufficient permission to access user profile HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from messenger-bot.