Building a Slack Reminder App 🀖 with Google Cloud Functions ⚡ and Google Cloud Scheduler ⏰

google-cloud-platform Jul 11, 2019

Google Cloud Platform provides awesome tools to help engineers perform automation with ease.

In this article, we will build and deploy a serverless application  that sends messages to Slack by leveraging on Google Cloud Function. We will also use Google Cloud Scheduler to periodically run our  application at an interval of 3hours.

Screenshot from 2019-07-11 00-30-57.png
Our final result.

Google Cloud Functions

Google Cloud Functions is a lightweight compute solution for  developers to create single-purpose, stand-alone functions that respond  to cloud events without the need to manage a server or runtime  environment.

Google Cloud Functions can be written in Node.js, Python, and Go, and are executed in language-specific runtimes.

Cloud Functions can be associated with a specific trigger. The  trigger type determines how and when your function executes. Cloud  Functions supports the following native trigger mechanisms:

  • HTTP Triggers
  • Cloud Pub/Sub Triggers
  • Cloud Storage Triggers
  • Direct Triggers
  • Cloud Firestore
  • Analytics for Firebase
  • Firebase Realtime Database
  • Firebase Authentication

Google Stackdriver provides a suite of monitoring tools that help you  understand what's going on in your Cloud Functions. Logs for Cloud  Functions are viewable in the Stackdriver Logging UI.

Cloud Function can be used for multiple cases such as Serverless  application backends, Real-time data processing systems or Artificial  Intelligence applications.

Cloud Scheduler

Google Cloud Scheduler is a fully managed, scalable, and  fault-tolerant cron job scheduler that allows engineers to automate all  their scheduled tasks in one place.

Google Cloud Scheduler allows you set up fully managed scheduled units of work to be executed at defined times or regular intervals with support for Unix cron format.

Cloud Scheduler can be associated with a target which can be either of the following:

  • HTTP/S endpoints
  • Cloud Pub/Sub topics
  • App Engine applications

In addition, Stackdriver integrates with  Cloud Scheduler providing powerful logging for greater transparency into job execution and performance.

Cloud Scheduler can be used for multiple cases such as sending out a  report email on a daily basis, updating some cached data every 10  minutes, or making requests to an endpoint.

Setting up Slack

Sign in to your Slack workspace and  Create a new Slack app  as follows:

Screenshot from 2019-07-11 00-00-51.png
  • Choose the app's name and your Slack team. Click Create.
  • Click Incoming Webhooks.
  • Enable incoming webhooks.
Screenshot from 2019-07-11 00-02-14.png
  • Click Add New Webhook to Team. An authorization page opens.
  • From the drop-down menu, select the channel to which you would like notifications sent. We'll be using the #random channel
  • Click Authorize.
Screenshot from 2019-07-11 00-02-45.png
  • A webhook for your Slack application has been created. Copy the webhook and save it for later use.
Screenshot from 2019-07-11 00-04-50.png

Setting up Cloud Function

Visit Cloud Functions and  Create Function

  • Enter your function's name
  • Set Memory Allocated : 256MB
  • Set Trigger : HTTP
  • Authentication : Check - Allow unauthenticated invocations
  • Source code : Select - Inline editor
  • Runtime : Node.js 8 (Feel free to change this to suit your choice in future)
  • Function to execute : sendToSlack

Paste the following code snippet into the Inline editor and replace the value of url to your Webhook from Slack and Deploy your function.

// index.js file contents
const IncomingWebhook = require('@slack/webhook').IncomingWebhook;
const url = "https://hooks.slack.com/services/XYZ";

const webhook = new IncomingWebhook(url);

// Send the notification - Gets callled by Cloud Scheduler
module.exports.sendToSlack = () => {
  (async () => {
    await webhook.send({
      icon_emoji: ':male-police-officer:',
      text: '@here Take a coffee break.',
    });
  })();
};
// package.json file contents
{
  "name": "cloud-functions-scheduler-slack",
  "version": "0.0.1",
  "description": "Google Cloud Functions and Cloud Scheduler - Building a Slack Reminder Bot",
  "main": "index.js",
  "dependencies": {
    "@slack/webhook": "^5.0.0"
  }
}
Screenshot from 2019-07-11 01-38-56.png

Once done, you can visit the URL on your Cloud Function page to test.

Screenshot from 2019-07-11 01-45-51.png

Setting up Cloud Scheduler

Visit Cloud Scheduler and Create Job

  • Enter job name
  • Set Frequency : every 3 hours
  • Target : Select HTTP
  • Set URL :  (Use your deployed Cloud Function URL)
  • HTTP Method : Select GET
Screenshot from 2019-07-11 01-53-42.png

Great! Your should get the following message from the RestReminder Bot on your Slack Channel every 3 hours.

Screenshot from 2019-07-11 00-30-57.png

Additional Resources

Thanks for reading through! Let me know if I missed any step, if  something didn’t work out quite right for you or if this guide was  helpful.

Tags

Timothy Olaleke

Software Developer & Google Cloud Enthusiast

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.