首页 > 解决方案 > How to send a simple message using a twilio number with an ASP.NET MVC project?

问题描述

I am trying to change and edit the code but it returns with exceptions errors in regards authentication errors. The username cannot be null as well as the category is not able to load the code. Another exception that is running on it is the Twilio.Exceptions.ApiExecution that requires a phone number.

The documentation is here: https://www.twilio.com/docs/sms/tutorials/server-notifications-csharp-mvc?code-sample=code-csv-list-of-phone-numbers-to-notify&code-language=csv&code-sdk-version=default

The video to build the code for integrating Twilio in an ASP.net MVC project is here: https://www.youtube.com/watch?v=ndxQXnoDIj8

The code excerpt is here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Configuration;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
using Twilio.TwiML;
using Twilio.AspNet.Mvc;

namespace SendandReceiveSms.Controllers
{
    public class SMSController : TwilioController
    {
        // GET: SMS
        public ActionResult SendSms()
        {
      var accountSid = ConfigurationManager.AppSettings["TwilioAccountSid"];
      var authToken = ConfigurationManager.AppSettings["TwilioAuthToken"];
      TwilioClient.Init("ACa4XXXXXXXXXX","77XXXXXXXXXX");
      var to = new PhoneNumber(ConfigurationManager.AppSettings["+65XXXXXXXX"]);
      var from = new PhoneNumber("+12053016835");
      var message = MessageResource.Create(
        to: to, 
        from: from,
        body: "Conserve with us and save the Wolrd ");
            return Content(message.Sid);
        }
    public ActionResult ReceiveSms()
    {
      var response = new MessagingResponse();
      response.Message(" We turn waste into environmental assets");
      return TwiML(response);
    }

    }
  }

标签: c#asp.netasp.net-mvctwilio

解决方案


你也可以试试这个。

 using DocGen.Notifications.Contract;
        using DocGen.Notifications.Models;
        using System;
        using System.Configuration;
        using System.Linq;
        using System.Text;
        using Twilio;
        using Twilio.Rest.Api.V2010.Account;
        using Twilio.Types;

        namespace DocGen.Notifications.Providers
        {
            public class SmsNotificationProvider : INotificationProtocolContract
            {
                NotificationResponseModel notificationResponseModel = new NotificationResponseModel();
                public NotificationResponseModel SendNotification(NotificationRequestModel notificationRequestModel)
                {
                    if (notificationRequestModel.SmsTo == null || notificationRequestModel.SmsTo.Count() == 0)
                        throw new ArgumentNullException(nameof(notificationRequestModel.SmsTo));

                    TwilioClient.Init(ConfigurationManager.AppSettings["accountSid"], ConfigurationManager.AppSettings["authToken"]);

                    foreach (var Sms_to in notificationRequestModel.SmsTo)
                    {
                        var to = new PhoneNumber(Sms_to);
                        var message = MessageResource.Create(
                            to,
                            from: new PhoneNumber(ConfigurationManager.AppSettings["senderNumber"]),//"+12563054795"
                            body: Encoding.UTF8.GetString(notificationRequestModel.Message));

                        notificationResponseModel.ResponseMessage = message.Status.ToString();
                    }

                    //notificationResponseModel.ResponseMessage = "Message Successfully sent.";

                    return notificationResponseModel;
                }
            }
        }

推荐阅读