简体   繁体   中英

Send SMS to multiple verified contacts using twilio

I am currently using the twilio trail account. I have integrated Twilio in azure(function app).

#r "Microsoft.Azure.ApiHub.Sdk"
#r "Newtonsoft.Json"
#r "System.Data"
#r "Twilio.Api"

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Azure.ApiHub;
using Newtonsoft.Json.Linq;
using Twilio;

    public static void Run(TraceWriter log, out SMSMessage smsmessage)
    {

            smsmessage = new SMSMessage();
          string phonenumber = "";
         string MessageBody = "sample message";
             using (SqlConnection conn = new SqlConnection(str))
                    {

                        conn.Open();
                    var sqlStr = "SELECT [phone] FROM [dbo].[tbl_ContactTable] where [ID] = 2";  //It returns two rows with twilio verified phone numbers

                    using (SqlCommand cmd = new SqlCommand(sqlStr, conn))
                    {

                        var dataReader = cmd.ExecuteReader();
                        if(dataReader.HasRows)
                        { 
                       while(dataReader.Read())
                        {

                            phonenumber = dataReader[0].ToString();
                                log.Info($" phonenumber {phonenumber}");

                                smsmessage.Body = MessageBody;
                                smsmessage.To = phonenumber;
                            }
                        }

                        dataReader.Close();
                    }
                    conn.Close();
                }}

Two phone numbers are shown in the log, but a message is sent only to the last phone number(phone number present in the last row). Is there any way to iterate through phone numbers to send the message to multiple numbers at once.

According to your mentioned code, in your case just has 1 out in your azure function. Based on my experience, if you want to send multiple messages, please have a try to use an ICollector as output. More details about multiple outbinding we could reference Queue output sample in C# . Please have a try to use the following code.

public static void Run(TraceWriter log, ICollector<SMSMessage> smsmessage) 
{
   string phonenumber = "";
   string MessageBody = "sample message";
   using (SqlConnection conn = new SqlConnection(str))
   {

        conn.Open();
        var sqlStr = "SELECT [phone] FROM [dbo].[tbl_ContactTable] where [ID] = 2";  //It returns two rows with twilio verified phone numbers

         using (SqlCommand cmd = new SqlCommand(sqlStr, conn))
         {

            var dataReader = cmd.ExecuteReader();
            if(dataReader.HasRows)
            { 
              while(dataReader.Read())
              {
                 var sms = new SMSMessage();  //changed code
                 phonenumber = dataReader[0].ToString();
                 log.Info($" phonenumber {phonenumber}");
                 sms.Body = MessageBody;
                 sms.To = phonenumber;
                 smsmessage.Add(sms); //changed oode
               }

             }

              dataReader.Close();
        }
              conn.Close();
     }
  }

I don't know about placing in Azure but this is how I send to multiple numbers in C# with a for loop:

using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
using TwilioSendMulti;

namespace TwilioSendMulti
{
    public class Program
    {

        static void Main(string[] args)
        {

           const string accountSid = "put account Sid Here or use class variable";

           const string authToken = "put auth Token Here or use class variable";

           string[] MultiNums = { "+1212number1", "+1212number2" };

           for (int i = 0; i < MultiNums.Length; i++)
            {
                TwilioClient.Init(accountSid, authToken);
                var message = MessageResource.Create(
                body: "Sent thru an Array in C# with Twilio!",
                from: new Twilio.Types.PhoneNumber("+1212mytwilio#"),
                to: new Twilio.Types.PhoneNumber(MultiNums[i]));
                Console.WriteLine(message.Sid);   
            }
            Console.ReadLine();
        }
    }
}

SMS is not an email, it can have only one recipient per message. So you need as many messages to send (each having its own recipient) as you have in database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM