2020-04-11 16:26:59 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-04-15 16:11:58 -04:00
|
|
|
"time"
|
2020-04-11 16:26:59 -04:00
|
|
|
|
|
|
|
"git.saintnet.tech/stryan/vega/botlib"
|
2020-04-15 16:11:58 -04:00
|
|
|
"git.saintnet.tech/stryan/vega/weatherbot"
|
|
|
|
"github.com/go-co-op/gocron"
|
|
|
|
"github.com/spf13/viper"
|
2020-04-11 16:26:59 -04:00
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestBot struct {
|
2020-04-15 16:11:58 -04:00
|
|
|
Bot *botlib.Bot
|
|
|
|
Conf *botlib.Config
|
|
|
|
Jobs chan *mautrix.Event
|
|
|
|
Scheduler *gocron.Scheduler
|
|
|
|
schan chan struct{} //for stoping scheduler
|
2020-04-11 16:26:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestBot) Shutdown() {
|
2020-04-15 16:11:58 -04:00
|
|
|
log.Printf("%v is shutting down\n", t.Conf.Name)
|
2020-04-11 16:26:59 -04:00
|
|
|
t.Bot.Client.StopSync()
|
2020-04-15 16:11:58 -04:00
|
|
|
t.Scheduler.Clear()
|
|
|
|
if t.schan != nil {
|
|
|
|
close(t.schan)
|
|
|
|
}
|
2020-04-11 16:26:59 -04:00
|
|
|
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("state")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-15 16:11:58 -04:00
|
|
|
t.Bot.ManagementRoomID = strings.Trim(string(dat), "\n")
|
2020-04-11 16:26:59 -04:00
|
|
|
}
|
2020-04-14 16:54:31 -04:00
|
|
|
|
|
|
|
func (t *TestBot) InitActions() {
|
2020-04-15 16:11:58 -04:00
|
|
|
log.Printf("%v initiating actions\n", t.Conf.Name)
|
2020-04-14 16:54:31 -04:00
|
|
|
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[:], "")
|
|
|
|
}
|
2020-04-15 16:11:58 -04:00
|
|
|
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()
|
2020-04-14 16:54:31 -04:00
|
|
|
}
|