move bot commands out to own files
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b8b53e8c5e
commit
0d75d4a5ac
103
bot/bot.go
103
bot/bot.go
@ -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)
|
||||
|
@ -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
58
bot/metar.go
Normal 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)
|
||||
|
||||
}
|
1
go.mod
1
go.mod
@ -5,5 +5,6 @@ go 1.15
|
||||
require (
|
||||
github.com/kr/pretty v0.1.0
|
||||
github.com/mattermost/mattermost-server/v5 v5.26.2
|
||||
github.com/mattn/go-isatty v0.0.12
|
||||
github.com/rs/zerolog v1.19.0
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@ -332,6 +332,7 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
|
||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
|
||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
@ -677,6 +678,7 @@ golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
|
||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
Loading…
Reference in New Issue
Block a user