Skip to content

Commit 0b13790

Browse files
authored
Add getContact(phone)
Add new function: Find phone Number and return "id" of contact. Searches all contacts without the limitations (1000 items) imposed by "Connections.list()"
1 parent 6b5a1fd commit 0b13790

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

advanced/people.gs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,54 @@ function getPhone() {
196196
* @see https://developers.google.com/people/api/rest/v1/people/updateContactPhoto
197197
*/
198198
function updatePhoto(id){
199-
var url = 'https://workwisewellness.co.uk/wp-content/uploads/2016/02/HiRes.jpg'
200-
var blob = UrlFetchApp.fetch(url).getBlob();
201-
var data = Utilities.base64EncodeWebSafe(blob.getBytes());
202-
var resourceName = 'people/'+id;
203-
var reqBody = {
204-
"photoBytes": data,
205-
"personFields": "photos"
199+
try {
200+
var url = 'https://workwisewellness.co.uk/wp-content/uploads/2016/02/HiRes.jpg'
201+
var blob = UrlFetchApp.fetch(url).getBlob();
202+
var data = Utilities.base64EncodeWebSafe(blob.getBytes());
203+
var resourceName = 'people/'+id;
204+
var reqBody = {
205+
"photoBytes": data,
206+
"personFields": "photos"
207+
}
208+
var res = People.People.updateContactPhoto(reqBody, resourceName)
209+
} catch (err) {
210+
// TODO (developers) - Handle exception
211+
console.log('Failed to get the connection with an error %s', err.message);
206212
}
207-
var res = People.People.updateContactPhoto(reqBody, resourceName)
208213
}
209214
// [END people_update_contact_photo]
215+
216+
// [START getContact]
217+
/************************************************************************************************************/
218+
// Find phone Number () and return "id" of contact
219+
// var id = getContact("+48507493304") without the second parameter, no token !
220+
//************************************************************************************************************/
221+
function getContact(phone, token){
222+
try {
223+
// Get the list of connections/contacts of user's profile
224+
// by default it only fetches the first 100 items
225+
const people = People.People.Connections.list('people/me', {
226+
pageToken: token, // in the first call the token is empty
227+
personFields: 'names,phoneNumbers'
228+
})
229+
// We search the received tables
230+
const contact = people['connections'].find((connection) => {
231+
if (connection['phoneNumbers'] !== undefined)
232+
return connection['phoneNumbers'].some((phoneNumber) => phoneNumber['canonicalForm'] === phone);
233+
});
234+
// If the result is different from undefined
235+
if ( contact !== undefined ){
236+
// The phone number you were looking for was found in the table
237+
// Converting from Json to arrays
238+
var data = JSON.parse(contact); // get all info about conntact 'names,phoneNumbers'
239+
// We are only interested in the "id" of the contact
240+
return data.names[0].metadata.source.id;
241+
}
242+
else // If we didn't find anything we keep looking, moving to nextPageToken
243+
return getContact(phone, people.nextPageToken);
244+
} catch (err) {
245+
// TODO (developers) - Handle exception
246+
console.log('Failed to get the connection with an error %s', err.message);
247+
}
248+
}
249+
// [END getContact]

0 commit comments

Comments
 (0)