https://github.com/sger/weather-bot
Weather bot in Golang
https://github.com/sger/weather-bot
Last synced: 8 months ago
JSON representation
Weather bot in Golang
- Host: GitHub
- URL: https://github.com/sger/weather-bot
- Owner: sger
- Created: 2015-12-20T11:18:06.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-20T15:53:11.000Z (over 10 years ago)
- Last Synced: 2025-01-06T01:23:13.982Z (over 1 year ago)
- Language: Go
- Size: 17.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Weather bot
First create a new bot in Slack more info here [https://api.slack.com/bot-users](https://api.slack.com/bot-users)
Usage:
create a go file with name test_wb.go
```
package main
import (
"fmt"
wb "github.com/sger/weather-bot"
"log"
"os"
"strings"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "usage: weather-bot token\n")
os.Exit(1)
}
ws, r, err := wb.Connect(os.Args[1])
fmt.Println("bot is running, hit ^C to exit")
if err != nil {
log.Fatal(err)
}
for {
m, err := wb.GetMessage(ws)
if err != nil {
log.Fatal(err)
}
if m.Type == "message" && strings.HasPrefix(m.Text, "<@"+r.Self.Id+">") {
fields := strings.Fields(m.Text)
if len(fields) == 2 {
go func(m wb.Message) {
m.Text = getWeather(fields[1])
m.Text += "\nThank you " + "<@" + m.User + ">" + "\n"
wb.PostMessage(ws, m)
}(m)
} else {
go func(m wb.Message) {
m.Text = "sorry wrong usage please write @name-of-the-weather-bot: Athens"
wb.PostMessage(ws, m)
}(m)
}
}
}
}
func getWeather(city string) string {
f, err := wb.Search(city)
if err != nil {
log.Fatal(err)
}
return fmt.Sprintf("Temperature for today\n Kelvin: %.2fK | Celsius: %.2f°C | Fahrenheit: %.2f°F", f.Info.Temp, f.Info.Temp-273.15, f.Info.Temp*9/5-459.67)
}
```
and run it with
```
$ go build test_wb.go
$ ./test_wb my-bot-token
```
open up your slack client and send a message to you bot myBot: london press enter to see the reply