'Create a new class SmsSender or another name
-----------------------------------------
Imports System.Collections.Generic
Imports System.Net
Imports System.Web
Imports System.Web.Script
Imports System.Web.Script.Serialization
Namespace API
Class SmsSender
Public Function SendSMS(ByVal _numberPara As String, ByVal _fromLab As String, ByVal _smsText As String, ByVal _apiKey As String, ByVal _apiCode As String) As SmsResponse
Dim uri As String = String.Format("http://rest.nexmo.com/sms/json?api_key={0}&api_secret={1}&from={2}&to={3}&type=unicode&text={4}", _apiKey, _apiCode, _fromLab, _numberPara, _smsText)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim json = New WebClient().DownloadString(uri)
Return ParseSmsResponseJson(json)
End Function
'Function to format the Json Response
Private Function ParseSmsResponseJson(ByVal json As String) As SmsResponse
' hyphens are not allowed in in .NET var names
json = json.Replace("message-count", "MessageCount")
json = json.Replace("message-price", "MessagePrice")
json = json.Replace("message-id", "MessageId")
json = json.Replace("remaining-balance", "RemainingBalance")
json = json.Replace("error-text", "ErrorText") 'Adicionei
Return New JavaScriptSerializer().Deserialize(Of SmsResponse)(json)
End Function
End Class
'-------------------------------------------
'Create a new class Message
'Property for each message
'-------------------------------------------
Public Class Message
Private m_To As String
Public Property [To]() As String
Get
Return m_To
End Get
Set(ByVal value As String)
m_To = value
End Set
End Property
Private m_Messageprice As String
Public Property Messageprice() As String
Get
Return m_Messageprice
End Get
Set(ByVal value As String)
m_Messageprice = value
End Set
End Property
Private m_Status As String
Public Property Status() As String
Get
Return m_Status
End Get
Set(ByVal value As String)
m_Status = value
End Set
End Property
'Adicionei esta variavel + Propriedade
Private m_StatusError As String
Public Property StatusError() As String
Get
Return m_StatusError
End Get
Set(ByVal value As String)
m_StatusError = value
End Set
End Property
Private m_MessageId As String
Public Property MessageId() As String
Get
Return m_MessageId
End Get
Set(ByVal value As String)
m_MessageId = value
End Set
End Property
Private m_RemainingBalance As String
Public Property RemainingBalance() As String
Get
Return m_RemainingBalance
End Get
Set(ByVal value As String)
m_RemainingBalance = value
End Set
End Property
End Class
'-------------------------------------------
'Create a new class SmsResponse
'Here we get the response from Nexmo
'with number of message sent and
'a list for each message with Status
'-------------------------------------------
Public Class SmsResponse
Private m_Messagecount As String
Public Property Messagecount() As String
Get
Return m_Messagecount
End Get
Set(ByVal value As String)
m_Messagecount = value
End Set
End Property
Private m_Messages As List(Of Message)
Public Property Messages() As List(Of Message)
Get
Return m_Messages
End Get
Set(ByVal value As List(Of Message))
m_Messages = value
End Set
End Property
End Class
End Namespace
'-----------------------------
'How to use:
'Create a Windows Form
'Or a WebPage in Asp.Net
'-----------------------------
Imports System.Net
Imports System.IO
Imports SMS.API
Dim _api As New _cClass_LSettings
Dim _apiKey, _apiCode, _apiRemetente, _apiDestinatario, _apiSMStext As String
Dim _smsNexmo As New SmsSender
Dim _resposta As New SmsResponse
Private Sub _smsAPI()
_resposta = _smsNexmo.SendSMS("Country Code" & txt_destinatario.Text, _api.remetente, txt_msg.Text, _api.apiKey, _api.apiCode)
'Get sms status If was or Not Send
If _resposta.Messagecount > 1 Then ' if more then 1 message
For Each item In _resposta.Messages
'Save the response
_lblStatus.Visible = True
'Save a big message(2 or more parts) in the same row in database
_messagem = _messagem + txt_msg.Text '''''
_mostrarStatusSMS(item.Status.ToString)
Exit For
Next
Else 'if only 1 messagem
For Each item In _resposta.Messages
_lblStatus.Visible = True
_mostrarStatusSMS(item.Status.ToString)
Next
End If
End Sub
--------------------------
A DEMO Project - NexmoSMS_SourceCode
