next #2

Manually merged
sneak merged 4 commits from next into master 2020-09-09 04:03:00 +00:00
4 changed files with 75 additions and 7 deletions
Showing only changes of commit b8b53e8c5e - Show all commits

View File

@ -4,6 +4,8 @@ package bot
import ( import (
"fmt" "fmt"
"github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/model"
"github.com/rs/zerolog/log"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -43,6 +45,14 @@ func New(options ...func(s *Bot)) *Bot {
return b return b
} }
func (b *Bot) identify() {
log.Info().
Str("version", b.Version).
Str("buildarch", b.Buildarch).
Str("commit", b.Commit).
Msg("starting")
}
func (b *Bot) Main() int { func (b *Bot) Main() int {
println(b.BotName) println(b.BotName)
@ -50,6 +60,8 @@ func (b *Bot) Main() int {
b.SetupGracefulShutdown() b.SetupGracefulShutdown()
b.setupLogging()
b.client = model.NewAPIv4Client(b.APIURL) b.client = model.NewAPIv4Client(b.APIURL)
// Lets test to see if the mattermost server is up and running // Lets test to see if the mattermost server is up and running
@ -231,20 +243,26 @@ func (b *Bot) Shutdown() {
} }
func (b *Bot) HandleWeatherRequest(channelid string, postid string, message string) { func (b *Bot) HandleWeatherRequest(channelid string, postid string, message string) {
msg := fmt.Sprintf("weather request received: `%s`", message)
b.SendMsgToChannel(msg, postid, channelid) // 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("weather request received: `%s`", message)
r := regexp.MustCompile(`metar\s+([A-Za-z]{4})`) r := regexp.MustCompile(`metar\s+([A-Za-z]{4})`)
loc := r.FindString(message) matches := r.FindStringSubmatch(message)
if loc == "" { if len(matches) < 2 {
b.SendMsgToChannel("error, sorry", postid, channelid) b.SendMsgToChannel("error, sorry", postid, channelid)
} }
loc := matches[1]
token := os.Getenv("METAR_API_TOKEN") token := os.Getenv("METAR_API_TOKEN")
url := fmt.Sprintf("https://avwx.rest/api/metar/%s?options=&airport=true&reporting=true&format=json&onfail=cache", loc) url := fmt.Sprintf("https://avwx.rest/api/metar/%s?options=&airport=true&reporting=true&format=json&onfail=cache", loc)
msg = fmt.Sprintf("calculated url: `%s`", url) log.Info().Msgf("calculated url: `%s`", url)
b.SendMsgToChannel(msg, postid, channelid)
client := http.Client{ client := http.Client{
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
@ -258,7 +276,15 @@ func (b *Bot) HandleWeatherRequest(channelid string, postid string, message stri
return return
} }
b.SendMsgToChannel(fmt.Sprintf("weather %s: %s", loc, resp), postid, channelid) if resp.StatusCode != http.StatusOK {
b.SendMsgToChannel(fmt.Sprintf("weather fetch error: http status %d", resp.StatusCode), postid, channelid)
return
}
data, _ := ioutil.ReadAll(resp.Body)
log.Info().Msgf("weather %s: %s", loc, data)
b.SendMsgToChannel(fmt.Sprintf("weather %s: %s", loc, data), postid, channelid)
} }

37
bot/logger.go Normal file
View File

@ -0,0 +1,37 @@
package bot
import (
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"time"
)
func (b *Bot) setupLogging() {
log.Logger = log.With().Caller().Logger()
tty := isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
if tty {
out := zerolog.NewConsoleWriter(
func(w *zerolog.ConsoleWriter) {
// Customize time format
w.TimeFormat = time.RFC3339
},
)
log.Logger = log.Output(out)
}
// always log in UTC
zerolog.TimestampFunc = func() time.Time {
return time.Now().UTC()
}
zerolog.SetGlobalLevel(zerolog.DebugLevel)
//zerolog.SetGlobalLevel(zerolog.InfoLevel)
//if viper.GetBool("debug") {
//}
b.identify()
}

1
go.mod
View File

@ -5,4 +5,5 @@ go 1.15
require ( require (
github.com/kr/pretty v0.1.0 github.com/kr/pretty v0.1.0
github.com/mattermost/mattermost-server/v5 v5.26.2 github.com/mattermost/mattermost-server/v5 v5.26.2
github.com/rs/zerolog v1.19.0
) )

4
go.sum
View File

@ -443,6 +443,9 @@ github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qq
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.19.0 h1:hYz4ZVdUgjXTBUmrkrw55j1nHx68LfOKIQk5IYtyScg=
github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/rudderlabs/analytics-go v3.2.1+incompatible/go.mod h1:LF8/ty9kUX4PTY3l5c97K3nZZaX5Hwsvt+NBaRL/f30= github.com/rudderlabs/analytics-go v3.2.1+incompatible/go.mod h1:LF8/ty9kUX4PTY3l5c97K3nZZaX5Hwsvt+NBaRL/f30=
github.com/russellhaering/goxmldsig v0.0.0-20180430223755-7acd5e4a6ef7/go.mod h1:Oz4y6ImuOQZxynhbSXk7btjEfNBtGlj2dcaOvXl2FSM= github.com/russellhaering/goxmldsig v0.0.0-20180430223755-7acd5e4a6ef7/go.mod h1:Oz4y6ImuOQZxynhbSXk7btjEfNBtGlj2dcaOvXl2FSM=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
@ -698,6 +701,7 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190424220101-1e8e1cfdf96b/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190424220101-1e8e1cfdf96b/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=