From 950467bd2effd6b6c85b04f0b30f46cad5cfab6c Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 22:03:39 -0700 Subject: [PATCH 1/3] add AQI lookup --- bot/aqi.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ bot/bot.go | 7 ++++- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 bot/aqi.go diff --git a/bot/aqi.go b/bot/aqi.go new file mode 100644 index 0000000..74f274d --- /dev/null +++ b/bot/aqi.go @@ -0,0 +1,84 @@ +package bot + +//import "github.com/kr/pretty" +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + "regexp" + "time" + + "github.com/rs/zerolog/log" +) + +type AQIResponse []struct { + DateObserved string `json:"DateObserved"` + HourObserved int `json:"HourObserved"` + LocalTimeZone string `json:"LocalTimeZone"` + ReportingArea string `json:"ReportingArea"` + StateCode string `json:"StateCode"` + Latitude float64 `json:"Latitude"` + Longitude float64 `json:"Longitude"` + ParameterName string `json:"ParameterName"` + AQI int `json:"AQI"` + Category struct { + Number int `json:"Number"` + Name string `json:"Name"` + } `json:"Category"` +} + +func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message string) { + + // we are using a very bare image with no CA cert bundle + // actually if you docker bind mount the ca cert bundle in the right + // place, golang will find it and use it. + //http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + + log.Info().Msgf("aqi request received: `%s`", message) + + r := regexp.MustCompile(`aqi\s+([0-9]{5})`) + matches := r.FindStringSubmatch(message) + if len(matches) < 2 { + b.SendMsgToChannel("error, sorry", postid, channelid) + } + + zip4 := matches[1] + + apikey := os.Getenv("AIRNOW_API_KEY") + url := fmt.Sprintf("http://www.airnowapi.org/aq/observation/zipCode/current/?format=application/json&zipCode=%d&distance=25&API_KEY=%s", zip4, apikey) + + log.Info().Msgf("calculated url: `%s`", url) + + client := http.Client{ + Timeout: 5 * time.Second, + } + req, err := http.NewRequest("GET", url, nil) + resp, err := client.Do(req) + + if err != nil { + b.SendMsgToChannel(fmt.Sprintf("aqi fetch error: %s", err), postid, channelid) + return + } + + if resp.StatusCode != http.StatusOK { + b.SendMsgToChannel(fmt.Sprintf("aqi fetch error: http status %d", resp.StatusCode), postid, channelid) + return + } + + data, _ := ioutil.ReadAll(resp.Body) + + var parsedAQIResponse AQIResponse + + log.Info().Msgf("aqi %s: %s", zip, data) + + err = nil + err = json.Unmarshal([]byte(data), &parsedAQIResponse) + + if err != nil { + b.SendMsgToChannel("error deserializing AQI data", postid, channelid) + return + } + b.SendMsgToChannel(fmt.Sprintf("AQI for `%s`: \n```\n%+v\n```\n", loc, parsedAQIResponse), postid, channelid) +} diff --git a/bot/bot.go b/bot/bot.go index 955f8a3..56472ef 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -247,11 +247,16 @@ func (b *Bot) HandleMsgFromChannel(event *model.WebSocketEvent) { return } - if matched, _ := regexp.MatchString(`(?:^|\W)metar(?:$|\W)`, post.Message); matched { + if matched, _ := regexp.MatchString(`metar\s+[^\s]+`, post.Message); matched { b.HandleWeatherRequest(post.ChannelId, post.Id, post.Message) return } + if matched, _ := regexp.MatchString(`aqi\s+[^\s]+`, post.Message); matched { + b.HandleAirQualityRequest(post.ChannelId, post.Id, post.Message) + return + } + if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched { b.SendMsgToChannel("yes I'm running", post.Id, post.ChannelId) return -- 2.40.1 From c3d0451d6292ac2ef74c2ab9bc69c35580e68b04 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 22:06:01 -0700 Subject: [PATCH 2/3] typos --- bot/aqi.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/aqi.go b/bot/aqi.go index 74f274d..8efa63f 100644 --- a/bot/aqi.go +++ b/bot/aqi.go @@ -47,7 +47,7 @@ func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message s zip4 := matches[1] apikey := os.Getenv("AIRNOW_API_KEY") - url := fmt.Sprintf("http://www.airnowapi.org/aq/observation/zipCode/current/?format=application/json&zipCode=%d&distance=25&API_KEY=%s", zip4, apikey) + url := fmt.Sprintf("http://www.airnowapi.org/aq/observation/zipCode/current/?format=application/json&zipCode=%s&distance=25&API_KEY=%s", zip4, apikey) log.Info().Msgf("calculated url: `%s`", url) @@ -71,7 +71,7 @@ func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message s var parsedAQIResponse AQIResponse - log.Info().Msgf("aqi %s: %s", zip, data) + log.Info().Msgf("aqi %s: %s", zip4, data) err = nil err = json.Unmarshal([]byte(data), &parsedAQIResponse) @@ -80,5 +80,5 @@ func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message s b.SendMsgToChannel("error deserializing AQI data", postid, channelid) return } - b.SendMsgToChannel(fmt.Sprintf("AQI for `%s`: \n```\n%+v\n```\n", loc, parsedAQIResponse), postid, channelid) + b.SendMsgToChannel(fmt.Sprintf("AQI for `%s`: \n```\n%+v\n```\n", zip4, parsedAQIResponse), postid, channelid) } -- 2.40.1 From d5f237e1f385a60b10b1074f80da053d1e49f325 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 22:11:55 -0700 Subject: [PATCH 3/3] update README --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 1ab695c..5d63645 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,23 @@ Mattermost bot. +# Environment Variables + +## for bot comms to mattermost server + +* `SCO_API_URL` +* `SCO_WEBSOCKET_URL` +* `SCO_DEBUG_CHANNEL` +* `SCO_ACCOUNT_EMAIL` +* `SCO_ACCOUNT_PASSWORD` +* `SCO_TEAM_NAME` +* `SCO_ACCOUNT_USERNAME` + +## remote stuff + +* `METAR_API_TOKEN` for https://avwx.rest +* `AIRNOW_API_KEY` for http://www.airnowapi.org + # status [![Build Status](https://drone.datavi.be/api/badges/sneak/sco/status.svg)](https://drone.datavi.be/sneak/sco) -- 2.40.1