vega/testbot/testbot.go

77 lines
1.8 KiB
Go

package main
import (
"io/ioutil"
"log"
"os"
"strings"
"time"
"git.saintnet.tech/stryan/vega/botlib"
"git.saintnet.tech/stryan/vega/weatherbot"
"github.com/go-co-op/gocron"
"github.com/spf13/viper"
"maunium.net/go/mautrix"
)
type TestBot struct {
Bot *botlib.Bot
Conf *botlib.Config
Jobs chan *mautrix.Event
Scheduler *gocron.Scheduler
StateFile string
schan chan struct{} //for stoping scheduler
}
func (t *TestBot) Shutdown() {
log.Printf("%v is shutting down\n", t.Conf.Name)
t.Bot.Client.StopSync()
t.Scheduler.Clear()
if t.schan != nil {
close(t.schan)
}
t.DumpState()
os.Exit(0)
}
func (t *TestBot) DumpState() {
id := []byte(t.Bot.ManagementRoomID)
err := ioutil.WriteFile("state", id, 0644)
if err != nil {
log.Fatal(err)
}
}
func (t *TestBot) LoadState() {
dat, err := ioutil.ReadFile(t.StateFile)
if err != nil {
log.Fatal(err)
}
t.Bot.ManagementRoomID = strings.Trim(string(dat), "\n")
}
func (t *TestBot) InitActions() {
log.Printf("%v initiating actions\n", t.Conf.Name)
ActionList = make(map[string]Action)
ActionList["version"] = func(inputs ...string) string { return "0.0.1" }
ActionList["echo"] = func(inputs ...string) string {
return strings.Join(inputs[:], "")
}
ActionList["weather"] = func(inputs ...string) string {
return weatherbot.GetDailyReport(viper.GetString("owm_api_key"), viper.GetString("lat"), viper.GetString("long"))
}
log.Printf("%v knows the following actions:\n", t.Conf.Name)
for k, _ := range ActionList {
log.Printf(" Action: %v\n", k)
}
}
func (t *TestBot) InitScheduler() {
log.Printf("%v initiating scheduler\n", t.Conf.Name)
loc, _ := time.LoadLocation("America/New_York")
t.Scheduler = gocron.NewScheduler(loc)
t.Scheduler.Every(1).Day().At("9:45").Do(t.WeatherReport)
t.schan = t.Scheduler.Start()
}