Compare commits
16 Commits
2ee81c19c8
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b26e11ac88 | |||
| 3d60361fa2 | |||
| f5fe993105 | |||
| b182c18d49 | |||
| 6930389a39 | |||
| d5f237e1f3 | |||
| c3d0451d62 | |||
| 950467bd2e | |||
| ba484557b8 | |||
| 1b15abb7f7 | |||
| f6ee57d25c | |||
| d22bf0a3b5 | |||
| 98e9eb44d3 | |||
| f6749f4e9e | |||
| 9ce4a9172a | |||
| 390ddd8c62 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
sco
|
sco
|
||||||
|
.DS_Store
|
||||||
|
|||||||
7
Makefile
7
Makefile
@@ -29,6 +29,7 @@ default: fmt
|
|||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
go fmt ./...
|
go fmt ./...
|
||||||
|
goimports -l -w .
|
||||||
|
|
||||||
docker-build:
|
docker-build:
|
||||||
docker build -t $(IMAGENAME) .
|
docker build -t $(IMAGENAME) .
|
||||||
@@ -41,5 +42,9 @@ build: ./$(FN)
|
|||||||
go-get:
|
go-get:
|
||||||
cd cmd/$(FN) && go get -v
|
cd cmd/$(FN) && go get -v
|
||||||
|
|
||||||
./$(FN): */*.go cmd/*/*.go go-get
|
vet:
|
||||||
|
go vet ./...
|
||||||
|
bash -c 'test -z "$$(gofmt -l .)"'
|
||||||
|
|
||||||
|
./$(FN): */*.go cmd/*/*.go go-get vet
|
||||||
cd cmd/$(FN) && go build -o ../../$(FN) $(GOFLAGS) .
|
cd cmd/$(FN) && go build -o ../../$(FN) $(GOFLAGS) .
|
||||||
|
|||||||
17
README.md
17
README.md
@@ -2,6 +2,23 @@
|
|||||||
|
|
||||||
Mattermost bot.
|
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
|
# status
|
||||||
|
|
||||||
[](https://drone.datavi.be/sneak/sco)
|
[](https://drone.datavi.be/sneak/sco)
|
||||||
|
|||||||
99
bot/aqi.go
Normal file
99
bot/aqi.go
Normal 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)
|
||||||
|
}
|
||||||
33
bot/bot.go
33
bot/bot.go
@@ -1,16 +1,17 @@
|
|||||||
package bot
|
package bot
|
||||||
|
|
||||||
import "github.com/kr/pretty"
|
//import "github.com/kr/pretty"
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mattermost/mattermost-server/v5/model"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost-server/v5/model"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
@@ -51,7 +52,7 @@ func (b *Bot) identify() {
|
|||||||
Msg("starting")
|
Msg("starting")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) Main() int {
|
func (b *Bot) Main() {
|
||||||
println(b.BotName)
|
println(b.BotName)
|
||||||
|
|
||||||
b.StartupUnixTime = time.Now().Unix()
|
b.StartupUnixTime = time.Now().Unix()
|
||||||
@@ -106,7 +107,6 @@ func (b *Bot) Main() int {
|
|||||||
|
|
||||||
// You can block forever with
|
// You can block forever with
|
||||||
select {}
|
select {}
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) MakeSureServerIsRunning() {
|
func (b *Bot) MakeSureServerIsRunning() {
|
||||||
@@ -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
|
||||||
@@ -247,11 +256,16 @@ func (b *Bot) HandleMsgFromChannel(event *model.WebSocketEvent) {
|
|||||||
return
|
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)
|
b.HandleWeatherRequest(post.ChannelId, post.Id, post.Message)
|
||||||
return
|
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 {
|
if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched {
|
||||||
b.SendMsgToChannel("yes I'm running", post.Id, post.ChannelId)
|
b.SendMsgToChannel("yes I'm running", post.Id, post.ChannelId)
|
||||||
return
|
return
|
||||||
@@ -285,9 +299,10 @@ func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME check and see if the message from mm is a bot message, if so,
|
if post.Message == "" {
|
||||||
// ignore it
|
// null message, we can probably ignore it.
|
||||||
pretty.Print(post)
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if matched, _ := regexp.MatchString(`(?:^|\W)shutdown(?:$|\W)`, post.Message); matched {
|
if matched, _ := regexp.MatchString(`(?:^|\W)shutdown(?:$|\W)`, post.Message); matched {
|
||||||
b.Shutdown()
|
b.Shutdown()
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/mattn/go-isatty"
|
"github.com/mattn/go-isatty"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bot) setupLogging() {
|
func (b *Bot) setupLogging() {
|
||||||
|
|||||||
92
bot/metar.go
92
bot/metar.go
@@ -2,15 +2,92 @@ package bot
|
|||||||
|
|
||||||
//import "github.com/kr/pretty"
|
//import "github.com/kr/pretty"
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MetarResponse struct {
|
||||||
|
Meta struct {
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
StationsUpdated string `json:"stations_updated"`
|
||||||
|
} `json:"meta"`
|
||||||
|
Altimeter struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value float64 `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"altimeter"`
|
||||||
|
Clouds []interface{} `json:"clouds"`
|
||||||
|
FlightRules string `json:"flight_rules"`
|
||||||
|
Other []interface{} `json:"other"`
|
||||||
|
Sanitized string `json:"sanitized"`
|
||||||
|
Visibility struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value int `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"visibility"`
|
||||||
|
WindDirection struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value int `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"wind_direction"`
|
||||||
|
WindGust struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value int `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"wind_gust"`
|
||||||
|
WindSpeed struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value int `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"wind_speed"`
|
||||||
|
WxCodes []interface{} `json:"wx_codes"`
|
||||||
|
Raw string `json:"raw"`
|
||||||
|
Station string `json:"station"`
|
||||||
|
Time struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Dt time.Time `json:"dt"`
|
||||||
|
} `json:"time"`
|
||||||
|
Remarks string `json:"remarks"`
|
||||||
|
Dewpoint struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value int `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"dewpoint"`
|
||||||
|
RemarksInfo struct {
|
||||||
|
DewpointDecimal struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value float64 `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"dewpoint_decimal"`
|
||||||
|
TemperatureDecimal struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value float64 `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"temperature_decimal"`
|
||||||
|
} `json:"remarks_info"`
|
||||||
|
RunwayVisibility []interface{} `json:"runway_visibility"`
|
||||||
|
Temperature struct {
|
||||||
|
Repr string `json:"repr"`
|
||||||
|
Value int `json:"value"`
|
||||||
|
Spoken string `json:"spoken"`
|
||||||
|
} `json:"temperature"`
|
||||||
|
WindVariableDirection []interface{} `json:"wind_variable_direction"`
|
||||||
|
Units struct {
|
||||||
|
Altimeter string `json:"altimeter"`
|
||||||
|
Altitude string `json:"altitude"`
|
||||||
|
Temperature string `json:"temperature"`
|
||||||
|
Visibility string `json:"visibility"`
|
||||||
|
WindSpeed string `json:"wind_speed"`
|
||||||
|
} `json:"units"`
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) HandleWeatherRequest(channelid string, postid string, message string) {
|
func (b *Bot) HandleWeatherRequest(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
|
||||||
@@ -52,7 +129,16 @@ func (b *Bot) HandleWeatherRequest(channelid string, postid string, message stri
|
|||||||
|
|
||||||
data, _ := ioutil.ReadAll(resp.Body)
|
data, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
log.Info().Msgf("weather %s: %s", loc, data)
|
var parsedMetarResponse MetarResponse
|
||||||
b.SendMsgToChannel(fmt.Sprintf("weather %s: %s", loc, data), postid, channelid)
|
|
||||||
|
|
||||||
|
log.Info().Msgf("weather %s: %s", loc, data)
|
||||||
|
|
||||||
|
err = nil
|
||||||
|
err = json.Unmarshal([]byte(data), &parsedMetarResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
b.SendMsgToChannel("error deserializing metar data", postid, channelid)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.SendMsgToChannel(fmt.Sprintf("weather for `%s`: \n```\n%+v\n```\n", loc, parsedMetarResponse), postid, channelid)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
import "git.eeqj.de/sneak/sco/bot"
|
"os"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/sco/bot"
|
||||||
|
)
|
||||||
|
|
||||||
var Version string
|
var Version string
|
||||||
var Commit string
|
var Commit string
|
||||||
|
|
||||||
var Buildarch string
|
var Buildarch string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -23,5 +27,5 @@ func main() {
|
|||||||
b.AccountFirstname = "LSV"
|
b.AccountFirstname = "LSV"
|
||||||
b.AccountLastname = "Serious Callers Only"
|
b.AccountLastname = "Serious Callers Only"
|
||||||
})
|
})
|
||||||
os.Exit(mybot.Main())
|
mybot.Main()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user