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) botlib.Message { resp := "v0.0.1" return botlib.Message{ Type: botlib.MessageResponse, Sender: t.Conf.Name, Receiver: t.Conf.Owner, Body: resp, } } ActionList["echo"] = func(inputs ...string) botlib.Message { resp := strings.Join(inputs[:], "") return botlib.Message{ Type: botlib.MessageResponse, Sender: t.Conf.Name, Receiver: t.Conf.Name, Body: resp, } } ActionList["weather"] = func(inputs ...string) botlib.Message { resp := weatherbot.GetDailyReport(viper.GetString("owm_api_key"), viper.GetString("lat"), viper.GetString("long")) return resp } 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() }