/// <summary>
/// Sends the specified communication.
/// </summary>
/// <param name="communication">The communication.</param>
/// <exception cref="System.NotImplementedException"></exception>
public override void Send( Rock.Model.Communication communication )
{
var rockContext = new RockContext();
// Requery the Communication
communication = new CommunicationService( rockContext ).Get( communication.Id );
if ( communication != null &&
communication.Status == Model.CommunicationStatus.Approved &&
communication.Recipients.Where( r => r.Status == Model.CommunicationRecipientStatus.Pending ).Any() &&
( !communication.FutureSendDateTime.HasValue || communication.FutureSendDateTime.Value.CompareTo( RockDateTime.Now ) <= 0 ) )
{
string fromPhone = string.Empty;
string fromValue = communication.GetMediumDataValue( "FromValue" );
int fromValueId = int.MinValue;
if ( int.TryParse( fromValue, out fromValueId ) )
{
fromPhone = DefinedValueCache.Read( fromValueId, rockContext ).Value;
}
if ( !string.IsNullOrWhiteSpace( fromPhone ) )
{
string accountSid = GetAttributeValue( "SID" );
string authToken = GetAttributeValue( "Token" );
var twilio = new TwilioRestClient( accountSid, authToken );
var historyService = new HistoryService( rockContext );
var recipientService = new CommunicationRecipientService( rockContext );
var personEntityTypeId = EntityTypeCache.Read( "Rock.Model.Person" ).Id;
var communicationEntityTypeId = EntityTypeCache.Read( "Rock.Model.Communication" ).Id;
var communicationCategoryId = CategoryCache.Read( Rock.SystemGuid.Category.HISTORY_PERSON_COMMUNICATIONS.AsGuid(), rockContext ).Id;
var globalConfigValues = GlobalAttributesCache.GetMergeFields( null );
bool recipientFound = true;
while ( recipientFound )
{
var recipient = recipientService.Get( communication.Id, CommunicationRecipientStatus.Pending ).FirstOrDefault();
if ( recipient != null )
{
try
{
var phoneNumber = recipient.PersonAlias.Person.PhoneNumbers
.Where( p => p.IsMessagingEnabled )
.FirstOrDefault();
if ( phoneNumber != null )
{
// Create merge field dictionary
var mergeObjects = recipient.CommunicationMergeValues( globalConfigValues );
string message = communication.GetMediumDataValue( "Message" );
message = message.ResolveMergeFields( mergeObjects );
string twilioNumber = phoneNumber.Number;
if ( !string.IsNullOrWhiteSpace( phoneNumber.CountryCode ) )
{
twilioNumber = "+" + phoneNumber.CountryCode + phoneNumber.Number;
}
var globalAttributes = Rock.Web.Cache.GlobalAttributesCache.Read();
string callbackUrl = globalAttributes.GetValue( "PublicApplicationRoot" ) + "Webhooks/Twilio.ashx";
var response = twilio.SendMessage( fromPhone, twilioNumber, message, callbackUrl );
recipient.Status = CommunicationRecipientStatus.Delivered;
recipient.TransportEntityTypeName = this.GetType().FullName;
recipient.UniqueMessageId = response.Sid;
try
{
historyService.Add( new History
{
CreatedByPersonAliasId = communication.SenderPersonAliasId,
EntityTypeId = personEntityTypeId,
CategoryId = communicationCategoryId,
EntityId = recipient.PersonAlias.PersonId,
Summary = "Sent SMS message.",
Caption = message.Truncate( 200 ),
RelatedEntityTypeId = communicationEntityTypeId,
RelatedEntityId = communication.Id
} );
}
catch (Exception ex)
{
ExceptionLogService.LogException( ex, null );
}
}
else
{
recipient.Status = CommunicationRecipientStatus.Failed;
recipient.StatusNote = "No Phone Number with Messaging Enabled";
}
}
catch ( Exception ex )
{
recipient.Status = CommunicationRecipientStatus.Failed;
recipient.StatusNote = "Twilio Exception: " + ex.Message;
}
rockContext.SaveChanges();
}
else
{
recipientFound = false;
}
}
}
}
}