move bot commands out to own files
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-09-08 20:59:46 -07:00
parent b8b53e8c5e
commit 0d75d4a5ac
5 changed files with 91 additions and 74 deletions

View File

@@ -5,8 +5,6 @@ import (
"fmt"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/rs/zerolog/log"
"io/ioutil"
"net/http"
"os"
"os/signal"
"regexp"
@@ -242,52 +240,6 @@ func (b *Bot) Shutdown() {
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
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)
}
func (b *Bot) HandleMsgFromChannel(event *model.WebSocketEvent) {
post := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
@@ -330,37 +282,40 @@ func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
println("responding to debugging channel msg")
post := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
if post != nil {
if post == nil {
return
}
// ignore my events
if matched, _ := regexp.MatchString(`(?:^|\W)shutdown(?:$|\W)`, post.Message); matched {
b.Shutdown()
return
}
// FIXME check and see if the message from mm is a bot message, if so,
// ignore it
// if you see any word matching 'alive' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
if matched, _ := regexp.MatchString(`(?:^|\W)shutdown(?:$|\W)`, post.Message); matched {
b.Shutdown()
return
}
// if you see any word matching 'up' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)up(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'alive' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'running' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)running(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'up' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)up(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'hello' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)hello(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'running' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)running(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'hello' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)hello(?:$|\W)`, post.Message); matched {
b.SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
b.SendMsgToChannel("I did not understand your command, sorry", post.Id, post.ChannelId)

View File

@@ -4,6 +4,7 @@ import (
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"time"
)

58
bot/metar.go Normal file
View File

@@ -0,0 +1,58 @@
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)
}