Compare commits

..

5 Commits

Author SHA1 Message Date
3d60361fa2 should now maybe show better aqi
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2020-09-13 18:55:43 -07:00
b182c18d49 should now allow "bot" address too
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2020-09-13 00:49:19 -07:00
d5f237e1f3 update README
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2020-09-08 22:11:55 -07:00
c3d0451d62 typos
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is passing
2020-09-08 22:06:01 -07:00
950467bd2e add AQI lookup
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
2020-09-08 22:03:39 -07:00
3 changed files with 131 additions and 1 deletions

View File

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

99
bot/aqi.go Normal file
View File

@@ -0,0 +1,99 @@
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 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) {
// 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)
}
zip := matches[1]
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", zip, 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
}
msg := formatAQIResponse(&parsedAQIResponse, zip)
b.SendMsgToChannel(msg, postid, channelid)
}

View File

@@ -221,6 +221,8 @@ func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
return
}
// FIXME check for parts and joins and whatnot and ignore those
// check to see if we have been addressed
if matched, _ := regexp.MatchString(`^\s*`+b.BotName+`\s*`, post.Message); matched {
println("i have been addressed in channel " + post.ChannelId)
@@ -229,6 +231,13 @@ func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
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 {
b.HandleMsgFromDebuggingChannel(event)
return
@@ -247,11 +256,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