One Shortcoming That Twilio Would not Need You to Discover Out

I not too long ago needed to create a Servo chatbot utilizing textual content messaging for a demo for a big firm. We determined to make use of Twilio as the plain alternative — purchased some numbers, created the Servo-Twilio driver, and applied the bot. Nevertheless, within the hours earlier than the presentation, one thing broke.

Brief Message Service is likely one of the greatest channels to get a message throughout. Everybody with a smartphone can ship and obtain messages, no matter which immediate messenger they use. It could possibly include photos and hyperlinks, and you’ll ship it as a push notification with out a lot trouble.

A number of firms supply easy-to-handle APIs that permit programmatic SMS dealing with. Some are less-known and localized to their very own nation, however clearly, the worldwide chief is Twilio, which presents builders some ways to combine messaging, voice, and even emails — ranging from RESTful API and ending in Autopilot visible movement editor. It appears that evidently a developer can simply construct a chatbot, for instance, that communicates over textual content messages.

Or can it? Properly, not many builders know, however Twilio doesn’t assure the order of its messages! Because of this in case you’d prefer to ship your clients a reminder that’s cut up throughout a number of sentences, you may be heading for hassle. For instance, what can be extra pure than sending the next four messages:

Hello!
It appears you have not activated your account. The sooner you activate it, the extra you save!
Would you want me that can assist you activate it?

However because it seems, these may seem on the shopper’s telephone as:

The sooner you activate it, the extra you save!
Would you want me that can assist you activate it?
It appears you have not activated your account.
Hello!

How embarrassing is that?

The issue is that because of the provider’s historic infrastructure, one can not assure the order upon arrival, as Twilio itself admits.

Happily, there are some methods round it. We got here throughout the issue when growing a channel driver for Twilio in Servo’s conversational framework. Sadly, this discovery was made only one hour earlier than an necessary demo to a consumer, so we couldn’t repair it immediately. However proper after the assembly, we began hacking away on the keyboard, making an attempt a couple of choices.

Essentially the most pure technique that got here to thoughts was to permit a while between the messages. It has the disadvantage that it will gradual the dialog, however the hope was that 500ms delay wouldn’t be too unhealthy. Nevertheless, this didn’t work properly sufficient.

The following evolution step was easy sufficient, and that one labored. We merely waited till Twilio drivers acknowledged that the message was acquired — by sending again the response — after which wait 500ms, or what was left of it. As at all times, it’s higher to make use of decoupled queues to implement it. I’ll provide the essence right here, and in case you want the remainder of the main points, verify the code itself in GitHub beneath the server/chat folder.

When a message arrives the channel driver for sending, we put it right into a messages queue.

... _clients[process.fsm_id].messages.push(msg);
...

And as soon as for each dialog, we name a recurrent perform that might extract this message from the queue:

if (!_clients[process.fsm_id].messages.size) {
this.send1Message(msg, course of);
}

Which might name:

perform send1Message(msg, course of) { twilio.sendMessage(msg, (err, responseData) => { if (!err) { setTimeout(() => { msg = _clients[process.fsm_id].messages.pop(); this.send1Message(msg, course of);// <== recursive loop }, this.timeoutMs); } else { console.error('twilio message err', err.message + ' ' + err.moreInfo, msg); } });
}

That’s it! It labored.

And what about that demo? Properly, in any case that, the demo went properly. Unexpectedly, luck was on our aspect that day.

readofadmin

Leave a Reply

Next Post

Automation Software program and seven Essential Questions

Mon Jul 15 , 2019
The Finest Inquiries to Ask for Reaping Automation Software program’s Advantages In terms of automation software program, most companies can expertise the advantages of RPA. Whether or not it’s to scale a enterprise, cut back human error, enhance efficiency, or enhance worker and buyer satisfaction, robotic automation could be a […]
Wordpress Social Share Plugin powered by Ultimatelysocial