Acumatica: SMS Provider, Twilio SMS provider, send SMS in action

Hello everybody,

Today I want to share one approach, how to send SMS message from custom action in Acumatica.

Acumatica has several sms providers in SalesDemo data base, it depends from Acumatica’s version, so, we will use Twilio provider. On SMS Provider screen you can find authorization parameters from each provider, as on screen shot from (22r1 build):

First you need to add two references from Acumatica’s Bin folder to your project in VS:

PX.SmsProvider.Core.dll

PX.SmsProvider.UI.dll

 

Then create graph extension for any screen that you need and develop next logic in custom button “Send SMS”.

Also, you need to use Dependency Injection (ASP.net) and IReadOnlyDictionary interface and define field in graph extension with ISmsProvider type.

The main logic consists from next steps: prepare list of settings (List<ISmsProviderSetting>), create SmsProvider fabric (using dependency injection), load setting to fabric, prepare SMS message and send it in async mode. Also, you can add PXLongOperation feature in action, it doesn’t have conflicts with async method.

Source code example here:

public class SOOrderEntryExt : PXGraphExtension<SOOrderEntry>
{
    public static bool IsActive() => true;
 
 
    [InjectDependency]
    internal IReadOnlyDictionary<string, ISmsProviderFactory> ProviderFactories { getset; }
    private ISmsProvider _currentProvider;
 
 
    public PXAction<SOOrder> SendSMS;
    [PXButton()]
    [PXUIField(DisplayName = "Send SMS", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
    protected virtual void sendSMS()
    {
        SendSMSNotification(this.Base, "+380990123456""Test SMS message from Acumatica server");
    }
 
    public virtual void SendSMSNotification(PXGraph graphstring phonestring smsMessage)
    {
        string parsePhoneTo = phone.Trim(' ''('')''-');
 
        var selectProviderAuth = SelectFrom<SmsPluginParameter>.InnerJoin<SmsPlugin>.On<SmsPluginParameter.pluginName.IsEqual<SmsPlugin.name>>.
            Where<SmsPlugin.isDefault.IsEqual<@P.AsBool>>.View.Select(graph, true);
 
        string ACCOUNT_SID = string.Empty;
        string SECRET = string.Empty;
        string FROM_PHONE_NBR = string.Empty;
 
        var setting = new List<TGSmsProviderSettings>();
 
        foreach (SmsPluginParameter item in selectProviderAuth)
        {
            if (item.PluginTypeName != "PX.SmsProvider.Twilio.TwilioVoiceProvider")
                throw new PXException("No preferences for Twilio provider!");
 
            switch (item.Name)
            {
                case nameof(ACCOUNT_SID):
                    {
                        ACCOUNT_SID = item.Value;
                        setting.Add(new TGSmsProviderSettings()
                        {
                            Name = nameof(ACCOUNT_SID),
                            Value = ACCOUNT_SID,
                        });
                        break;
                    }
                case nameof(SECRET):
                    {
                        SECRET = item.Value;
                        setting.Add(new TGSmsProviderSettings()
                        {
                            Name = nameof(SECRET),
                            Value = SECRET,
                        });
                        break;
                    }
                case nameof(FROM_PHONE_NBR):
                    {
                        FROM_PHONE_NBR = item.Value;
                        setting.Add(new TGSmsProviderSettings()
                        {
                            Name = nameof(FROM_PHONE_NBR),
                            Value = FROM_PHONE_NBR,
                        });
                        break;
                    }
            }
        }
 
        if (string.IsNullOrEmpty(ACCOUNT_SID) || string.IsNullOrEmpty(FROM_PHONE_NBR) || string.IsNullOrEmpty(SECRET))
            throw new PXException("No preferences for Twilio provider!");
 
        SendSmSMessage(setting, parsePhoneTo, smsMessage);
    }
 
    private void SendSmSMessage(List<TGSmsProviderSettings> settingsstring phonestring body)
    {
        if (this._currentProvider != nullreturn;
 
        this._currentProvider = this.ProviderFactories["PX.SmsProvider.Twilio.TwilioVoiceProvider"].Create();
        this._currentProvider.LoadSettings(settings);
 
        var messageRequest = new SendMessageRequest()
        {
            RecepientPhoneNbr = phone,
            RecepientSMSMessage = body
        };
 
        try
        {
            _currentProvider.SendMessageAsync(messageRequest, CancellationToken.None).Wait();
        }
        catch (AggregateException ex)
        {
            string str = string.Join(";", ex.InnerExceptions.Select(x => x.Message));
            throw new PXException(str);
        }
    }
}
 
 
public class TGSmsProviderSettings : ISmsProviderSetting
{
    public string Name { getset; }
    public string Description { getset; }
    public string Value { getset; }
}

Summary

With provided code and Twilio you can send sms messages to your USA based customers. Similar activites may be done for other customers, but out of the box Acumatica allows to use Twilio.