﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;


namespace TestCgSms
{
    class Class1
    {
        static void Main()
        {
            string response = sendCgSms("<Your API ID>", "<Your API Password>", "<Your mobileNumber", "CommzGate Test Message");


            Console.WriteLine("CG response:" + response);
        }


        public static string sendCgSms(string ID, string Password, string mobile, string msg)
        {
            string response = "";
            try
            {
                //Construct Send Params.


                string gateWay = "https://www.commzgate.net/gateway/SendMessage?";
                //Setup Params
                string paramData = "";
                paramData += "ID=" + ID + "&";
                paramData += "Password=" + Password + "&";
                paramData += "Mobile=" + mobile + "&";
                paramData += "Type=" + "A" + "&";
                paramData += "Message=" + System.Uri.EscapeDataString(msg) + "&";


                //Ensure Ascii format
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] ASCIIparamData = encoding.GetBytes(paramData);


                //Setting Request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gateWay);
                request.Method = "POST";


                request.Accept = "text/plain";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = ASCIIparamData.Length;


                //Sending Request.
                Stream streamReq = request.GetRequestStream();
                streamReq.Write(ASCIIparamData, 0, ASCIIparamData.Length);


                //Get Response
                HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();
                Stream streamResponse = HttpWResp.GetResponseStream();


                //Read the Response.. and open a dialog
                StreamReader reader = new StreamReader(streamResponse);
                response = reader.ReadToEnd();


                //Consult CommzGate API guide for response details.
                return response;
            }
            catch (Exception e)
            {
                return ""+e;
            }
        }
        
        /* For OTP, message content shall be in the format "Your OTP is ##OTP## " 
           where ##OTP## is a placeholder will be automatically replaced by CommzGate.
        */
        public static string sendCgOtp(string ID, string Password, string mobile, string msg, string otpExpiry, string otpLength, string otpMaxValidate)
        {
            string response = "";
            try
            {
                //Construct Send Params.


                string gateWay = "https://www.commzgate.net/gateway/SendMessage?";
                //Setup Params
                string paramData = "";
                paramData += "ID=" + ID + "&";
                paramData += "Password=" + Password + "&";
                paramData += "Mobile=" + mobile + "&";
                paramData += "Type=" + "A" + "&";
                paramData += "OTP=" + "advanced" + "&";
                paramData += "OtpExpiry=" + otpExpiry + "&";
                paramData += "OtpLength=" + otpLength + "&";
                paramData += "OtpMaxValidate=" + otpMaxValidate + "&";
                paramData += "Message=" + System.Uri.EscapeDataString(msg) + "&";


                //Ensure Ascii format
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] ASCIIparamData = encoding.GetBytes(paramData);


                //Setting Request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gateWay);
                request.Method = "POST";


                request.Accept = "text/plain";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = ASCIIparamData.Length;


                //Sending Request.
                Stream streamReq = request.GetRequestStream();
                streamReq.Write(ASCIIparamData, 0, ASCIIparamData.Length);


                //Get Response
                HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();
                Stream streamResponse = HttpWResp.GetResponseStream();


                //Read the Response.. and open a dialog
                StreamReader reader = new StreamReader(streamResponse);
                response = reader.ReadToEnd();


                 //Consult CommzGate API guide for response details.
                return response;
            }
            catch (Exception e)
            {
                return ""+e;
            }
        }
        
        
        /*  Validated based on ACCEPT or REJECT respond
        */
        public static string validateCgOtp(string ID, string Password, string mobile, string otp)
        {
            string response = "";
            try
            {
                //Construct Send Params.


                string gateWay = "https://www.commzgate.net/OTP/Validate?";
                //Setup Params
                string paramData = "";
                paramData += "ID=" + ID + "&";
                paramData += "Password=" + Password + "&";
                paramData += "Mobile=" + mobile + "&";
                paramData += "OtpValidate=" + otp;

                //Setting Request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gateWay+paramData);
                request.Method = "GET";


                request.Accept = "text/plain";
                request.ContentType = "application/x-www-form-urlencoded";


                //Get Response
                HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();
                Stream streamResponse = HttpWResp.GetResponseStream();


                //Read the Response.. and open a dialog
                StreamReader reader = new StreamReader(streamResponse);
                response = reader.ReadToEnd();


                //Consult CommzGate API guide for response details.
                return response; 
            }
            catch (Exception e)
            {
                return ""+e;
            }
        }



    }
}