3
votes

Out-of-box Acumatica has Twilio and Amazon SNS providers to send SMS. However, we have a business relationship with different provider (Plivo for example) and would like to utilize them for SMS service. Is it possible to use different provider?

1

1 Answers

4
votes

Yes, it is possible to use different provider to send SMS.

In Acumatica, SMS service is used for

  1. sending access code during two-factor authentication
  2. sending business notifications

Acumatica ERP provides set of interfaces to implement SMS provider for sending SMS message.

1. PX.SmsProvider.ISmsProvider

Classes implementing this interface are automatically discovered by Acumatica ERP and are available for selection in the Provider Type box on the SMS Provider (SM203535) screen. Classes must be part of Library (DLL).

Define class implementing PX.SmsProvider.ISmsProvider interface and implement methods of ISmsProvider interface.

public class MySmsProvider : ISmsProvider
{        
    public IEnumerable<PXFieldState> ExportSettings
    {
        // Implement definition of each setting/parameter and add to settings list
        get
        {
            return new List<PXFieldState>();
        }
    }

    public void LoadSettings(IEnumerable<ISmsProviderSetting> settings)
    {
        // Retrieve value of each setting/parameter and assign to corresponding member variable
    }

    public async Task SendMessageAsync(SendMessageRequest request, CancellationToken cancellation)
    {
        // Implement logic to send SMS
    }
}

2. PX.SmsProvider.ISmsProviderFactory

Class implementing constructor to initialize provider. And public properties to hold Name and Description for this provider – the way you need it to be displayed in Provider Type box on the SMS Provider (SM203535) screen.

Define class implementing PX.SmsProvider.ISmsProviderFactory interface and implement methods and properties of ISmsProviderFactory interface.

public class MySmsProviderFactory : ISmsProviderFactory
{
    //Create Provider and initialize with settings
    public ISmsProvider Create(IEnumerable<ISmsProviderSetting> settings)
    {
        var provider = new MySmsProvider();
        provider.LoadSettings(settings);
        return provider;
    }

    public ISmsProvider Create()
    {
        var provider = new MySmsProvider();
        return provider;
    }

    public string Description { get; } = "My Provider";
    public string Name { get; } = typeof(MySmsProvider).FullName;
}

Below example illustrates creating SMS Provider using Plivo Service.

In C# class library project, add references of PX.Common.dll, PX.Data.dll and PX.SmsProvider.Core.dll from your Acumatica Site’s bin folder.

Definition of PlivoSmsProvider class implementing PX.SmsProvider.ISmsProvider interface: We will need Auth ID, Auth Token and From Number parameters to work with Plivo. So, we will set them up in ExportSettings method and assign them to member variables in LoadSettings method. And we will implement logic to send SMS in SendMessageAsync.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using PX.Data;

namespace PX.SmsProvider.Plivo
{
    public class PlivoSmsProvider : ISmsProvider
    {
        #region DetailIDs const
        private const string AuthID_DetailID = "AUTH_ID";
        private const string AuthToken_DetailID = "AUTH_TOKEN";
        private const string FromPhoneNbr_DetailID = "FROM_PHONE_NBR";
        #endregion

        #region DetailID_Display const
        private const string AuthID_DetailID_Display = "Auth ID";
        private const string AuthToken_DetailID_Display = "Auth Token";
        private const string FromPhoneNbr_DetailID_Display = "From Number";
        #endregion

        private string m_AuthID;
        public string AuthID { get { return m_AuthID; } }

        private string m_AuthToken;
        public string AuthToken { get { return m_AuthToken; } }

        private string m_FromPhoneNbr;
        public string FromPhoneNbr { get { return m_FromPhoneNbr; } }

        public IEnumerable<PXFieldState> ExportSettings
        {
            get
            {
                var settings = new List<PXFieldState>();

                var authID = (PXStringState)PXStringState.CreateInstance(
                    m_AuthID,
                    null,
                    false,
                    AuthID_DetailID,
                    null,
                    1,
                    null,
                    null,
                    null,
                    null,
                    null
                );
                authID.DisplayName = AuthID_DetailID_Display;
                settings.Add(authID);
                var authToken = (PXStringState)PXStringState.CreateInstance(
                    m_AuthToken,
                    null,
                    false,
                    AuthToken_DetailID,
                    null,
                    1,
                    "*",
                    null,
                    null,
                    null,
                    null
                );
                authToken.DisplayName = AuthToken_DetailID_Display;
                settings.Add(authToken);

                var fromPhoneNbr = (PXStringState)PXStringState.CreateInstance(
                    m_FromPhoneNbr,
                    null,
                    false,
                    FromPhoneNbr_DetailID,
                    null,
                    1,
                    null,
                    null,
                    null,
                    null,
                    null
                );
                fromPhoneNbr.DisplayName = FromPhoneNbr_DetailID_Display;
                settings.Add(fromPhoneNbr);

                return settings;
            }
        }

        public void LoadSettings(IEnumerable<ISmsProviderSetting> settings)
        {
            foreach (ISmsProviderSetting detail in settings)
            {
                switch (detail.Name.ToUpper())
                {
                    case AuthID_DetailID: m_AuthID = detail.Value; break;
                    case AuthToken_DetailID: m_AuthToken = detail.Value; break;
                    case FromPhoneNbr_DetailID: m_FromPhoneNbr = detail.Value; break;
                }
            }
        }

        public async Task SendMessageAsync(SendMessageRequest request, CancellationToken cancellation)
        {
            // implement logic to send SMS
        }
    }
}

Definition of PlivoSmsProviderFactory class implementing PX.SmsProvider.ISmsProviderFactory interface.

using System.Collections.Generic;

namespace PX.SmsProvider.Plivo
{
    public class PlivoSmsProviderFactory : ISmsProviderFactory
    {
        public ISmsProvider Create(IEnumerable<ISmsProviderSetting> settings)
        {
            var provider = new PlivoSmsProvider();
            provider.LoadSettings(settings);
            return provider;
        }

        public ISmsProvider Create()
        {
            var provider = new PlivoSmsProvider();
            return provider;
        }

        public string Description { get; } = "Plivo SMS Provider";
        public string Name { get; } = typeof(PlivoSmsProvider).FullName;
    }
}

Once this library is published via customization, this New provider will be available in SMS Providers (SM203535) screen.

SMS Providers

Download Acumatica Source code and Customization deployment package