add AQI lookup
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
Jeffrey Paul 2020-09-08 22:03:39 -07:00
parent 1b15abb7f7
commit 950467bd2e
2 changed files with 90 additions and 1 deletions

84
bot/aqi.go Normal file
View File

@ -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)
}

View File

@ -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