簡體   English   中英

如何從 asp.net mvc 應用程序發送批量短信

[英]How to send bulk sms from asp.net mvc application

我正在創建 asp,net MVC 應用程序,其中一項要求是從存儲在數據庫中的單元格號碼發送批量短信。 我通過 REST API 在 C# 和 .NET 中獲得了 Twilio Send SMS Messages,但我不知道如何在 MVC 中使用它,這是代碼:

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}");
        }
    }
}

}

我將感謝您的幫助。

從數據庫中收集用戶,以便您擁有與給定用戶關聯的電話號碼集合,現有用戶是一個包含有關您發短信內容的信息的對象。 您可以根據自己的需要進行自定義。

而不是像這樣的靜態字典:

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

您可以像這樣進行數據庫調用:

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

在這里,您可以遍歷“批量”用戶:

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

通知程序類(編輯“xxxxxxx”以提供電話號碼 Twilio):

 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()));

        }
    }

我將 API sid 和令牌存儲在 web 配置文件中,並使用ConfigurationManager檢索它。 如果您需要更多幫助,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM