1 of 28

Totally UnScripted 20

Building Google Hangouts Chat bots with Google Apps Script

GUEST PRESENTERS

Martin Hawksey

G Suite Google Developers Expert

Cleo Espiritu

G Suite Google Developers Expert

2 of 28

Totally UnScripted 20

Building Google Hangouts Chat bots with Google Apps Script

GUEST PRESENTERS

Martin Hawksey

G Suite Google Developers Expert

4pm GMT 01 February 2019

tu.appscript.info

Cleo Espiritu

G Suite Google Developers Expert

3 of 28

Hangouts Chat

A messaging platform built for teams

4 of 28

Hangouts Chat

A messaging platform built for teams

5 of 28

Hangouts Chat

A messaging platform built for teams

Architecture Options - Web service

Image: CC-BY Google

6 of 28

Hangouts Chat

A messaging platform built for teams

Architecture Options - Cloud Pub/Sub

Image: CC-BY Google

7 of 28

Hangouts Chat

A messaging platform built for teams

Architecture Options - Google Apps Script

Image: CC-BY Google

8 of 28

Hangouts Chat

A messaging platform built for teams

Architecture Options - … but which one?

Image: CC-BY Google

9 of 28

Apps Script Bot Boilerplate

10 of 28

Bot Events

/**� * Responds to an ADDED_TO_SPACE event in Hangouts Chat.� * @param {Object} event the event object from Hangouts Chat� */function onAddToSpace(event) {...}��/**� * Responds to a REMOVED_FROM_SPACE event in Hangouts Chat.� * @param {Object} event the event object from Hangouts Chat� */function onRemoveFromSpace(event) {� ...�}

Apps Script

11 of 28

Bot Events

/**� * Responds to a CARD_CLICKED event in Hangouts Chat.� * @param {Object} event the event object from Hangouts Chat� */function onCardClick(event) {...}

/**� * Responds to a MESSAGE event in Hangouts Chat.� * @param {Object} event the event object from Hangouts Chat� */function onMessage(event) {...}

Apps Script

12 of 28

‘whereis’ bot

13 of 28

@whereis bot

Chat

Drive

Maps

14 of 28

@whereis bot - basic code

/**� * Responds to a MESSAGE event in Hangouts Chat.� * @param {Object} event the event object from Hangouts Chat

* @return {Card} object to render to the user� */

function onMessage(e) {var bot = e.message.annotations[0].userMention.user.displayName;var loc = encodeURI(e.message.text.substring(bot.length+2));var mapClick = {"openLink": {"url": "https://google.com/maps/search/?api=1&query=" + loc� }};return {// see JSON payload in the documentation };}

Apps Script

15 of 28

Attendance bot

16 of 28

Attendance Bot - codelab

17 of 28

Attendance Bot - logic

function onMessage(e) {

// If the user said that they were 'sick', adjust the image in the// header sent in response.if (userMessage.indexOf('sick') > -1) {// Hospital material icon� … � } else if (userMessage.indexOf('vacation') > -1) {// Spa material icon� … � }

}

Apps Script

18 of 28

Regex...

Image: https://xkcd.com/1171/

19 of 28

Hangouts Chat

A messaging platform built for teams

Architecture Options - Google Apps Script

Image: CC-BY Google

modified CC-BY mhawksey

HTTP

Cloud

20 of 28

Dah daa...

21 of 28

22 of 28

Simple integration with App Maker

23 of 28

Hangouts Chat

A messaging platform built for teams

Architecture Options - Google Apps Script

Image: CC-BY Google

modified CC-BY @cinfourthirds

Cloud

Cloud SQL

App Maker App

24 of 28

App Maker - Document Approval App

25 of 28

App Maker - Project Tracker App

26 of 28

Combining data from both apps...

27 of 28

JDBC Connection code

function onMessage(e) {

...� var conn = Jdbc.getCloudSqlConnection(

"jdbc:google:mysql://INSTANCE_NAME/DB_NAME",{user: ”USERNAME",� password: ”PASS" } );var stmt = conn.createStatement();� var result = stmt.execute(

"SELECT r.DocumentName, r.DocumentUrl, r.Id, r.Owner FROM Approver a join WorkflowStage w on a.WorkflowStage_fk = w.Id join Request r on r.Id = w.Request_fk where a.Email = '"+ e.user.email +"' and w.Status=1;"");

// createCards takes the results and returns them in the card format

var cards = createCards(result);

...� return { “cards”: cards };}

Apps Script

28 of 28

Next steps...