- Part 1: Setup the project (5-10 minutes)
- Part 2: Get the weather (5-10 minutes)
- Part 3: Respond to the
weather <place>
command (5-10 minutes)
The goal of the portion is to setup a project weatherbot
which will
hold the code for your weather bot project.
We're going to use two libraries to build our bot:
- nlopes Slack library - https://github.com/nlopes/slack
- Open Weather Map http://briandowns.github.io/openweathermap/
- 🌟 Create a directory
weatherbot
for your code in your $GOPATH, and a filemain.go
that will contain the code.
E.g. under $GOPATH/bostongolang
```bash
mkdir -p weatherbot
cd weatherbot
touch weatherbot/weatherbot.go
In Windows:
md weatherbot
cd weatherbot
```
-
Create an account on openweathermap and setup your API key.
- 🌟 Sign up to openweathermap.org and get an API key.
- 🌟 Set your
OWM_API_KEY
environment variable to the API key you just created, e.g.
export OWM_API_KEY=5634bf4c007be5d2f95aceeae356a2ba or in Windows SET OWM_API_KEY=5634bf4c007be5d2f95aceeae356a2ba
-
Setup your weatherbot Slack API token.
- 🌟 Set your Slack bot's
SLACK_API_TOKEN
environment variable:
export SLACK_API_TOKEN=my-token-foo-bar
- 🌟 Set your Slack bot's
-
Get the necessary Go libraries:
- 🌟 Get the slack libraries
go get github.com/BeepBoopHQ/go-slackbot go get github.com/nlopes/slack
- 🌟 Get the openweathermap library
go get github.com/briandowns/openweathermap
-
Populate
weatherbot.go
with a shell to get started:package main import ( "fmt" "os" "strings" "golang.org/x/net/context" slackbot "github.com/BeepBoopHQ/go-slackbot" "github.com/briandowns/openweathermap" "github.com/nlopes/slack" ) func main() { ListenForWeather() } func GetWeather(place string) (*openweathermap.CurrentWeatherData, error) { // TODO: get the weather for the place return nil, nil } func ListenForWeather() { bot := slackbot.New(os.Getenv("SLACK_API_TOKEN")) // TODO: add a bot.Hear listener here for a `weather <place>` request bot.Run() } func WeatherHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) { // TODO: Respond to a `weather <place>` request }
Type
go run weatherbot.go
and verify the code works.
In this section, we'll populate the GetWeather
function that
will connect to openweather using the openweathermap library.
-
🌟 Find the
GetWeather
function and add some code that connects to openweathermap.- Hint: To connect, you'll need to use
NewCurrent
method from the openweathermap library. Check out the README for examples.
- Hint: To connect, you'll need to use
-
🌟 Get the weather object for the
place
provided- Hint: You'll need to use the
CurrentByName
function. Check out the README for examples.
- Hint: You'll need to use the
-
🌟 To test, you can add a temporary
GetWeather("02145")
call to themain()
function and print the results. Rungo run weatherbot.go
to test.
We need to add a bot.Hear handler that will look for a weather <place>
command.
- Populate the
ListenForWeather
handler to respond to messages
-
🌟 In the
ListenForWeather
function, identify the "TODO:" statement and add abot.Hear
handler that looks for anyweather (.*)
message from Slack. It should call theWeatherHandler
response. -
You can look at this code for an example on how to listen for a message.
- Populate the
WeatherHandler
function
-
🌟 Parse the
place
parameter for weather lookup. Hint: you'll need to look atevt.Msg.Text
and grab the second part. -
🌟 Once you have the place, call
GetWeather
for the place to get a response. -
🌟 Format the response and add a
bot.Reply
that will return something like this:
The current temperature for Somerville is 70 degrees farenheight (light rain)
* Hint: Look at the `Name`, `Main`, and `Weather[0].Description` fields in the GetWeather() response