1

I am creating asp,net MVC application, one of the requirements is to send bulk sms from cell numbers stored in in database. I got Twilio Send SMS Messages in C# and .NET via REST API, but i have no idea how to use this in MVC, Here is the code:

using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace Quickstart
{
class SmsSender
{
    static void Main(string[] args)
    {
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";

        TwilioClient.Init(accountSid, authToken);

        var people = new Dictionary<string, string>() {
            {"+14158675309", "Curious George"},
            {"+14158675310", "Boots"},
            {"+14158675311", "Virgil"}
        };

        // Iterate over all our friends
        foreach (var person in people)
        {
            // Send a new outgoing SMS by POSTing to the Messages resource
            MessageResource.Create(
                from: new PhoneNumber("555-555-5555"), // From number, must be an SMS-enabled Twilio number
                to: new PhoneNumber(person.Key), // To number, if using Sandbox see note above
                // Message content
                body: $"Hey {person.Value} Monkey Party at 6PM. Bring Bananas!");

            Console.WriteLine($"Sent message to {person.Value}");
        }
    }
}

}

I will appreciate your assistance.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Haro
  • 59
  • 1
  • 10

1 Answers1

1

Gather users from database so you have a collection of phone numbers associated with given users, existing user is an object that contains information about what you're texting. You can customize this to your needs.

Instead of a static dictionary like:

var people = new Dictionary<string, string>() {
            {"+14158675309", "Curious George"},
            {"+14158675310", "Boots"},
            {"+14158675311", "Virgil"}
        };

You can make a database call like this:

 SMSNotifier notifier = new SMSNotifier();
 var doctorsToNotify = db.AspNetUsers.Where(x => x.ReceiveSMS == true && x.AspNetUserRoles.Any(y => y.RoleId == "1")).ToList();

Here you loop through your 'bulk' users:

            foreach (AspNetUser dr in doctorsToNotify)
            {
                await notifier.SendWaitingPatientNotification(existingUser, dr.Phone);
            }

Notifier Class (edit 'xxxxxxx' to have the phone number Twilio provided):

 public class SMSNotifier
    {
        public SMSNotifier()
        {
            var accountSid = ConfigurationManager.AppSettings["TwilioSid"];
            // Your Auth Token from twilio.com/console
            var authToken = ConfigurationManager.AppSettings["TwilioToken"]; ;

            TwilioClient.Init(accountSid, authToken);
        }

        public async Task SendWaitingPatientNotification(AspNetUser user, string phonenumber)
        {
            var message = await MessageResource.CreateAsync(
             to: new PhoneNumber("+1" + phonenumber),
             from: new PhoneNumber("+xxxxxxxxxx"),
             body: string.Format("Patient {0} {1} has entered the waiting room at {2}. Please visit the patient queue to see patient.", user.FirstName, user.LastName, DateTime.Now.ToString()));

        }
    }

I store the API sid and token in the web config file and retrieve it using the ConfigurationManager. Let me know if you need more help.

jon.r
  • 896
  • 8
  • 16