GithubHelp home page GithubHelp logo

Comments (4)

remixz avatar remixz commented on July 30, 2024

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.

aminebenkeroum avatar aminebenkeroum commented on July 30, 2024

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.

glittle avatar glittle commented on July 30, 2024

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.

codenaz avatar codenaz commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.