add zerolog, maybe fix weather
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-09-08 20:56:24 -07:00
parent 6ec52ebca9
commit b8b53e8c5e
4 changed files with 75 additions and 7 deletions

View File

@@ -4,6 +4,8 @@ package bot
import (
"fmt"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/rs/zerolog/log"
"io/ioutil"
"net/http"
"os"
"os/signal"
@@ -43,6 +45,14 @@ func New(options ...func(s *Bot)) *Bot {
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 {
println(b.BotName)
@@ -50,6 +60,8 @@ func (b *Bot) Main() int {
b.SetupGracefulShutdown()
b.setupLogging()
b.client = model.NewAPIv4Client(b.APIURL)
// 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) {
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})`)
loc := r.FindString(message)
if loc == "" {
matches := r.FindStringSubmatch(message)
if len(matches) < 2 {
b.SendMsgToChannel("error, sorry", postid, channelid)
}
loc := matches[1]
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)
msg = fmt.Sprintf("calculated url: `%s`", url)
b.SendMsgToChannel(msg, postid, channelid)
log.Info().Msgf("calculated url: `%s`", url)
client := http.Client{
Timeout: 5 * time.Second,
@@ -258,7 +276,15 @@ func (b *Bot) HandleWeatherRequest(channelid string, postid string, message stri
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()
}