Advertisement
Guest User

SkebbySMS

a guest
Apr 11th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.91 KB | None | 0 0
  1.  
  2. Imports System.Net
  3. Imports System.IO
  4. Imports System.Text
  5. Imports System.Web
  6.  
  7. Module Module1
  8.  
  9.     Const SMS_TYPE_CLASSIC As String = "classic"
  10.     Const SMS_TYPE_CLASSIC_PLUS As String = "classic_plus"
  11.     Const SMS_TYPE_BASIC As String = "basic"
  12.     Const SMS_TYPE_TEST_CLASSIC As String = "test_classic"
  13.     Const SMS_TYPE_TEST_CLASSIC_PLUS As String = "test_classic_plus"
  14.     Const SMS_TYPE_TEST_BASIC As String = "test_basic"
  15.  
  16.     Private Function skebbyGatewaySendSMS(
  17.                          ByVal username As String, _
  18.                          ByVal password As String, _
  19.                          ByVal recipients() As String, _
  20.                          ByVal text As String, _
  21.                          Optional ByVal sms_type As String = "basic", _
  22.                          Optional ByVal sender_number As String = "", _
  23.                          Optional ByVal sender_string As String = "", _
  24.                          Optional ByVal user_reference As String = "", _
  25.                          Optional ByVal charset As String = "" _
  26.                      ) As Dictionary(Of String, String)
  27.         Dim parameters, method As String
  28.         Dim URL As String
  29.         Dim result As New Dictionary(Of String, String)
  30.         Dim values() As String
  31.         Dim temp
  32.  
  33.         URL = "http://gateway.skebby.it/api/send/smseasy/advanced/http.php"
  34.  
  35.         method = "send_sms_classic"
  36.  
  37.         Select Case sms_type
  38.             Case SMS_TYPE_CLASSIC
  39.                 method = "send_sms_classic"
  40.             Case SMS_TYPE_CLASSIC_PLUS
  41.                 method = "send_sms_classic_report"
  42.             Case SMS_TYPE_BASIC
  43.                 method = "send_sms_basic"
  44.             Case SMS_TYPE_TEST_CLASSIC
  45.                 method = "test_send_sms_classic"
  46.             Case SMS_TYPE_TEST_CLASSIC_PLUS
  47.                 method = "test_send_sms_classic_report"
  48.             Case SMS_TYPE_TEST_BASIC
  49.                 method = "test_send_sms_basic"
  50.             Case Else
  51.                 method = "send_sms_classic"
  52.         End Select
  53.  
  54.         parameters = "method=" + HttpUtility.UrlEncode(method) + "&" _
  55.                     + "username=" + HttpUtility.UrlEncode(username) + "&" _
  56.                     + "password=" + HttpUtility.UrlEncode(password) + "&" _
  57.                     + "text=" + HttpUtility.UrlEncode(text) + "&" _
  58.                     + "recipients[]=" + String.Join("&recipients[]=", recipients)
  59.  
  60.         If (sender_number <> "") And (sender_string <> "") Then
  61.             result.Add("status", "failed")
  62.             result.Add("code", "0")
  63.             result.Add("message", "You can specify only one type of sender, numeric or alphanumeric")
  64.             Return result
  65.         End If
  66.  
  67.         If sender_number <> "" Then parameters = parameters + "&sender_number=" + HttpUtility.UrlEncode(sender_number)
  68.         If sender_string <> "" Then parameters = parameters + "&sender_string=" + HttpUtility.UrlEncode(sender_string)
  69.         If user_reference <> "" Then parameters = parameters + "&user_reference=" + HttpUtility.UrlEncode(user_reference)
  70.  
  71.         Select Case charset
  72.             Case "UTF-8"
  73.                 parameters = parameters + "&charset=" + HttpUtility.UrlEncode("UTF-8")
  74.             Case Else
  75.         End Select
  76.  
  77.         ' Create POST
  78.         Dim request As WebRequest = WebRequest.Create(URL)
  79.         request.Method = "POST"
  80.         Dim byteArray As Byte() = Encoding.UTF8.GetBytes(parameters)
  81.         request.ContentType = "application/x-www-form-urlencoded"
  82.         request.ContentLength = byteArray.Length
  83.         Dim dataStream As Stream = request.GetRequestStream()
  84.         dataStream.Write(byteArray, 0, byteArray.Length)
  85.         dataStream.Close()
  86.  
  87.         Dim response As WebResponse
  88.         Try
  89.             'Trying to get the response.
  90.             response = request.GetResponse()
  91.         Catch ex As System.Net.WebException
  92.             result.Add("status", "failed")
  93.             result.Add("code", "0")
  94.             result.Add("message", "Network error, unable to send the message")
  95.             Return result
  96.         End Try
  97.  
  98.         dataStream = response.GetResponseStream()
  99.         Dim reader As StreamReader = New StreamReader(dataStream)
  100.         Dim responseFromServer As String = reader.ReadToEnd()
  101.         ' Clean up the streams.
  102.         reader.Close()
  103.         dataStream.Close()
  104.         response.Close()
  105.  
  106.         ' Return result to calling function
  107.         If responseFromServer.Length > 0 Then
  108.             values = responseFromServer.Split(New Char() {"&"c})
  109.             For Each value In values
  110.                 temp = value.Split(New Char() {"="c})
  111.                 result.Add(temp(0), temp(1))
  112.             Next
  113.             Return result
  114.         End If
  115.     End Function
  116.  
  117.     Function skebbyGatewayGetCredit(username As String, password As String, Optional charset As String = "") As Dictionary(Of String, String)
  118.         Dim parameters, method As String
  119.         Dim URL As String
  120.         Dim result As New Dictionary(Of String, String)
  121.         Dim values() As String
  122.         Dim temp
  123.  
  124.         URL = "http://gateway.skebby.it/api/send/smseasy/advanced/http.php"
  125.         method = "get_credit"
  126.  
  127.         parameters = "method=" + HttpUtility.UrlEncode(method) + "&" _
  128.                    + "username=" + HttpUtility.UrlEncode(username) + "&" _
  129.                    + "password=" + HttpUtility.UrlEncode(password)
  130.  
  131.         Select Case charset
  132.             Case "UTF-8"
  133.                 parameters = parameters + "&charset=" + HttpUtility.UrlEncode("UTF-8")
  134.             Case Else
  135.         End Select
  136.  
  137.         ' Create POST
  138.         Dim request As WebRequest = WebRequest.Create(URL)
  139.         request.Method = "POST"
  140.         Dim byteArray As Byte() = Encoding.UTF8.GetBytes(parameters)
  141.         request.ContentType = "application/x-www-form-urlencoded"
  142.         request.ContentLength = byteArray.Length
  143.         Dim dataStream As Stream = request.GetRequestStream()
  144.         dataStream.Write(byteArray, 0, byteArray.Length)
  145.         dataStream.Close()
  146.  
  147.         Dim response As WebResponse
  148.         Try
  149.             'Trying to get the response.
  150.             response = request.GetResponse()
  151.         Catch ex As System.Net.WebException
  152.             result.Add("status", "failed")
  153.             result.Add("code", "0")
  154.             result.Add("message", "Network error, unable to send the message")
  155.             Return result
  156.         End Try
  157.  
  158.         dataStream = response.GetResponseStream()
  159.         Dim reader As StreamReader = New StreamReader(dataStream)
  160.         Dim responseFromServer As String = reader.ReadToEnd()
  161.         ' Clean up the streams.
  162.         reader.Close()
  163.         dataStream.Close()
  164.         response.Close()
  165.  
  166.         ' Return result to calling function
  167.         If responseFromServer.Length > 0 Then
  168.             values = responseFromServer.Split(New Char() {"&"c})
  169.             For Each value In values
  170.                 temp = value.Split(New Char() {"="c})
  171.                 result.Add(temp(0), temp(1))
  172.             Next
  173.             Return result
  174.         End If
  175.  
  176.     End Function
  177.  
  178.     Sub Main()
  179.  
  180.         Dim result
  181.         Dim credit_result
  182.  
  183.         ' Single dispatch
  184.         Dim recipients(0) As String
  185.         recipients(0) = "393471234567"
  186.  
  187.         ' Multiple dispatch
  188.         ' Dim recipients(1) As String
  189.         ' recipients(0) = "393471234567"
  190.         ' recipients(1) = "393477654321"
  191.  
  192.         ' ------------ SMS Classic dispatch --------------
  193.  
  194.         ' SMS CLASSIC dispatch with custom alphanumeric sender
  195.         result = skebbyGatewaySendSMS("username", "password", recipients, "Hi Mike, how are you?", SMS_TYPE_CLASSIC, "", "John")
  196.  
  197.         ' SMS CLASSIC dispatch with custom numeric sender
  198.         ' result = skebbyGatewaySendSMS("username", "password", recipients, "Hi Mike, how are you?", SMS_TYPE_CLASSIC, "393471234567")
  199.  
  200.  
  201.         ' ------------- SMS Basic dispatch ----------------
  202.         ' result = skebbyGatewaySendSMS("username","password",recipients,"Hi Mike, how are you? By John", SMS_TYPE_BASIC)
  203.  
  204.  
  205.         ' ------------ SMS Classic Plus dispatch -----------
  206.  
  207.         ' SMS CLASSIC PLUS dispatch (with delivery report) with custom alphanumeric sender
  208.         ' result = skebbyGatewaySendSMS("username", "password", recipients, "Hi Mike, how are you?", SMS_TYPE_CLASSIC_PLUS, "", "John")
  209.  
  210.         ' SMS CLASSIC PLUS dispatch (with delivery report) with custom numeric sender
  211.         ' result = skebbyGatewaySendSMS("username","password",recipients,"Hi Mike, how are you?", SMS_TYPE_CLASSIC_PLUS,"393471234567")
  212.  
  213.         ' SMS CLASSIC PLUS dispatch (with delivery report) with custom alphanumeric sender and custom reference string
  214.         ' result = skebbyGatewaySendSMS("username","password",recipients,"Hi Mike, how are you?", SMS_TYPE_CLASSIC_PLUS,"393471234567","","reference")
  215.  
  216.         ' ------------------------------------------------------------------
  217.         '     WARNING! THE SMS_TYPE_TEST* SMS TYPES DOESN'T DISPATCH ANY SMS
  218.         '     USE THEM ONLY TO CHECK IF YOU CAN REACH THE SKEBBY SERVER
  219.         ' ------------------------------------------------------------------
  220.  
  221.  
  222.         ' ------------- SMS Classic test dispatch ---------
  223.  
  224.         ' SMS CLASSIC test dispatch with custom alphanumeric sender
  225.         ' result = skebbyGatewaySendSMS("username", "password", recipients, "Hi Mike, how are you?", SMS_TYPE_TEST_CLASSIC, "", "John")
  226.  
  227.         ' SMS CLASSIC test dispatch with custom numeric sender
  228.         ' result = skebbyGatewaySendSMS("username","password",recipients,"Hi Mike, how are you?", SMS_TYPE_TEST_CLASSIC,"393471234567")
  229.  
  230.  
  231.         ' ------------- Testing invio SMS Classic Plus ---------
  232.  
  233.         ' SMS CLASSIC PLUS test dispatch (with delivery report) with custom alphanumeric sender
  234.         ' result = skebbyGatewaySendSMS("username", "password", recipients, "Hi Mike, how are you?", SMS_TYPE_TEST_CLASSIC_PLUS, "", "John")
  235.  
  236.         ' SMS CLASSIC PLUS test dispatch (with delivery report) with custom numeric sender
  237.         ' result = skebbyGatewaySendSMS("username","password",recipients,"Hi Mike, how are you?", SMS_TYPE_TEST_CLASSIC_PLUS,"393471234567")
  238.  
  239.  
  240.         ' ------------- SMS Basic test dispatch ---------------
  241.         ' result = skebbyGatewaySendSMS("username", "password", recipients, "Hi Mike, how are you? By John", SMS_TYPE_TEST_BASIC)
  242.  
  243.  
  244.         If result("status") = "success" Then
  245.             Console.WriteLine("Message Sent!")
  246.             Console.WriteLine("Remaining SMSs: " + result("remaining_sms"))
  247.  
  248.             If (result.ContainsKey("id")) Then
  249.                 Console.WriteLine("ID: " + result("id"))
  250.             End If
  251.         End If
  252.  
  253.         ' ------------------------------------------------------------------
  254.         ' Check the complete documentation at http:#www.skebby.com/business/index/send-docs/
  255.         ' ------------------------------------------------------------------
  256.         ' For eventual errors see http:#www.skebby.com/business/index/send-docs/#errorCodesSection
  257.         ' WARNING: in case of error DON'T retry the sending, since they are blocking errors
  258.         ' ------------------------------------------------------------------                   
  259.         If result("status") = "failed" Then
  260.             Console.WriteLine("Sendign Failed")
  261.             Console.WriteLine("Code:" + result("code"))
  262.             Console.WriteLine("Description:" + result("message"))
  263.         End If
  264.         Console.ReadLine()
  265.  
  266.         ' ------------ REMAINING CREDIT Check -------------
  267.         'credit_result = skebbyGatewayGetCredit("username", "password")
  268.  
  269.         'If credit_result("status") = "success" Then
  270.         '    Console.WriteLine("Current Balance: " + credit_result("credit_left"))
  271.         '    Console.WriteLine("Remaining SMS Classic: " + credit_result("classic_sms"))
  272.         '    Console.WriteLine("Remaining SMS Basic: " + credit_result("basic_sms"))
  273.         'End If
  274.         'If credit_result("status") = "failed" Then
  275.         '    Console.WriteLine("Sending request failed")
  276.         'End If
  277.         'Console.ReadLine()
  278.  
  279.     End Sub
  280.  
  281. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement