newbie_developer
newbie_developer

Reputation: 21

How to concatenate long incoming text in twilio

I'm using Twilio to send/receive texts. When I receive a long text message beyond 160 character, it comes in as multiple segments appearing as individual text messages. Is there anyway to identify them as different segments of the same text and concatenate them? I'm using Twilio's Java helper libraries. Any help will be much appreciated. Thanks.

Upvotes: 2

Views: 1188

Answers (3)

John Nagle
John Nagle

Reputation: 1568

Twilio now does reassembly on their servers. So all this is now unnecessary.

Upvotes: 0

John Nagle
John Nagle

Reputation: 1568

Twilio has a feature in beta to deal with this, and it works. You can ask Twilio to turn on multipart message segment info for your account. This gets you three new parameters for an incoming SMS:

   "SegmentNum"      # The order of this SMS Sid in relation to the total number of segments.
   "TotalSegments"   # total parts of multipart msg
   "ConcatRef"       # reference number or ID of the original message over 160 characters. 

Reassembly is your problem, but it's not hard. Multipart SMS messages come in parts, sent separately by the sending phone. All parts of the same message have the same ConcatRef and source phone number. (ConcatRef is a small number, usually < 256, generated by the sender, not Twilio.) TotalSegments appears in each part and is the total number of parts of the message. SegmentNum is numbered from 0 to TotalSegments-1. When you have all the parts, you can reassemble the message by concatenating the Body fields in SegmentNum order.

Any leftover parts that don't match within a few minutes should be treated as errors. Don't keep them indefinitely, because ConcatRef numbers will cycle after a few hundred messages and mess up reassembly of future messages. (This was a known IOS and Android bug. Both keep unmatched message parts forever, resulting in strange reassemblies with old message parts. Drives users nuts.)

Upvotes: 3

Gaurav Dave
Gaurav Dave

Reputation: 7474

With the Messages resource URI, you can send messages containing up to 1600 characters. Learn about the functionality here.
Note: the resource URI is deprecated and does not support >160 characters.

For outgoing messages, concatenated messages are supported when sending messages to most carriers* in the United States and Canada. With the Messages resource URI, you can send messages containing up to 1600 characters. When we receive your request, Twilio will auto segment messages with more than 160 characters and the messages will be automatically reassembled on the destination handset.

You may also send messages over 160 characters to international phone numbers, but very likely they will appear as multiple messages on the destination handset.

For incoming messages that are greater than 160 characters, the sending carrier will break up the message behind the scenes before delivering them to us, so Twilio will treat them as separate incoming messages and deliver them to your application in the order we receive them.

Referred: Source

Upvotes: 1

Related Questions