@@ -196,14 +196,54 @@ function getPhone() {
196
196
* @see https://developers.google.com/people/api/rest/v1/people/updateContactPhoto
197
197
*/
198
198
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 ) ;
206
212
}
207
- var res = People . People . updateContactPhoto ( reqBody , resourceName )
208
213
}
209
214
// [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