﻿# Integration with an SMS service

> [HTML Version](sms-integration.html)

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](360013409220.md) 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](security_settings.md#authorization) or [logging into the system](security_settings.md#mobile) with phone number and one-time codes.

## Create and set up the integration module 

````
начало внимание

````
Only users included in the [Administrators](360006871932.md#administrators) group can create and set up modules.

````
конец внимание

````
To [create a module](create-extention.md) 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](extention-api.md) 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;  
\}

6. ````
Implement the SMS sending function: 

````
async function SMS\_sendSMS(phones: string\[\], msg: string): Promise<SMSSendError\[\]> \{  
    // function content  
\}

7. ````
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. 

**(hmfile_hash_f3e2dc3a.PNG)**

3. Return to the module page. The fields added in the previous step will appear on it: 

**(hmfile_hash_54a2d1c2.PNG)**

4. 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  
\}

5. ````
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](security_settings.md#authorization), [logging in](security_settings.md#mobile) with phone number and one-time codes or the [Send SMS](360013409220.md) activity . 