简体   繁体   English

使用 twilio 向多个经过验证的联系人发送短信

[英]Send SMS to multiple verified contacts using twilio

I am currently using the twilio trail account.我目前正在使用 twilio 跟踪帐户。 I have integrated Twilio in azure(function app).我已将 Twilio 集成到 azure(功能应用程序)中。

#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.根据您提到的代码,在您的情况下,您的 azure 函数中只有 1 个。 Based on my experience, if you want to send multiple messages, please have a try to use an ICollector as output.根据我的经验,如果您想发送多条消息,请尝试使用ICollector作为输出。 More details about multiple outbinding we could reference Queue output sample in C# .我们可以参考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:我不知道如何放置在 Azure 中,但这就是我使用 for 循环在 C# 中发送到多个数字的方式:

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. SMS 不是电子邮件,每条消息只能有一个收件人。 So you need as many messages to send (each having its own recipient) as you have in database.因此,您需要发送与数据库中一样多的消息(每个消息都有自己的收件人)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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