public TwilioSmsSender(
     TwilioSmsCredentials credentials,
     ILogger logger)
 {
     this.credentials = credentials;
     log = logger;
 }
Exemple #2
0
        /// <summary>
        /// Send an sms message using Twilio REST API
        /// </summary>
        /// <param name="credentials">TwilioSmsCredentials</param>
        /// <param name="toPhoneNumber">E.164 formatted phone number, e.g. +16175551212</param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task <bool> SendMessage(
            TwilioSmsCredentials credentials,
            string toPhoneNumber,
            string message)
        {
            if (string.IsNullOrEmpty(toPhoneNumber))
            {
                throw new ArgumentException("toPhoneNumber was not provided");
            }

            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentException("message was not provided");
            }

            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = CreateBasicAuthenticationHeader(
                credentials.AccountSid,
                credentials.AuthToken);

            var keyValues = new List <KeyValuePair <string, string> >();

            keyValues.Add(new KeyValuePair <string, string>("To", toPhoneNumber));
            keyValues.Add(new KeyValuePair <string, string>("From", credentials.FromNumber));
            keyValues.Add(new KeyValuePair <string, string>("Body", message));

            var content = new FormUrlEncodedContent(keyValues);

            var postUrl = string.Format(
                CultureInfo.InvariantCulture,
                TwilioSmsEndpointFormat,
                credentials.AccountSid);

            var response = await client.PostAsync(
                postUrl,
                content).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                if (log != null)
                {
                    log.LogDebug("success sending sms message to " + toPhoneNumber);
                }

                return(true);
            }
            else
            {
                if (log != null)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();

                    var logmessage = $"failed to send sms message to {toPhoneNumber} from {credentials.FromNumber} { response.ReasonPhrase } { responseBody }";
                    log.LogWarning(logmessage);
                }

                return(false);
            }
        }
        /// <summary>
        /// Send an sms message using Twilio REST API
        /// </summary>
        /// <param name="credentials">TwilioSmsCredentials</param>
        /// <param name="toPhoneNumber">E.164 formatted phone number, e.g. +16175551212</param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task<bool> SendMessage(
            TwilioSmsCredentials credentials,
            string toPhoneNumber, 
            string message)
        {
            if (string.IsNullOrEmpty(toPhoneNumber))
            {
                throw new ArgumentException("toPhoneNumber was not provided");
            }

            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentException("message was not provided");
            }

            var client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = CreateBasicAuthenticationHeader(
                credentials.AccountSid, 
                credentials.AuthToken);

            var keyValues = new List<KeyValuePair<string, string>>();
            keyValues.Add(new KeyValuePair<string, string>("To", toPhoneNumber));
            keyValues.Add(new KeyValuePair<string, string>("From", credentials.FromNumber));
            keyValues.Add(new KeyValuePair<string, string>("Body", message));

            var content = new FormUrlEncodedContent(keyValues);
            
            var postUrl = string.Format(
                    CultureInfo.InvariantCulture,
                    TwilioSmsEndpointFormat,
                    credentials.AccountSid);

            var response = await client.PostAsync(
                postUrl, 
                content).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                if(log != null)
                {
                    log.LogDebug("success sending sms message to " + toPhoneNumber);
                }
                
                return true;
            }
            else
            {
                if (log != null)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var logmessage = $"failed to send sms message to {toPhoneNumber} from {credentials.FromNumber} { response.ReasonPhrase } { responseBody }";
                    log.LogWarning(logmessage);
                }
                
                return false;
            }
            
        }
Exemple #4
0
        private TwilioSmsCredentials GetCredentials(ISiteSettings site)
        {
            if(site == null) { return null; }
            if(string.IsNullOrEmpty(site.SmsClientId)) { return null; }
            if (string.IsNullOrEmpty(site.SmsSecureToken)) { return null; }
            if (string.IsNullOrEmpty(site.SmsFrom)) { return null; }

            TwilioSmsCredentials creds = new TwilioSmsCredentials();
            creds.AccountSid = site.SmsClientId;
            creds.AuthToken = site.SmsSecureToken;
            creds.FromNumber = site.SmsFrom;

            return creds;

        }