sms-send.js 2.68 KB
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;
  }

}