How to send sms via gatewayapi.com

 

In this example we show how to send an sms to one or several addresses from Seminar Pro

 

Requirement: Account at gatewayapi.com and valid access token.

https://gatewayapi.com/docs/apis/rest/

 

Example XojoScript:

 

Dim modul As String = GetActiveModul

 

If modul = "addresses" Then

 

// Retrieve all IDs of selected Addresses

Dim arID() As String

arID = IDArrayOfSelectedRecords

 

// How many adresses are selected?

Dim numRecs As Integer

numRecs = Ubound(arID)+1

 

/// **************** Begin Showing User Dialog ***********

// Prepare Texts to show in SMS dialog window

Dim arTxt() As String

Dim lbl As String            = "Number of recipients : " + Str(numRecs)

Dim defaultSender As String  = "COMPANY" // max 11 chars

Dim defaultMessage As String = EOL + "==" + EOL + "Do not reply"

Dim dialogTitle As String    = "SEND SMS"

 

// Ask user for sms text and sender

arTxt = SendSms(lbl,defaultSender,dialogTitle, defaultMessage)

 

Dim sender As String = arTxt(0)

Dim msg    As String = arTxt(1)

 

If msg = "" Then Return

/// **************** End Showing User Dialog **********

 

// Now we want to retrieve the mobilephone numbers of selected addresses:

 

// prepare sql statement

Dim idStr As String

idStr = Join(arID,",")

Dim sql As String

sql = "SELECT MobilePhone FROM " + GetPrefix + "addresses WHERE id IN (" + idStr + ")"

 

// Query database and store result in Array

Dim MobileNumbers() As String

Dim FieldDelimiter As String = "**"

MobileNumbers = SQLSelect(sql,FieldDelimiter)

 

If Ubound(MobileNumbers) = -1 Then

Return

End If

 

// Create a new JSON object

Dim handleContent      As Integer = JsonCreate

Dim handleRecipients   As Integer = JsonCreate

Dim handleOneRecipient As Integer

 

// Add key-value pairs to the JSON object

JsonSetValue(handleContent,"message", msg)

JsonSetValue(handleContent,"sender", sender)

 

For i As Integer = 0 To Ubound(MobileNumbers)

Dim mobile As String = ReplaceAll(Trim(MobileNumbers(i))," ","")

mobile = ReplaceAll(mobile,"+","")

mobile = ReplaceAll(mobile,"-","")

 

// Todo: remove any non-digits

 

If mobile <> "" Then

// Add one recipient

handleOneRecipient = JsonCreate

JsonSetValue(handleOneRecipient,"msisdn",mobile)

// Add this new recipient to Array of recipients

JsonAppend(handleRecipients,handleOneRecipient)

End If

Next

 

// Add whole recipients Array to Json Content object

JsonSetValueArray(handleContent,"recipients",handleRecipients)

 

// Convert JSON object to a string

Dim js As String = JsonToString(handleContent)

 

'MsgBox(js)

 

// Create new URLConnection

Dim conID As String = htmlConnection

 

Dim response As String = ""

If conID <> "ERROR" Then

 

// Set RequestHeader

response = htmlRequestHeader(conID,"Content-Type","application/json; charset=utf-8")

If response <> "OK" Then

MsgBox("RequestHeader : " + response)

Return

End If

 

// Set RequestContent: Json String js

response = htmlRequestContent(conID,js,"application/json")

 

If response = "OK" Then

// Get your own token from gatewayapi.com!

// Example: zGUs7Y_PQMKd34XbruRrYGJiN-lcZnEU4BKdz5fdpFwU-QW5C8qf4x0_zrYYyr

Dim myToken As String = GetProperty("GatewayApiToken")

 

// Define URL for GET request

Dim URL As String = "https://gatewayapi.com/rest/mtsms?token=" + myToken

 

// Send request out to webservice

response = htmlsendSync(conID,"POST",URL,15)

 

// Parse JSON Response, show cost.

'MsgBox(response)

 

Dim h As Integer = JsonCreateFromString(response)

Dim usage As String

Dim crncy, total_cost As String

 

If h >= 0 Then

usage = JsonGetValue(h,"usage")

Dim h2 As Integer = JsonCreateFromString(usage)

If h2 >= 0 Then

crncy      = JsonGetValue(h2,"currency")

total_cost = JsonGetValue(h2,"total_cost")

End If

 

MsgBox("SMS sending cost : " + crncy + " " + Format(Val(total_cost),"###,##0.00"))

End If

 

End If

 

// Release connection

Call htmlConnectionClose(conID)

End If

 

// Release JSON Objects

JsonclearAll

End If