﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;


namespace TestCgWhatsApp
{
    class Class1
    {
        static void Main()
        {
            string response = sendCgWhatsAppMt("<Your API ID>", "<Your API Password>", "<WhatsApp Number>", "77","Here is the image you requested","image","http://www.commzgate.com/assets/img/commzgate_cloud_portal.png");


            Console.WriteLine("CG response:" + response);
        }

        
        /* Sending Whatsapp MT
            Depending on the pre-approved WhatsApp templateID, placeholder configuration may vary. Please refer to CommzGate API guide.
        */

        public static string sendCgWhatsAppMt(string ID, string Password, string mobile, string templateid, string placeholder, string mediatype, string mediaurl)
        {
            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 += "WhatsApp=" + "true"+ "&";
                paramData += "TemplateID=" + templateid + "&"; // WhatsApp Template ID. Use this only for WhatsApp Template messages
                paramData += "PlaceHolder1=" + System.Uri.EscapeDataString(placeholder) + "&";

                //Image Params
                paramData += "MediaType=" + "image"+ "&"; //Can be text, image or document. (Default is text)
                paramData += "MediaUrl=" + System.Uri.EscapeDataString(mediaurl) + "&"; //Example http://www.commzgate.com/assets/img/commzgate_cloud_portal.png (only possible if its document or image)

                //Text Params
                //paramData += "MediaType=" + "text"+ "&";
                //paramData += "Caption=" + "caption"+ "&"; // only possible for text message format.
                //paramData += "Message=" + System.Uri.EscapeDataString(msg) + "&"; //Use this only for WhatsApp Session message

                //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;
            }
        }



    }
}