Friday, 18 November 2016

Send message with CallFire

No comments
public static string SendText(string number, string message)
        {
            UnitOfWork uow = new UnitOfWork();
            var appConfigCodeRepository = uow.Repository<AppConfigRepository>();
            var appConfigCode = "CallFireAuth";

            var webAddr = "https://api.callfire.com/v2/texts";
            var webrequest = (HttpWebRequest)WebRequest.Create(webAddr);
            webrequest.Method = "POST";
            webrequest.ContentType = "application/json";
            webrequest.Headers.Add("Authorization", String.Concat("Basic ", appConfigCode.Value));

            using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
            {                              
                string json = "[{\"message\":\"" + message + "\",\"phoneNumber\":\"" + number + "\"}]";
                streamWriter.Write(json);
            }
            StreamReader reader = null;
            string stripeResponse;
            try
            {
                HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
                Stream responseStream = webresponse.GetResponseStream();
                reader = new StreamReader(responseStream);
                return stripeResponse = reader.ReadToEnd();
            }
            catch (WebException exception)
            {
                using (WebResponse response = exception.Response)
                {
                    using (Stream data = response.GetResponseStream())
                    using (reader = new StreamReader(data))
                    {
                        return stripeResponse = reader.ReadToEnd();
                    }
                }
            }
        }

 public class CallFireResponse
    {

        public int httpStatusCode { get; set; }
        public string internalCode { get; set; }
        public string message { get; set; }

    }