{"size": 2359, "ext": "js", "max_stars_repo_path": "cont3xt/integrations/twilio/index.js", "max_stars_repo_name": "mmguero-dev/moloch", "max_stars_repo_stars_event_min_datetime": "2015-01-01T12:40:23.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-17T11:52:59.000Z", "max_issues_repo_path": "cont3xt/integrations/twilio/index.js", "max_issues_repo_name": "mmguero-dev/moloch", "max_issues_repo_issues_event_min_datetime": "2015-01-02T18:07:40.000Z", "max_issues_repo_issues_event_max_datetime": "2020-11-16T18:44:42.000Z", "max_forks_repo_path": "cont3xt/integrations/twilio/index.js", "max_forks_repo_name": "mmguero-dev/moloch", "max_forks_repo_forks_event_min_datetime": "2015-01-01T20:32:05.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-15T06:58:31.000Z"}
{"max_stars_count": 3999, "max_issues_count": 1035, "max_forks_count": 886, "avg_line_length": 24.5729166667, "max_line_length": 141, "alphanum_fraction": 0.5735481136}
/******************************************************************************/
/* Copyright Yahoo Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this Software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
const Integration = require('../../integration.js');
const axios = require('axios');

class TwilioIntegration extends Integration {
  name = 'Twilio';
  icon = 'integrations/twilio/icon.png';
  order = 440;
  cacheTimeout = '1w';
  itypes = {
    phone: 'fetch'
  };

  homePage = 'https://www.twilio.com/';
  settings = {
    disabled: {
      help: 'Disable integration for all queries',
      type: 'boolean'
    },
    sid: {
      help: 'Your Twilio SID',
      required: true
    },
    token: {
      help: 'Your Twilio Token',
      password: true,
      required: true
    }
  };

  card = {
    title: 'Twilio for %{query}',
    fields: [
      'phone_number',
      {
        label: 'caller_name',
        type: 'json'
      },
      {
        label: 'carrier',
        type: 'json'
      }
    ]
  };

  constructor () {
    super();

    Integration.register(this);
  }

  async fetch (user, query) {
    try {
      const sid = this.getUserConfig(user, 'Twilio', 'sid');
      if (!sid) {
        return undefined;
      }

      const token = this.getUserConfig(user, 'Twilio', 'token');
      if (!token) {
        return undefined;
      }

      const result = await axios.get(`https://${sid}:${token}@lookups.twilio.com/v1/PhoneNumbers/+1${query}?Type=carrier&Type=caller-name`, {
        headers: {
          'User-Agent': this.userAgent()
        }
      });

      result.data._cont3xt = { count: 1 };
      return result.data;
    } catch (err) {
      if (Integration.debug <= 1 && err?.response?.status === 404) { return null; }
      console.log(this.name, query, err);
      return null;
    }
  }
}

// eslint-disable-next-line no-new
new TwilioIntegration();
