我正在创建asp,net MVC应用程序,其中一个要求是从存储在数据库中的单元格号发送批量sms。我通过REST API在C#和.NET中获得了Twilio发送短信,但我不知道如何在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}");
}
}
}
}
感谢您的协助。
答案 0 :(得分:1)
从数据库中收集用户,以便您拥有与给定用户关联的电话号码集合,现有用户是一个对象,其中包含有关您正在发短信的信息。您可以根据自己的需要进行自定义。
而不是像:
这样的静态字典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检索它。如果您需要更多帮助,请告诉我。