Christian Ahlin
Christian Ahlin

Reputation: 21

How can I make a Hangouts bot in Apps Script reply with employee data?

I've built my first chat bot through AppsScript and it's going great.

The use case I got stuck on is "get information about co-workers".

This code below should work, but the contact doesn't have any phone numbers. Well, not in the local users "Contacts" but in the company directory. There is some big API for users I don't know if that can get authorization to run when another user is calling the function.

var contacts = ContactsApp.getContactsByName("Firstname Lastname");
var phoneFields = contacts[0].getPhones();

var widgets = [{
   "buttons": [{
     "imageButton": {
                "icon": "EMAIL",
                "onClick": {
                  "openLink": {
                    "url": "mailto:"+ contacts[0].getPrimaryEmail()+""
                  }
              }
            }
            },{
     "imageButton": {
                "icon": "PHONE",
                "onClick": {
                  "openLink": {
                    "url": "tel:" + phoneFields[0].getPhoneNumber() +""
                  }
              }
            }
            }
            ]

Thanks in advance

Upvotes: 0

Views: 1132

Answers (1)

Christian Ahlin
Christian Ahlin

Reputation: 21

I apparently forgot to press "post" this morning, and after 6 hours I solved it. I create a separate Spreadsheet under admin control that gets directory data. Set that Spreadsheet so everyone in the domain can access it.

  if((event.message.text.toLowerCase().indexOf("firstname1") !== -1) || (event.message.text.toLowerCase().indexOf("firstname2") !== -1)){ 
// Get the sheet
var sheet = SpreadsheetApp.openById("secret-key-from-sheet");
var data = sheet.getActiveSheet().getDataRange().getValues();
var personFound = false;
var i = 0;
// Search for person in document
while (personFound == false) {
  if (event.message.text.toLowerCase().indexOf(data[i][0].toLowerCase()) !== -1){
    personFound = true
  } else {
    i++;
  }
}

/* List all data you can get
    data[i][0]); //First name
    data[i][1]); //Last name
    data[i][2]); //E-mail
    data[i][8]); //Work Phone
    data[i][29]); //Custom field
    data[i][30]); //Custom field (image)
    data[i][31]); //Custom field
*/
//Card
var header = {
  "header": {
    "title" : data[i][0] +" " +data[i][1],
    "subtitle" : data[i][31],
    "imageUrl" : data[i][30]
  }
}

var widgets = [{
   "buttons": [{
     "imageButton": {
                "icon": "EMAIL",
                "onClick": {
                  "openLink": {
                    "url": "mailto:" + data[i][2]
                  }
              }
            }
            },{
     "imageButton": {
                "icon": "PHONE",
                "onClick": {
                  "openLink": {
                    "url": "tel:" + data[i][8]
                  }
              }
            }
            }
            ]
},{
   "keyValue": {
     "topLabel": "Custom header for custom data",
     "content": data[i][29]
   }
 }
              ];

return createCardResponsewithHeader(header, widgets); //this is from Googles example, but I'm passing a custom header instead of using a "Bot"-header.
}

Upvotes: 2

Related Questions