c# - 我如何使用不同的提供商从 Acumatica ERP 发送短信?

标签 c# acumatica acumatica-kb

开箱即用的 Acumatica 有 TwilioAmazon SNS 提供商来发送 SMS。但是,我们与不同的提供商(例如 Plivo)有业务关系,并希望将它们用于 SMS 服务。是否可以使用不同的提供商?

最佳答案

是的,可以使用不同的提供商来发送 SMS。

在 Acumatica 中,SMS 服务用于

  1. two-factor authentication 期间发送访问代码
  2. 发送business notifications

Acumatica ERP 提供了一组接口(interface)来实现用于发送 SMS 消息的 SMS 提供程序。

<强>1。 PX.SmsProvider.ISmsProvider

实现此接口(interface)的类由 Acumatica ERP 自动发现,并可在 SMS 提供程序 (SM203535) 屏幕上的 Provider Type 框中进行选择。类必须是库 (DLL) 的一部分。

定义实现PX.SmsProvider.ISmsProvider接口(interface)的类,并实现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

类实现构造函数来初始化提供者。以及用于保存此提供程序的名称和描述的公共(public)属性——您需要它在 SMS 提供程序 (SM203535) 屏幕上的提供程序类型框中显示的方式。

定义实现PX.SmsProvider.ISmsProviderFactory接口(interface)的类,并实现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;
}

以下示例说明了使用 Plivo 服务创建 SMS 提供程序。

在C#类库工程中,添加PX.Common.dllPX.Data.dllPX.SmsProvider.Core.dll的引用code> 从您的 Acumatica 站点的 bin 文件夹中。

PlivoSmsProvider 类实现PX.SmsProvider.ISmsProvider 接口(interface)的定义: 我们将需要 Auth IDAuth TokenFrom Number 参数来使用 Plivo。因此,我们将在ExportSettings 方法中设置它们,并在LoadSettings 方法中将它们赋值给成员变量。我们将在 SendMessageAsync 中实现发送 SMS 的逻辑。

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
        }
    }
}

PlivoSmsProviderFactory 类实现 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;
    }
}

通过自定义发布此库后,此新提供程序将在 SMS 提供程序 (SM203535) 屏幕中可用。

SMS Providers

Download Acumatica Source code and Customization deployment package

关于c# - 我如何使用不同的提供商从 Acumatica ERP 发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60217617/

相关文章:

c# - 使用带反射的 T4 .tt 模板

c# - AutoMapper 项目嵌套对象,其中内部对象可为空失败

acumatica - 扩展通用查询以显示记录数

c# - 如何确定更新 DAC 字段的正确方法?

acumatica - 从 REST API 获取组合框值集

c# - 我是否以错误的方式使用处理程序?

c# - 设置字符串的最大长度c#但以整个单词结尾

acumatica - 当我使用 Acumatica 代码在销售订单屏幕上单击自定义操作时,我们如何附加报告 PDF/Excel 文件

c# - 在 Acumatica 中调试自定义代码

acumatica - Acumatica 定制项目中的 CREATE VIEW 语句