summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/phone.js
blob: 9f9236b3daac8c4a0868e6fd3eeac4c354827783 (plain)
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
const axios = require('axios');
const apiVersion = "v2";
const app = require('../../app.json');

function sleep(ms) {
    return new Promise((res) => {
        setTimeout(res, ms);
    });
}

(async () => {
    let user = process.argv[2];

    try {
        let data = (await axios.get(`https://lookups.twilio.com/${apiVersion}/PhoneNumbers/${user}`, {
            auth: {
                username: app.twilio.sid,
                password: app.twilio.secret
            }
        })).data;

        if (!data['valid']) throw new Error(JSON.stringify(data['validation_errors']));

        let obj = {
            error: null,
            avatar: `https://flagcdn.com/w160/${data['country_code'].toLowerCase()}.png`,
            name: data['national_format'],
            description: data['phone_number'],
            link: `tel:${data['phone_number']}`,
            copy: [
                {
                    title: "Copy national phone number",
                    text: data['national_format']
                },
                {
                    title: "Copy international phone number",
                    text: data['phone_number']
                },
                {
                    title: "Copy tel: link",
                    text: `tel:${data['phone_number']}`
                }
            ]
        }

        console.log(JSON.stringify(obj, null, 2));
    } catch (e) {
        console.log(JSON.stringify({
            error: e,
            avatar: "https://img.icons8.com/fluency-systems-regular/64/ffffff/phone.png",
            name: user,
            description: "Phone",
            link: null,
            copy: []
        }, null, 2));
    }


})();