Post Weather Forecast to Slack Regularly


I created a script to post weather forecast to Slack everyday, with Google Apps Script. It can be configured to post only specific weekdays.

Direction

  • Get forecast information from Open Weather Map.
  • Write script with CoffeeScript.
  • Post to Slack using Incoming WebHooks.
  • Post automatically using Google Apps Script.

Preparation

Get url of Incoming Webhooks in Slack.

Code

Open Weather Map become to require API Key. The following code is old, so please modify if you use it.

First, prepare CoffeeScript like the following. If you want to copy the following code, change Slack url to one that you configured.

It is CoffeeScript code, so it work as JavaScript mostly, but UrlFetchApp, Utilities is Google Apps object.

If you write the code without creating class, it may be more short code.

After creating CoffeScript, compile it to JavaScript and copy it to Google Apps Script like the following.

Put the generated JavaScript into execute function. And choose execute function as the function to be executed first. If you want to execute and post weather forecast regularly, click menu, Resource and Current Project’s Triggers and configure when to post data to slack.

The above is posting to slack. You can also send data to mail, an example is written in the article, “Send Mail Automatically“. If you want to execute procedure only on business day, look at the articke “Process only on Business Day“.