Modules > Examples of ELMA365 integration modules / Integration with an SMS service

Integration with an SMS service

Modules of integration with SMS-sending services are needed to inform customers and employees about any events occurring in the course of the business process. For example, using the Send SMS activity, you can set up automatic sending of SMS with order status data for customers or with information about setting important tasks for your co-workers.

Also, such modules can be used when setting up two-factor authentication or logging into the system with phone number and one-time codes.

Create and set up the integration module

начало внимание

Only users included in the Administrators group can create and set up modules.

конец внимание

To create a module for integration with an SMS sender, follow the steps below:

  1. Go to Administration > Modules.
  2. In the upper right corner, click +Module and in the window that opens, select the Create option.
  3. Enter a name and description for the module and click Create.
  4. In the module settings, go to the API Methods tab and click the Edit button. The method editor will open.
  5. Go to the Scripts tab and connect the following interfaces:

// Data structure for storing the connection check response
interface SMSTestConnectionResult {
    success: boolean;
    failReason: string;
}
 
// Data structure for storing SMS sending a response to a phone number.
interface SMSSendError {
    phone: string;
    statusCode?: number;
    statusText?: string;
}

  1. Implement the SMS sending function:

async function SMS_SendSMS(phones: string[], msg: string): Promise<SMSSendError[]> {
    // function content
}

  1. Save and publish the script.

After that, the enabled module can be used to send SMS notifications during the business process or for authorization in the system.

Integration example

Let us take a closer look at module creation using the example of integration with the SMS Aero service. In order to integrate with this service, you need to additionally specify the e-mail to which your SMS Aero account is registered, as well as the API key. These fields should be placed on the module settings page. To do this:

  1. Go to the Administration > Modules page. In the upper right corner, click +Module, and then click Create. Specify the name of the module and a brief description.
  2. Open the Settings tab and create two string fields that will store the settings for connecting to SMS Aero on the module side:
  • API key. API key for connecting the module and the SMS Aero service;
  • email. The email to which the SMS Aero account is registered. Used for authorization in the service.

sms_integtration 01

  1. Return to the module page. The fields added in the previous step will appear on it:

sms_integtration 02

  1. Go to the script editor: Module Settings > API Methods > Scripts and add the following code to the file:

// Data structure for storing the connection check response.
interface SMSTestConnectionResult {
 success: boolean;
 failReason: string;
}
 
// Data structure for storing SMS sending a response to a number.
interface SMSSendError {
 phone: string;
 statusCode?: number;
 statusText?: string;
}
 
const api_key = Namespace.params.data.api_key
const email = Namespace.params.data.email
 
// Send SMS to phone numbers.
async function SMS_sendSMS(phones: string[], msg: string): Promise<SMSSendError[]> {
 const smsErrs: SMSSendError[] = []
 const authSuccess = await tryAuth()
 
 if (!authSuccess) {
         smsErrs.push({ phone: '', statusCode: 401, statusText: 'Unauthorized' })
         return smsErrs

 
 const phoneListString = phones.join('&numbers[]=')
 
 const res = await fetch(`https://${email}:${api_key}@gate.smsaero.ru/v2/sms/send?numbers[]=${phoneListString}&text=${msg}&sign=SMS Aero`)
 if (!res.ok) {
         // smsErrs allows you to write separate processing for each phone number
         smsErrs.push({phone: '', statusCode: res.status, statusText: res.statusText})
         return smsErrs
 }
 return smsErrs
}
 
async function tryAuth(): Promise<boolean> {
 const res = await fetch(`https://${email}:${api_key}@gate.smsaero.ru/v2/auth`)
 if (!res.ok) {
         return false
 }
 return true
}

  1. Save and publish the scripts.

After enabling the module and filling in its parameters, you will be able to choose integration with SMS Aero service when configuring two-factor authentication, logging in with phone number and one-time codes or the Send SMS activity .

Found a typo? Highlight the text, press ctrl + enter and notify us