How to Send Text Messages (Twilio)
Das Goravani
goravanis at gmail.com
Mon Mar 13 14:43:42 UTC 2023
Hello all,
I managed to get my first text message back from Twilio. The authentication approach that Kelly showed me did the trick. I am putting it all in this message for posterity and anyone who is interested.
To talk to Twilio to send SMS Text messages for 2FA Two Factor Authentication (or any other purpose), you do the following.
First of all you use the HTTP Post command. You use it in a straightforward manner except for the Headers List and Authentication which contain a cool trick..
Near the top of your routine will be these standard lines:
Calculate iHost as 'api.twilio.com'
Calculate iURI as '/2010-04-01/Accounts/AC75801fa7b99e6025f8816739b5ecdc9e/Messages.json'
Calculate iCode as randintrng(100000,900000). (Your 2FA 6 digit code that they have to enter back on your page to pass)
Do iCodeList.$add(iCode) (Keep track of the codes you have sent them so you can check if they entered a real code)
Calculate iUserName as 'AC75801fa7XXXXXXXXXXXXX39b5ecdc9e'
Calculate iPassword as '410a9c44XXXXXXX6ea2f487e7c30ae'
Calculate iMobilePhone as '14243539546'. (This is the phone you want to text to, my phone in this case)
Where iUserName and iPassword are the two strings Twilio gives you for identification. They call the user name one the account SID and the other one is the authorization token.
Your contents list has 3 lines, for To, From, and Body. You just put those values into the contents list. No need to try to encode them or anything.. Omnis does that for you in the case of this command.
Do iContentList.$add('From','15676777733') (The phone number you bought from Twilio to send with)
Do iContentList.$add('To',iMobilePhone). (Phone to send to, make sure it is like this: 14152348475
Do iContentList.$add('Body',con('Your Insurance Verification Number is: ',iCode))
For your headers list, pack it in the following way:
Do iHeadersList.$define(iHeaderName,iHeaderValue)
Calculate authString as con(iUserName,':',iPassword)
Calculate authHeader as con('Basic ',utf8tochar(bintobase64(authString)))
Do iHeadersList.$add('Authorization',authHeader)
Note the space after the word Basic
At the end of your routine you actually call the command as under:
HTTPPost (iHost,iURI,iContentList,iHeadersList,'',kTrue,kFalse,kFalse) Returns iSocket
HTTPRead (iSocket,iBuffer) Returns iBytesReceived
HTTPClose (iSocket)
Note that Security is marked as kTrue. Twilio needs it to be secure.
You read right after you post so that you get their return content in iBuffer. This is straight text they send back.
If your message was sent the message will contain the word Created near the top.
That’s it. Now text messaging for everyone on Omnis !
As far as cost goes, it’s extremely low. You pay like $1.50 for a phone number per month. Then the messages are really cheap. Twilio is geared towards those wanting to send many thousands of text messages, like advertisements. For the 2FA users like us we never rack up a serious bill with Twilio doing what we do.
Thanks to Kelly Burgess for the Headers List trick. I never would have thought that up in a million years.
More information about the omnisdev-en
mailing list