-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
54 lines (45 loc) · 1.41 KB
/
app.js
File metadata and controls
54 lines (45 loc) · 1.41 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
require("dotenv").config();
const { parsePhoneNumberFromString } = require("libphonenumber-js");
const { google } = require("googleapis");
process.on("unhandledRejection", (message, _) => {
console.error(message);
});
const OAuth2 = google.auth.OAuth2;
const client = new OAuth2(process.env.CLIENT_ID, process.env.CLIENT_SECRET);
client.setCredentials({
access_token: process.env.ACCESS_TOKEN,
refresh_token: process.env.REFRESH_TOKEN
});
const people = google.people({
version: "v1",
auth: client
}).people;
const connections = people.connections.list({
resourceName: "people/me",
personFields: "names,phoneNumbers"
});
connections.then(connections => {
for (connection of connections.data.connections) {
if (!("phoneNumbers" in connection)) {
continue;
}
for (phoneNumber of connection.phoneNumbers) {
try {
const number = parsePhoneNumberFromString(phoneNumber.value, "GB");
phoneNumber.value =
number.country == process.env.DEFAULT_CODE
? number.formatNational()
: number.formatInternational();
} catch (e) {
console.error(
`Cannot update contact "${connection.names[0].displayName}" with number "${phoneNumber.value}": ${e.message}`
);
}
}
people.updateContact({
resourceName: connection.resourceName,
updatePersonFields: "phoneNumbers",
resource: connection
});
}
});