Building a Slack reminder app with Cloud Functions and Cloud Scheduler
Contributed by the Google Cloud community. Not official Google documentation.
In this tutorial, you build and deploy a serverless application that sends messages to Slack using a Cloud Function. You also use Cloud Scheduler to periodically run your application at an interval of 3 hours.
- Create a Slack app.
- Create an HTTP serverless function that sends messages to Slack with a Cloud Function.
- Create a scheduled job that calls your Cloud Function every 3 hours with Cloud Scheduler.
Before you begin
- Create a new Google Cloud project, or use an existing one.
- Enable billing for your project.
- Create a new Slack team, or use a team for which you have permissions to add integrations.
This tutorial uses billable components of Google Cloud including Compute Engine. Use the Pricing Calculator to estimate the costs for your usage.
Slack is free for up to 10 apps and integrations. Check the Slack pricing page for details.
Get the sample code
Get the sample code from GitHub Gist, here.
Create a new Slack app and set up Slack
- Go to the Slack app management page.
- Click Create new app.
- Give the app a name, such as "RestReminder".
- Choose the Slack team for development and where you will eventually install it.
- Click Incoming Webhooks.
- Enable incoming webhooks.
- 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.
- A webhook for your Slack application has been created. Copy the webhook and save it for later use.
Set up the Cloud Function
- Go to the Cloud Functions page of the Cloud Console.
- Click Create Function.
- Enter your function's name in the Name field.
- Set Memory allocated to 256MB.
- Set Trigger to HTTP.
- For Authentication, check Allow unauthenticated invocations.
- For Source code, select Inline editor.
- For Runtime, choose Node.js 8. (Feel free to change this to suit your choice in future.)
- Paste the sample code into the inline editor, both
- For Function to execute, enter
- Click the Create button to deploy the new Cloud Function.
After these steps are done, you can visit the URL on your Cloud Function URL to test.
Set up Cloud Scheduler
- Go to the Cloud Scheduler page of the Cloud Console.
- Click Create Job.
- Enter a job name in the Name field.
- In the Frequency field, enter
0 */3 * * *, which indicates every 3 hours in unix-cron format.
- For Target, select HTTP.
- In the URL field, enter your deployed Cloud Function URL.
- For HTTP method, select GET.
Great! You just scheduled a Slack Bot that sends a message to your Slack channel every 3 hours.
To prevent unnecessary charges, clean up the resources created for this tutorial.
- Delete the project used (if you created a new project).
- Delete the Cloud Scheduler job and Cloud Function.
You might also choose to pause the Cloud Scheduler job.
If you want to learn more about Cloud Functions and Cloud Scheduler, check out the following resources: