Visual Basic .NET (v12, vs2013) The following code assumes you have created a Web Service reference to http://api.123-txt.com/Api123WCF.svc and called it apiSms.
Common shared code
Dim userName As String = "user@email.goes.here" Dim userPassword As String = "user_password_goes_here” Dim sourceNumber As String = "virtual_number" Dim destNumber1 As String = "dest_number_1" Dim destNumber2 As String = "dest_number_1" Dim sms As String = "Test Message "
Send SMS Message
Using client = New apiSms.Api123WCFClient("soap") ' apiSms = a web reference to: http://api.123-txt.com/Api123WCF.svc Dim request As _123txt_API.apiSms.jsonBasicSendMessageRequest = New apiSms.jsonBasicSendMessageRequest request.user = userName request.pass = userPassword request.source = sourceNumber request.destination = destNumber1 request.sms = sms & "SendSmsData" Dim response As Boolean response = client.SendSmsData(request) If (response) Then ' Message sent Trace.TraceInformation("Message was sent") Else ' Message not sent Trace.TraceInformation("Message was not sent") End If End Using
Send SMS Message Advanced
Using client = New apiSms.Api123WCFClient("soap") ' apiSms = a web reference to: http://api.123-txt.com/Api123WCF.svc Dim request As _123txt_API.apiSms.jsonAdvancedSendMessageRequest = New apiSms.jsonAdvancedSendMessageRequest request.user = userName request.pass = userPassword request.source = sourceNumber request.destinations = {destNumber1, destNumber2} request.sms = sms & "SendSmsAdvancedData" Dim response As _123txt_API.apiSms.jsonSendSmsReturn response = client.SendSmsAdvancedData(request) If (response.SendSucceeded) Then ' 1 or more messages sent Trace.TraceInformation("Invalid numbers identified: {0}", response.CountOfInvalidNumbers) Trace.TraceInformation("Valid numbers identified: {0}", response.CountOfValidNumbers) Trace.TraceInformation("Credit cost of this transaction: {0}", response.TotalCreditCost) If (response.WillNotSend IsNot Nothing) Then For Each badNumber As _123txt_API.apiSms.DestinationNumberBad In response.WillNotSend Trace.TraceInformation("Bad number: {0}, Error code: {1}", badNumber.vn, badNumber.status) Next End If If (response.WillSend IsNot Nothing) Then For Each goodNumber As _123txt_API.apiSms.DestinationNumberGood In response.WillSend Trace.TraceInformation("Good number: {0}, DestId: {1}", goodNumber.vn, goodNumber.destId) ' You will need to persist the returned DestId's so that you can check on their delivery status at a later date. Next End If Else ' All destinations failed to be sent a message Trace.TraceError("Total failure to send, the reason is: {0}", response.ErrorMessage) End If End Using
Receive Messages
Static lastDate As DateTime = CDate("2015-01-01 00:00:01") Using client = New apiSms.Api123WCFClient("soap") ' apiSms = a web reference to: http://api.123-txt.com/Api123WCF.svc Dim request As _123txt_API.apiSms.jsonBasicReceiveSmsRequest = New apiSms.jsonBasicReceiveSmsRequest request.user = userName request.pass = userPassword request.maxRecords = 20 request.LastDate = lastDate Dim response As _123txt_API.apiSms.jsonReceivedMessages response = client.ReceiveSms(request) If (response.SmsList IsNot Nothing) Then ' Messages received For Each incomingMessage As _123txt_API.apiSms.jsonReceivedMessage In response.SmsList Trace.TraceInformation("Unique ID of this incoming message: {0}", incomingMessage.MessageId) Trace.TraceInformation("The 'from' number or source of this SMS - it could be an alphanumeric ID: {0}", incomingMessage.Source) Trace.TraceInformation("The destination of this incoming message: {0}", incomingMessage.Dest) Trace.TraceInformation("The body of the incoming SMS: {0}", incomingMessage.Text) Trace.TraceInformation("The timestamp this message was received (in UTC datetime format) {0}", incomingMessage.TimeStamp) Next Trace.TraceInformation("Store this date: {0} and use it the next time you call this method so that you do not receive duplicate copies of incoming messages", response.MostRecentDate) lastDate = CDate(response.MostRecentDate) Else ' No messages to receive Trace.TraceInformation("Store this date: {0} and use it the next time you call this method so that you do not receive duplicate copies of incoming messages", response.MostRecentDate) lastDate = CDate(response.MostRecentDate) End If End Using
Note: The response from this method will contain a date which you can then use to set to ‘lastDate’ in your next call, this will ensure you do not pull duplicate incoming messages from the service.
Get Message Statuses
Using client = New apiSms.Api123WCFClient("soap") ' apiSms = a web reference to: http://api.123-txt.com/Api123WCF.svc Dim request As _123txt_API.apiSms.jsonSentSmsStatusRequest = New apiSms.jsonSentSmsStatusRequest request.user = userName request.pass = userPassword request.destIds = {Guid.Parse("a812d280-4083-40c3-9216-c50baae2cd5d"), Guid.Parse("df6c2572-a981-4e33-9bfe-cdd0300e9cca"), Guid.Parse("8c6a6e96-453c-4ee3-bfaf-c74acb792df8")} Dim response As _123txt_API.apiSms.smsMsgs response = client.SentSmsStatus(request) If (response.Success) Then ' Successfully received statuses If (response.Msgs IsNot Nothing) Then ' Dump out all the statuses that were returned: For Each masterMessage As _123txt_API.apiSms.smsMsg In response.Msgs Trace.TraceInformation("The following delivery status(es) are related to recipients associated with Message Id: {0}", masterMessage.MsgId.ToString) If (masterMessage.DestIds IsNot Nothing) Then For Each messageDestination As _123txt_API.apiSms.smsDest In masterMessage.DestIds Trace.TraceInformation("This status update is related to destination with DestId: {0}", messageDestination.DestId.ToString) If (messageDestination.PartIds IsNot Nothing) Then For Each messagePart As _123txt_API.apiSms.smsStatuses In messageDestination.PartIds Trace.TraceInformation("This status update is related to part number {0} with unique ID {1}", messagePart.PartNo, messagePart.PartId.ToString) If (messagePart.Statuses IsNot Nothing) Then For Each messagePartStatus As _123txt_API.apiSms.smsStatus In messagePart.Statuses Trace.TraceInformation("This is a final delivery status: {0}, received on {1}, with Code1(Major Number)={2}, Code2(Minor Number)={3}, Code3(Final Outcome)={4}", messagePartStatus.Final, messagePartStatus.Date, messagePartStatus.Code1, messagePartStatus.Code2, messagePartStatus.Code3) Next End If Next End If Next End If Next End If ' Dump out the descriptions of every status code returned above If (response.Codes IsNot Nothing) Then For Each code As _123txt_API.apiSms.smsCode In response.Codes Trace.TraceInformation("Code1: {0}, Code2: {1}, Description for this code pair: {2}", code.Code1, code.Code2, code.Description) Next End If ' Final Outcome code descriptions: ' 1 = Final delivery status, success. ' 2 = Intermediate delivery status, still in progress. ' 3 = Final delivery status, failure, will not succeed if retried. ' 4 = Final delivery status, failure, may succeed if retried after a wait period (6+ hours) ' 5 = Final delivery status, failure, system error. Else ' Could not receive statuses Trace.TraceError("Total failure to receive message statuses, the reason is: {0}", response.ErrorMessage) End If End Using
Note: SendSmsAdvanced returns the destId’s of each sent message, you must hold onto these IDs and use them within SentSmsStatus to identify their current and final delivery states.