title | description | author | tags | date_published |
---|---|---|---|---|
Building a Slack reminder app with Cloud Functions and Cloud Scheduler |
Build a Slack app that sends messages to a channel at an interval of 3 hours. |
timtech4u |
Cloud Function, Cloud Scheduler, Slack |
2019-07-13 |
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.
- 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 from GitHub Gist, here.
- 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.
- 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
index.js
andpackage.json
. - For Function to execute, enter
sendToSlack
. - 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.
- 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: