Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send sms via twillo provider

sal21

Programmer
Apr 26, 2004
445
IT
I just have an account on Twillo.
I need to use this api code to send sms.

curl 'https://api.twilio.com/2010-04-01/Accounts/123456789/Messages.json' -X POST \
--data-urlencode 'To=+39123456789' \
--data-urlencode 'From=+12345687' \
--data-urlencode 'Body=cvbcvbcvbcvb' \
-u 123456789[AuthToken]

how to with vb 6.0.?
Tks
 
Last edited:
Start with something like this:

Ensure the MSXML2 library is registered on your system.
Microsoft XML 3.0 or later installed and registered on your system.


Code:
Private Sub SendSMS()
    Dim xmlHttp As Object
    Dim URL As String
    Dim AccountSID As String
    Dim AuthToken As String
    Dim EncodedAuth As String
    Dim ToNumber As String
    Dim FromNumber As String
    Dim Body As String
    Dim PostData As String

    ' Twilio account details
    AccountSID = "123456789" ' Replace with your actual Account SID
    AuthToken = "YourAuthToken" ' Replace with your actual Auth Token
    URL = "https://api.twilio.com/2010-04-01/Accounts/" & AccountSID & "/Messages.json"
   
    ' SMS details
    ToNumber = "+39123456789" ' Replace with the recipient's phone number
    FromNumber = "+12345687" ' Replace with your Twilio phone number
    Body = "cvbcvbcvbcvb" ' Replace with your message body

    ' Encode authentication in base64
    EncodedAuth = Base64Encode(AccountSID & ":" & AuthToken)
   
    ' Prepare POST data
    PostData = "To=" & URLEncode(ToNumber) & "&From=" & URLEncode(FromNumber) & "&Body=" & URLEncode(Body)

    ' Create an XMLHTTP object
    Set xmlHttp = CreateObject("MSXML2.XMLHTTP")

    ' Make the POST request
    xmlHttp.Open "POST", URL, False
    xmlHttp.setRequestHeader "Authorization", "Basic " & EncodedAuth
    xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    xmlHttp.Send PostData

    ' Check the response
    If xmlHttp.Status = 201 Then
        MsgBox "SMS sent successfully!"
    Else
        MsgBox "Error sending SMS: " & xmlHttp.responseText
    End If

    ' Clean up
    Set xmlHttp = Nothing
End Sub

' Function to URL encode a string
Private Function URLEncode(ByVal str As String) As String
    Dim i As Long
    Dim ch As String
    Dim res As String
    res = ""
    For i = 1 To Len(str)
        ch = Mid(str, i, 1)
        Select Case Asc(ch)
            Case 48 To 57, 65 To 90, 97 To 122 ' 0-9, A-Z, a-z
                res = res & ch
            Case Else
                res = res & "%" & Right("0" & Hex(Asc(ch)), 2)
        End Select
    Next i
    URLEncode = res
End Function

' Function to encode a string in Base64
Private Function Base64Encode(ByVal str As String) As String
    Dim objXML As Object
    Dim objNode As Object
    Set objXML = CreateObject("MSXML2.DOMDocument")
    Set objNode = objXML.createElement("base64")
    objNode.DataType = "bin.base64"
    objNode.NodeTypedValue = StrConv(str, vbFromUnicode)
    Base64Encode = objNode.Text
    Set objNode = Nothing
    Set objXML = Nothing
End Function
 

Part and Inventory Search

Sponsor

Back
Top