Android Code Snippet Send SMS/MMS using Twilio Rest API

You need to first create an account at Twilio.com and buy a phone number for around $1.00/month. Then using the below code you can send a text message to any desired number from the twilio number you bought at about $0.01 per SMS (around 125 characters).

Receiving an SMS to your Twilio number is a bit more complex because it requires a server post-back. I also have code to send MMS messages (send text + pictures), and code to receive MMS text/pictures messages and convert them into emails with attachments. I am available for hire if you need these additional abilities :)

Here is the SMS text sending code:

B4X:
Sub SendTwilioSMS(NumbertoSendto As String, MessageToSend As String)
    'This routine sends an SMS from your Twilio.com number to specified number (must include country code, but without leading "+")
    'This routine uses the new method that can send more then 160 characters (as an MMS)

    Dim j As HttpJob
    Dim URL As String         'url to post to
    Dim P As String           'url parameters
    Dim TUserName As String   '= your twilio username
    Dim TPassword As String   '= your twilio password
    Dim TNumber As String     '= your twilio number (ex. 12125551234 - must include country code, but without leading "+")

    URL = "https://api.twilio.com/2010-04-01/Accounts/" & TUserName & "/Messages"
    P = "From=%2B" & TNumber & "&To=%2B" & NumbertoSendto & "&Body=" & URLencode(MessageToSend)

    j.Initialize("", Me)
    j.Username = TUserName
    j.Password = TPassword
    j.PostString(URL,P)

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)

        Msgbox("Message Sent OK!","SMS")
    Else
        Msgbox("There was an error sending the SMS message (" & j.ErrorMessage & ")","Error")
        Return
    End If
    j.Release
End Sub

Public Sub URLencode(InText As String) As String
    'works to url encode SMS params to twilio
    Dim C As Int 'As Integer
    Dim O As String
    Dim Z As Int 'As Long

    O = ""

    InText = InText.Replace(CRLF, Chr(10))

    For Z = 1 To InText.Length
        C = Asc(Mid(InText, Z, 1))

        If C = 13 Then C = 0
        If C = 94 Then C = 0

        Select Case C
            Case 10, 37, 38, 43, 94
                O = O & "%" & Pad(Bit.ToHexString(C), 2, "0")
            Case 32
                O = O & "+"
            Case Else
                If (C>32 And C<96) Or (C>96 And C<123) Then
                    O = O & Chr(C)
                End If
        End Select
    Next
    Return O
End Sub

Sub Pad(ItemToPad As String, ResultSize As Long, LeadPadding As String) As String
    'adds lead PADDING if ItemToPad is smaller then ResultSize
    Return Right(GenString(ResultSize, LeadPadding) & ItemToPad.Trim, ResultSize )
End Sub

Sub Right(Text As String, Length As Long) As String
    Dim R As String
    Try
        If Length > Text.Length Then Length = Text.Length
        R = Text.SubString(Text.Length - Length)
    Catch
        R = ""
    End Try
    Return R
End Sub

Sub GenString(NumChar As Long, Character As String) As String
    Dim Z As Long
    Dim R As String
    For Z = 1 To NumChar
        R = R & Character
    Next
    Return R
End Sub

Sub Mid(Text As String, Start As Int, Length As Int) As String
    Return Text.SubString2(Start-1,(Start + Length) -1)
End Sub
 
Last edited:

Pathrose

Member
Licensed User
Yes, you can send a photo and the recipient will receive the actual photo (not a link).

However, the API requires that the media (to be sent) is located at an internet address. Since phones do not have a fixed IP, you would need to first copy the media to a web server, then call the API with that address.
Hi, I would like to incorporate an SMS sending functionality into my App. It's another service provider like Twilio. They have provided the sample API in various languages like python, java, etc. How to convert this to B4A code. Just attaching the sample in VBA provided by them...Please help
  1. Public Sub Send()
  2. Dim username As String
  3. Dim password As String
  4. Dim result As String
  5. Dim myURL As String
  6. Dim Sender As String
  7. Dim numbers As String
  8. Dim Message As String
  9. Dim postData As String
  10. Dim winHttpReq As Object
  11. apikey = "yourAPIkey"
  12. Sender = "TXTLCL"
  13. numbers = "918123456789"
  14. Message = "This is your message"
  15. Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
  16. myURL = "https://api.textlocal.in/send/?"
  17. postData = "apikey=" + apikey + "&message=" + Message + "&numbers=" + numbers + "&sender=" + Sender
  18. winHttpReq.Open "POST", myURL, False
  19. winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  20. winHttpReq.Send (postData)
  21. SendSMS = winHttpReq.responseText
  22. End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Hi, I would like to incorporate an SMS sending functionality into my App. It's another service provider like Twilio. They have provided the sample API in various languages like python, java, etc. How to convert this to B4A code. Just attaching the sample in VBA provided by them...
1. You should ALWAYS start a new thread for any issue.
Posting to existing threads is a mistake.
2. Search for okhttputils2. This you have to use.
 
Top