B4J Code Snippet SMS cost of the tariff plan of the smartphone with mysms API

Installare sul telefono l'app mysms - SMS dal Computer
Link: https://play.google.com/store/apps/details?id=com.mysms.android.sms&hl=it

Richiedere la APIKey per lo sviluppare su questo
Link: https://www.mysms.com/developer

B4X:
    Type ServiceMySMS  (SMSApiKey   As String, _
                        SMSmsisdn As String, _
                        SMSPassword As String, _
                        SMSCountry As String, _
                        SMSTo As String , _
                        SMSMessaggio As String, _
                        SMSLinkAPI As String , _
                        SMSAuthToken As String)
    Dim mySMSSend As ServiceMySMS
    mySMSSend.SMSApiKey = "....."
    mySMSSend.SmSmsisdn =""         'Cellulare registrato su mySMS
    mySMSSend.SMSPassword = "...."    'Password
    mySMSSend.SMSCountry  = "39"    'Nazione del cellulare di destinazione
    mySMSSend.SMSTo = ""             'Cellulare del destinatario SENZA +39 (Max 50 numeri per volta)
    mySMSSend.SMSMessaggio = ""        'Messaggio max 160 caratteri
    mySMSSend.SMSLinkAPI = ""
    mySMSSend.SMSAuthToken = ""

  Type ServiceMySMSErrCode  (SMSReturnCode  As Int, _
                 SMSErrCode      As Int, _
                 SMSErrDesc      As String)
   Dim mySMSErrore As ServiceMySMSErrCode
   mySMSErrore.SMSReturnCode = 0
   mySMSErrore.SMSErrCode = 0
   mySMSErrore.SMSErrDesc = ""

  Private mySMS_USER_LOGIN   = "sms_user_login" As String  
  Private mySMS_REMOTE_SMS   = "sms_remote_sms" As String


B4X:
Private Sub mySMS_Login
'============================================================
' Autenticazione LOGIN
'============================================================ 
'https://api.mysms.com/el_ns0_userLoginRequest.html
'https://api.mysms.com/json/user/login
'============================================================ 
'    Example JSON - REQUEST
'{
'  "msisdn" : ...,
'  "password" : "...",
'  "key" : "...",
'  "checkKey" : false,
'  "apiKey" : "..."
'}
'JSON
'-------------------------------------------------------------
'property         |Type                 |description
'-------------------------------------------------------------
'msisdn            |msisdn (long)         |The user's verified mobile number
'password        |password (string)     |The user's password
'key             |key (string)         |The optional key (Max. 64 chars) For login check. If set And checkKey Is True the key must match the last used key.
'checkKey         |checkKey (boolean) |If True the provided key will be checked otherwise the provided key will overwrite the old one after successful login.
'============================================================
'    Example JSON - RESPONSE
'{
'  "authToken" : "...",
'  "info" : "...",
'  "dateVerified" : "...",
'  "countryCode" : "...",
'  "dialPrefix" : ...,
'  "currencyCode" : "...",
'  "errorCode" : ...
'}
'============================================================
'ERROR CODES
'------------------------------------------------------------
' | 97    | The access To the api was denied
' | 98    | You made too much requests in a short time
' | 99    | The service Is currently Not available
' | 101    | The credentials are wrong
' | 107    | The user msisdn Not exists
' | 108    | The key Is wrong
' | 109    | The login Is blocked For a specific time, because some wrong logins were made
' | 600    | The api key Is invalid
'============================================================
    lblMsgSMS.Text = ""
    mySMSSend.SmSmsisdn = "39" & UsaCellulareSender         
    Dim tmpParametri As String = $"{
                              "msisdn" : "$ & mySMSSend.SMSmsisdn & $",
                              "password" : ""$ & mySMSSend.SMSPassword & $"",
                              "key" : "",
                              "checkKey" : false,
                              "apiKey" : ""$ & mySMSSend.SMSApiKey & $""
                            }"$
'    Log (tmpParametri)                     
    mySMSSend.SMSLinkAPI = "https://api.mysms.com/json/user/login"
    ExecuteRemoteQuery(mySMSSend.SMSLinkAPI, tmpParametri, mySMS_USER_LOGIN) 
End Sub

Private Sub mySMS_Message_Send (tmpDestinatariSMS As String)
'============================================================
' Invio SMS
'============================================================ 
'https://api.mysms.com/el_ns0_remoteSmsSendRequest.html
'https://api.mysms.com/json/remote/sms/send
'============================================================
'    Example JSON - REQUEST
'{
'  "recipients" : [ "...", ... ],
'  "message" : "...",
'  "dateSendOn" : "...",
'  "encoding" : ...,
'  "smsConnectorId" : ...,
'  "store" : False,
'  "authToken" : "...",
'  "apiKey" : "..."
'}
'JSON
'-------------------------------------------------------------
'property         |Type                              |description
'-------------------------------------------------------------
'recipients     |Array of recipients(string)    |The recipients in international format (+436761234567)
'message         |message            (string)     |The message To send
'dateSendOn     |dateSendOn         (DateTime)     |The optional date when the message should be delivered. If Not given it will be delivered immediately.
'encoding         |encoding             (int)         |The encoding For the message (0 => 7-Bit, 1 => 8-Bit, 2 => UCS2)
'smsConnectorId    |smsConnectorId     (int)         |The sms connector id To be used (optional). Leave zero For normal operator sms.
'store            |store                 (boolean)     |True If the messages should be stored on the server As outgoing messages. In Case of failed delivery the will be marked As unsent.
'============================================================
'    Example JSON - RESPONSE
'{
'  "requestId" : ...,
'  "dateSent" : "...",
'  "remoteSmsSendAcks" : [ {
'    "recipient" : "...",
'    "messageId" : ...
'  }, ... ],
'  "errorCode" : ...
'}
'============================================================
'ERROR CODES
'------------------------------------------------------------
' | 2    | A required parameter was Not given
' | 97    | The access To the api was denied
' | 98    | You made too much requests in a short time
' | 99    | The service Is currently Not available
' | 100    | The auth token Is invalid
' | 700    | One Or more recipients are Not in the correct format Or are containing invalid msisdns (i.e: 436761234567)
' | 701    | You only are allowed To send a sms To 50 Recipients per request
' | 702    | One Or more recipients are blocked by the system.
' | 600    | The api key Is invalid
'============================================================
    lblMsgSMS.Text = ""
    mySMSSend.SMSMessaggio = txtTestoSMS.Text.Trim
    Dim tmpParametri As String = $"{
            "recipients" : [ "$ & tmpDestinatariSMS & $" ],
              "message" : ""$ & mySMSSend.SMSMessaggio & $"",
              "dateSendOn" : "",
              "encoding" : 0,
              "smsConnectorId" : 0,
              "store" : true,
              "authToken" : ""$ & mySMSSend.SMSAuthToken & $"",
              "apiKey" : ""$ & mySMSSend.SMSApiKey & $""
            }"$
'    Log (tmpParametri) 
    mySMSSend.SMSLinkAPI = "https://api.mysms.com/json/remote/sms/send"
    ExecuteRemoteQuery(mySMSSend.SMSLinkAPI, tmpParametri, mySMS_REMOTE_SMS)
End Sub

Sub ExecuteRemoteQuery(tmpUsaLink As String, tmpParamentri As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString(tmpUsaLink, tmpParamentri)            'Uso Metodo POST
    job.GetRequest.SetContentType ("application/json")    'Solo per Metodo POST
    job.GetRequest.SetContentEncoding("utf-8")            'Solo per Metodo POST
End Sub

Sub JobDone(job As HttpJob)

    If job.Success Then
        Dim res As String
        res = job.GetString
'        Log (res)
        Dim parser As JSONParser
        parser.Initialize(res)
     
        Select job.JobName
            Case mySMS_USER_LOGIN
                CallSub (Me, "Progress_Dialog_Close")
                Dim m As Map
                m = parser.NextObject
'                Log(m.Get("errorCode"))
                mySMSErrore.SMSReturnCode = 0
                lblMsgSMS.Text = ""
                Select m.Get("errorCode") 
                    Case 0
                        mySMSSend.SMSAuthToken = m.Get("authToken")
'                        lblMsgSMS.Text = "Autenticato correttamente"
                        Dim tmpContaSMS As Int = 0
                        Dim tmpNumberDestinazione As String = ""     
                        For i = 0 To tblElencoDati.items.Size - 1 Step 1
                            Dim r() As Object = tblElencoDati.Items.Get(i)
                            Dim tmpReadCellulare As String = r(6)
                            If tmpReadCellulare.Length > 0 Then
                                tmpReadCellulare = "+39" & tmpReadCellulare
                                tmpNumberDestinazione = tmpNumberDestinazione &""""& tmpReadCellulare &""","
                                tmpContaSMS = tmpContaSMS + 1
                            End If
                            If tmpContaSMS = 40 Then 'Mando 40 SMS per volta (Max 50)
                                tmpNumberDestinazione = tmpNumberDestinazione.SubString2(0, (tmpNumberDestinazione.Length -1))
                                CallSub2 (Me , "mySMS_Message_Send" , tmpNumberDestinazione)
                                tmpContaSMS = 0
                                tmpNumberDestinazione = ""
                            End If
                        Next
                        If tmpContaSMS > 0 Then
                            tmpNumberDestinazione = tmpNumberDestinazione.SubString2(0, (tmpNumberDestinazione.Length -1))
                            CallSub2 (Me , "mySMS_Message_Send" , tmpNumberDestinazione)
                        End If 
                    Case Else
                        Select m.Get("errorCode")
                            Case  97
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The access To the api was denied"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  98
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - You made too much requests in a short time"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  99
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The service Is currently Not available"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  101
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The credentials are wrong"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  107
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The user msisdn Not exists"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  108
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The key Is wrong"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  109                                 
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The login Is blocked For a specific time, because some wrong logins were made"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc                         
                            Case  600
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The api key Is invalid"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc                                                             
                            Case Else
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - Errore non codificato"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                        End Select
                        mySMSErrore.SMSReturnCode = 1
                End Select
            Case mySMS_REMOTE_SMS
                CallSub (Me, "Progress_Dialog_Close")
                Dim m As Map
                m = parser.NextObject
'                Log(m.Get("errorCode"))
                mySMSErrore.SMSReturnCode = 0
                lblMsgSMS.Text = "" 
                Select m.Get("errorCode")
                    Case 0
                        lblMsgSMS.Text = "SMS inviati correttamente"
                    Case Else
                        Select m.Get("errorCode")
                            Case 2
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - A required parameter was Not given"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc                             
                            Case  97
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The access To the api was denied"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  98
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - You made too much requests in a short time"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  99
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The service Is currently Not available"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  100
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The auth token Is invalid"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  700
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - One Or more recipients are Not in the correct format Or are containing invalid msisdns (i.e: 436761234567)"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  701
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - You only are allowed To send a sms To 50 Recipients per request"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                            Case  702                                 
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - One Or more recipients are blocked by the system"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc                         
                            Case  600
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - The api key Is invalid"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc                                                             
                            Case Else
                                mySMSErrore.SMSErrDesc = m.Get("errorCode") & " - Errore non codificato"
                                lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                        End Select
                        mySMSErrore.SMSReturnCode = 1
                        mySMSErrore.SMSErrDesc = m.Get("errorCode")
                        lblMsgSMS.Text = mySMSErrore.SMSErrDesc
                End Select                             
            Case Else
        End Select
    Else
        mySMSErrore.SMSReturnCode = 9
        mySMSErrore.SMSErrCode = 999999
        mySMSErrore.SMSErrDesc = "Error: " & job.ErrorMessage
        lblMsgSMS.Text = mySMSErrore.SMSErrDesc
'        Log ("Error: " & job.ErrorMessage)
    End If
    job.Release
End Sub
 
Top