package bot //import "github.com/kr/pretty" import ( "fmt" "github.com/rs/zerolog/log" "io/ioutil" "net/http" "os" "regexp" "time" ) func (b *Bot) HandleWeatherRequest(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("weather request received: `%s`", message) r := regexp.MustCompile(`metar\s+([A-Za-z]{4})`) 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) log.Info().Msgf("calculated url: `%s`", url) client := http.Client{ Timeout: 5 * time.Second, } req, err := http.NewRequest("GET", url, nil) req.Header.Add("Authorization", `Token `+token) resp, err := client.Do(req) if err != nil { b.SendMsgToChannel(fmt.Sprintf("weather fetch error: %s", err), postid, channelid) return } 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) }