How to create a Typetalk bot for weather alerts

Whether deciding what to wear to and from the office or planning a team outing for the coming week, we’re constantly adjusting for the weather. Using the OpenWeatherMap API and Google Apps Script, you can make keeping track of the weather a little easier.

With this Typetalk bot, you can schedule regular weather updates and receive replies to current weather requests right in a Typetalk topic.

How to create a weather bot for Typetalk

Get an API Key

We are using the OpenWeatherMap API. You will need to sign up on their website to use the API.

create api key account

Simply create an account, and retrieve a free API Key.

api keys

Reply with current weather

To start, we need to create a new Typetalk bot that you can mention with a city name and it will reply with the current weather in that city.

weather bot

1. Create Google Apps Script

First, we need to create the Google Apps Script and run a test to make sure the API call succeeds.

In your Google Drive, create a new Google Apps Script.

google apps script

You can copy our code and replace the API Key with your own, or create your own code from scratch. Once you’ve finished, save the script.

Select the “testCurrent” function in your dropdown menu, and click the triangle button to test the code.

test current

After running the test, you can see a log by selecting View > Logs from the dropdown menu.

view logs

If the API call succeeded, you should see logs like the following.

logs

2. Deploy your script

Add a doPost function in the code to receive a webhook from Typetalk.

And then “Deploy as web app” from the Publish section of the drop-down menu.

publish

 

For “Execute the app as:” select “Me” and for “Who has access to the app:” select “Anyone, even anonymous”.

deploy as web app

Copy the web app URL in the dialog.

deployed

3. Create a bot

In the Typetalk web app, click the Topic Settings icon and select the “BOTS” tab. Click the “Add New” button, and then create an ID and Full Name. Finally, check “Use Webhook”, and paste the URL that you copied in the previous step before clicking the “Create” button to save.

create a bot

Finally, post a message like “@weather+ Tokyo” in the topic. Your new bot should reply with the current weather.

Scheduled weather forecast

Now we’ll update the weather bot you just created to post weather forecasts at a scheduled time.

weather forecast

1. Update your script

Copy the following code into your script, or create your own.

Run the “testForecast” function to check your script.

2. Post a message to Typetalk

Under Topic Settings > BOTS > Bot Settings, check “topic.post” for the API Scope and copy the “Get or post messages URL” to your clipboard.

check topic.postget or post url
Add the following code to your script, but replace the POST_URL with your bot’s URL, and try to run a “postForecast” function.

3. Set a schedule

Click the “Current project’s trigger” button.

current project trigger button

Add a trigger like the following making sure to select the “postForecast” function.

current projects trigger

Next time your trigger time hits, you will see the weather forecast you selected.

And there you have it! Now you have your very own weather bot that can respond with current weather updates, and share recurring weather forecasts whenever you’d like.

Make communication fun, again

Try It Free