﻿Imports System.IO
Imports System.Net
Imports Microsoft.VisualBasic


Public Class SMS
    Private Class Settings
        Public Property emailid As String
        Public Property password As String
        Public Property clientref As String
    End Class

    Private Class Recipient
        Public Property [to] As String
        Public Property identifier As String
    End Class

    Private Class Message
        Public Property ctid As String
        Public Property recipients As Recipient()
        Public Property messageText As String
    End Class

    Private Class SMSMessage
        Public Property settings As Settings
        Public Property messages As Message()
    End Class

    Public Property Setting_EmailId As String = "jaco@randmark40.com"
    Public Property Setting_Password As String = "Ks4IDP"
    Public Property Setting_ClientRef As String = "Rm40"


    Public Function SendMessage([to] As String, identifier As String, ctid As String, messageText As String) As String
        Dim settings As New SMS.Settings With {
            .emailid = Setting_EmailId,
            .password = Setting_Password,
            .clientref = Setting_ClientRef
        }

        Dim recipients As New List(Of SMS.Recipient)
        recipients.Add(New SMS.Recipient With {.identifier = identifier, .[to] = [to]})

        Dim messages As New List(Of SMS.Message)
        messages.Add(New SMS.Message With {.ctid = ctid, .messageText = messageText, .recipients = recipients.ToArray})


        Dim message As New SMS.SMSMessage With
        {
            .messages = messages.ToArray,
            .settings = settings
        }
        Try
            Dim jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(message)

            Dim httpWebRequest As HttpWebRequest = HttpWebRequest.Create("http://api.payless4sms.co.za/sms/json")
            httpWebRequest.ContentType = "application/json" 'text/json
            httpWebRequest.Method = "POST"

            Using streamWriter As New StreamWriter(httpWebRequest.GetRequestStream)
                streamWriter.Write(jsonString)
            End Using
            Dim httpResponse As HttpWebResponse = httpWebRequest.GetResponse
            Using streamReader As New StreamReader(httpResponse.GetResponseStream)
                Dim responseText As String = streamReader.ReadToEnd
                Return responseText
            End Using
        Catch ex As Exception
            Return ""
        End Try


    End Function






End Class