Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending replies in sequential execution order #39

Open
aminebenkeroum opened this issue Sep 8, 2016 · 4 comments
Open

Sending replies in sequential execution order #39

aminebenkeroum opened this issue Sep 8, 2016 · 4 comments
Labels

Comments

@aminebenkeroum
Copy link

aminebenkeroum commented Sep 8, 2016

Hello everyone,

I am currently splitting my replies into an array, because the length is superior to 320, but I can't send them in order :

let answers = currentQuestion.answer.split("\n"); answers.forEach( (answer) => { reply({"text": answer}, (err) =>{ })

In Messenger, I'm receiving them in a random order, I looked a bit around Promises, is there any way I can use them ?

Thank you

@remixz
Copy link
Owner

remixz commented Sep 8, 2016

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
  }
})

@aminebenkeroum
Copy link
Author

aminebenkeroum commented Sep 8, 2016

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 }) }); }

@remixz remixz added the question label Sep 8, 2016
@glittle
Copy link

glittle commented Nov 30, 2016

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.

@codenaz
Copy link

codenaz commented Nov 16, 2017

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);
				}
			});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants