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
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
Hangouts Chat
A messaging platform built for teams
Hangouts Chat
A messaging platform built for teams
Hangouts Chat
A messaging platform built for teams
Architecture Options - Web service
Image: CC-BY Google
Hangouts Chat
A messaging platform built for teams
Architecture Options - Cloud Pub/Sub
Image: CC-BY Google
Hangouts Chat
A messaging platform built for teams
Architecture Options - Google Apps Script
Image: CC-BY Google
Hangouts Chat
A messaging platform built for teams
Architecture Options - … but which one?
Image: CC-BY Google
Apps Script Bot Boilerplate
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
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
‘whereis’ bot
@whereis bot
Chat
Drive
Maps
@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
Attendance bot
Attendance Bot - codelab
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
Regex...
Image: https://xkcd.com/1171/
Hangouts Chat
A messaging platform built for teams
Architecture Options - Google Apps Script
Image: CC-BY Google
modified CC-BY mhawksey
HTTP
Cloud
Dah daa...
Simple integration with App Maker
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
App Maker - Document Approval App
App Maker - Project Tracker App
Combining data from both apps...
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
Next steps...