Compare commits
2 Commits
6930389a39
...
next
| Author | SHA1 | Date | |
|---|---|---|---|
| 3d60361fa2 | |||
| b182c18d49 |
27
bot/aqi.go
27
bot/aqi.go
@@ -13,7 +13,7 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AQIResponse []struct {
|
type AQIResponse struct {
|
||||||
DateObserved string `json:"DateObserved"`
|
DateObserved string `json:"DateObserved"`
|
||||||
HourObserved int `json:"HourObserved"`
|
HourObserved int `json:"HourObserved"`
|
||||||
LocalTimeZone string `json:"LocalTimeZone"`
|
LocalTimeZone string `json:"LocalTimeZone"`
|
||||||
@@ -29,6 +29,19 @@ type AQIResponse []struct {
|
|||||||
} `json:"Category"`
|
} `json:"Category"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatAQIResponse(input *[]AQIResponse, zip string) string {
|
||||||
|
dinput := *input
|
||||||
|
in := dinput[0]
|
||||||
|
when := fmt.Sprintf("%s at %d:00 %s", in.DateObserved, in.HourObserved, in.LocalTimeZone)
|
||||||
|
bytes, err := json.Marshal(input)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
formatted := string(bytes)
|
||||||
|
out := fmt.Sprintf("# AQI for %s as of %s:\n %s", zip, when, formatted)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message string) {
|
func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message string) {
|
||||||
|
|
||||||
// we are using a very bare image with no CA cert bundle
|
// we are using a very bare image with no CA cert bundle
|
||||||
@@ -44,10 +57,10 @@ func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message s
|
|||||||
b.SendMsgToChannel("error, sorry", postid, channelid)
|
b.SendMsgToChannel("error, sorry", postid, channelid)
|
||||||
}
|
}
|
||||||
|
|
||||||
zip4 := matches[1]
|
zip := matches[1]
|
||||||
|
|
||||||
apikey := os.Getenv("AIRNOW_API_KEY")
|
apikey := os.Getenv("AIRNOW_API_KEY")
|
||||||
url := fmt.Sprintf("http://www.airnowapi.org/aq/observation/zipCode/current/?format=application/json&zipCode=%s&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", zip, apikey)
|
||||||
|
|
||||||
log.Info().Msgf("calculated url: `%s`", url)
|
log.Info().Msgf("calculated url: `%s`", url)
|
||||||
|
|
||||||
@@ -69,9 +82,9 @@ func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message s
|
|||||||
|
|
||||||
data, _ := ioutil.ReadAll(resp.Body)
|
data, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
var parsedAQIResponse AQIResponse
|
var parsedAQIResponse []AQIResponse
|
||||||
|
|
||||||
log.Info().Msgf("aqi %s: %s", zip4, data)
|
log.Info().Msgf("aqi %s: %s", zip, data)
|
||||||
|
|
||||||
err = nil
|
err = nil
|
||||||
err = json.Unmarshal([]byte(data), &parsedAQIResponse)
|
err = json.Unmarshal([]byte(data), &parsedAQIResponse)
|
||||||
@@ -80,5 +93,7 @@ func (b *Bot) HandleAirQualityRequest(channelid string, postid string, message s
|
|||||||
b.SendMsgToChannel("error deserializing AQI data", postid, channelid)
|
b.SendMsgToChannel("error deserializing AQI data", postid, channelid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
b.SendMsgToChannel(fmt.Sprintf("AQI for `%s`: \n```\n%+v\n```\n", zip4, parsedAQIResponse), postid, channelid)
|
|
||||||
|
msg := formatAQIResponse(&parsedAQIResponse, zip)
|
||||||
|
b.SendMsgToChannel(msg, postid, channelid)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -221,6 +221,8 @@ func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME check for parts and joins and whatnot and ignore those
|
||||||
|
|
||||||
// check to see if we have been addressed
|
// check to see if we have been addressed
|
||||||
if matched, _ := regexp.MatchString(`^\s*`+b.BotName+`\s*`, post.Message); matched {
|
if matched, _ := regexp.MatchString(`^\s*`+b.BotName+`\s*`, post.Message); matched {
|
||||||
println("i have been addressed in channel " + post.ChannelId)
|
println("i have been addressed in channel " + post.ChannelId)
|
||||||
@@ -229,6 +231,13 @@ func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if matched, _ := regexp.MatchString(`^\s*bot([\,]?)\s*`, post.Message); matched {
|
||||||
|
println("i have been addressed in channel " + post.ChannelId)
|
||||||
|
//b.SendMsgToDebuggingChannel("i have been addressed in channel "+post.ChannelId, "")
|
||||||
|
b.HandleMsgFromChannel(event)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if event.Broadcast.ChannelId == b.debuggingChannel.Id {
|
if event.Broadcast.ChannelId == b.debuggingChannel.Id {
|
||||||
b.HandleMsgFromDebuggingChannel(event)
|
b.HandleMsgFromDebuggingChannel(event)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user