Automate custom Google Chat notifications driven by Gmail activity

What if you wanted to get prompted in Google Chat when you get an email from your boss, your wife, a particular application? Using AppScript, we can perform custom gmail searches, then based whether there’s a result, get a Google Chat notification. This will take all of 5 mins to set up.

Example Google Chat card-formatted notification

What we’ll use:

First, setup your new Appscript project

  • Go to script.google.com
  • Create a new project
New projects are easy to set up….you can have as many as you need.
  • Add the Gmail Service, using the menu on the left (Services, select Gmail, Click Add)
Click Services to browse the included services list
Select from multiple service libraries, but for this you’ll just need Gmail
Add the Gmail service, or the script won’t work
  • If you’ve done the right thing, then “Gmail” will show up on the left side of the screen.

Now, copy the code from the repo i’ve shared

  • Copy everything from the Code.gs file in this repo
  • Paste it into the appscript code editor, replacing the existing “function myFunction()” text that’s already there. IE: Replace everything that was already there, with the contents of the repo i linked above.

Get the webhook URL from your Google Chat room

  • Open google chat (desktop or web client will work)…screenshots will be from the web version
  • Select the room that you want to get notified in, then click “Manage Webhooks”
Manage webhooks via the menu for your chat room
  • Fill out the fields with whatever details you want. I’ve shown a sample icon file URL, but you can use whatever you want.
Set up your webhook, including a logo
  • Click Save, then you’ll see the following information:
Your incoming webhooks are here (note, you can have more than one)

Click the “copy” icon on the right hand side, which will copy the URL (that you’ll paste into appscript in the next step).

Click the copy icon to get the webhook url

Now your webhook is created, and you just need to point your script at it.

  • Return to your appscript window, and paste the webhook URL into the top of the code, replacing the value for “WEBHOOK_URL”
  • Also update the other variables to whatever you want. You can always come back and change things once you see the first notification
Code snippet from the appscript that you’ll change to fit your own chat room / notification title etc

Now, configure the gmail searches…one for each notification type you want

  • scroll down in the code, looking for the “function config_gmail_search_array_() {“ line….
  • this is where you’ll add an array value for each search you want to perform. Use the existing format, adding in as many as you want.
Example searches to give you some ideas

That’s it! Now, all you need to do is test it out.

  • Using the options at the top, run the job_check_gmail function.
  • The first time, it will prompt you to accept permissions so that the script runs on behalf of your own account. Click through the acceptance prompts, and that’s it.
  • If there’s a match for the search(es) then you’ll get a card notification in Google Chat that looks like this:
Card notification from Google Chat fed from appscript

Now, you’ll want to run this on a schedule, right?

  • This is where your search queries need to be in sync with your triggered/automatic runs. IE: if you run this every 5 mins, then you’ll only want to look for new emails in the last 5 minutes, right? If you run it every hour, then you can change the queries to use different variables / values, like “newer:1h” etc.
  • Set up your trigger by clicking “Triggers” on the left hand side of your appscript screen
Click Triggers
  • Create a new trigger and set it up like this:

Now, your script will run every 5 mins, and if it gets a hit on any of your searches, you’ll get a card notification.

That’s really it now. Simply….easy to follow (hopefully), and extendable if you want to push a notification to Slack instead of Google Chat.

Repository Link:

https://github.com/usaussie/appscript-gmail-to-chat-notifier

Follow me on twitter for stuff like this, mostly related to data, analytics, google…. @techupover

** UPDATE — added support in the repo code for showing snippets in the chat card notification. The basic instructions still work, just the functions to run will be one of the two provided “jobs”**