0
votes

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.

3
I am not familiar with the C# API. However, I would create a new SMSMessage instance for each phone number & then send it accordingly. Or, if C# supports cloning go with cloning a SMSMessage instance.AMartinNo1
I tried creating multiple instances, but am getting an error 'Cannot bind parameter 'smsmessage1' to type SMSMessage&. Make sure the parameter Type is supported by the binding.'TheChosenOne94

3 Answers

1
votes

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();
     }
  }
1
votes

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();
        }
    }
}
-1
votes

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.