sms-send.js
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const axios = require("axios");
// Uncomment next URL only for authorization testing
// const api_method_send_path = "gate.smsaero.ru/v2/auth";
// Uncomment next URL for send SMS by authorized service user
const api_method_send_path = "gate.smsaero.ru/v2/sms/send";
const api_method_send = "https://" + api_method_send_path;
const api_username = 'ai@sales-mc.com';
const api_password = 'lOpVI_GrMOCe0VnUlpe9gA63nGkhmP_-';
const api_headers = { Accept: "application/json", Language: "ru-RU" };
const api_sign = "SMS Aero";
const api_auth = { username: api_username, password: api_password };
const api_options = { headers: api_headers, auth: api_auth };
module.exports = exports = {
/*
* Function for send text to abonent by Short Message Service
*
* Input params:
* number - abonent phone number from input param 'caller'
* text - text for send to abonent by Short Message Service
*
* Output params
* boolean: true|false
*
* ----------------------------------------------
* Usage in index.js for define external function
* ----------------------------------------------
*
* const sms = require("./sms-send");
*
* app.setExternal("send_sms", async (args, conv) => {
* let result = await sms.send_sms_text(args.phone, args.message);
* if (!result.success) console.log(result);
* return result.success;
* });
*
* ------------------------------------------------
* Usage in main.dsl for activate external function
* ------------------------------------------------
*
* external function send_sms(phone: string, message: string): boolean;
*
* --------------------------------------------
* Usage in any DSL for send message to abonent
* --------------------------------------------
*
* external send_sms(caller, "SMS text fo abonent");
*
*/
async send_sms_text (number, text) {
let result = {};
const params = { number: number, text: text, sign: api_sign };
let post_data = JSON.stringify(params);
api_options.params = params;
try {
result = await axios.get(api_method_send, api_options);
} catch (err) {
let error = err;
if (error.cause) error = error.cause;
if (error.response) error = {
status: error.response.status,
statusText: error.response.statusText,
method: error.response.request.method,
url: error.response.config.url,
path: error.response.request.path,
request_header: error.response.request._header,
};
result.data = {
"success": false,
"data": error,
"message": "axios crashed"
};
if (error.data) result.data = error.data;
}
return result.data;
}
}