diff --git a/testbot/action.go b/testbot/action.go index 1ae8f28..36e13ce 100644 --- a/testbot/action.go +++ b/testbot/action.go @@ -3,17 +3,18 @@ package main import ( "log" + "git.saintnet.tech/stryan/vega/botlib" "git.saintnet.tech/stryan/vega/weatherbot" "github.com/spf13/viper" ) -type Action func(inputs ...string) string +type Action func(inputs ...string) botlib.Message var ActionList map[string]Action func (t *TestBot) WeatherReport() { report := weatherbot.GetDailyReport(viper.GetString("owm_api_key"), viper.GetString("lat"), viper.GetString("long")) - _, err := t.Bot.Client.SendText(t.Bot.ManagementRoomID, report) + _, err := t.Bot.Client.SendText(t.Bot.ManagementRoomID, report.Body) if err != nil { log.Fatal(err) } diff --git a/testbot/go.mod b/testbot/go.mod index 3fcd011..4adc388 100644 --- a/testbot/go.mod +++ b/testbot/go.mod @@ -8,7 +8,7 @@ replace git.saintnet.tech/stryan/vega/weatherbot => /home/stryan/code/vega/weath require ( git.saintnet.tech/stryan/vega v0.0.0-20200415191842-4fc91fae8f17 - git.saintnet.tech/stryan/vega/botlib v0.0.0-20200411185307-3aa502fe6aad + git.saintnet.tech/stryan/vega/botlib v0.0.0-20200421201809-03a5e56614c1 git.saintnet.tech/stryan/vega/weatherbot v0.0.0-00010101000000-000000000000 github.com/go-co-op/gocron v0.1.1 github.com/prologic/go-gopher v0.0.0-20191226035442-664dbdb49f44 // indirect diff --git a/testbot/messages.go b/testbot/messages.go index 0658925..1649685 100644 --- a/testbot/messages.go +++ b/testbot/messages.go @@ -4,6 +4,7 @@ import ( "log" "strings" + "git.saintnet.tech/stryan/vega/botlib" "maunium.net/go/mautrix" ) @@ -47,10 +48,17 @@ func (t *TestBot) HandleMessage(e *mautrix.Event) { } response, found := ActionList[body_s[1]] if !found { - response = func(inputs ...string) string { return "Command not found" } + response = func(inputs ...string) botlib.Message { + return botlib.Message{ + Type: botlib.MessageResponse, + Sender: t.Conf.Name, + Receiver: t.Conf.Name, + Body: "Command not found", + } + } } msg := response(body_s[1:]...) - err := t.Message(e.RoomID, msg) + err := t.Message(e.RoomID, msg.Body) if err != nil { log.Fatal(err) } diff --git a/testbot/testbot.go b/testbot/testbot.go index 12df0d2..8df517a 100644 --- a/testbot/testbot.go +++ b/testbot/testbot.go @@ -54,12 +54,27 @@ func (t *TestBot) LoadState() { 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["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["weather"] = func(inputs ...string) string { - return weatherbot.GetDailyReport(viper.GetString("owm_api_key"), viper.GetString("lat"), viper.GetString("long")) + 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 { diff --git a/weatherbot/bot.go b/weatherbot/bot.go index 7fa7bcb..dcda15b 100644 --- a/weatherbot/bot.go +++ b/weatherbot/bot.go @@ -6,9 +6,11 @@ import ( "io/ioutil" "log" "net/http" + + "git.saintnet.tech/stryan/vega/botlib" ) -func GetDailyReport(apikey, lat, long string) string { +func GetDailyReport(apikey, lat, long string) botlib.Message { oneCallUrl := fmt.Sprintf("https://api.openweathermap.org/data/2.5/onecall?lat=%v&lon=%v&units=imperial&appid=%v", lat, long, apikey) response, err := http.Get(oneCallUrl) @@ -22,5 +24,10 @@ func GetDailyReport(apikey, lat, long string) string { if err != nil { log.Fatal(err) } - return TodayToReport(report.Daily[0]) + return botlib.Message{ + Type: botlib.MessageResponse, + Sender: "WeatherBot", + Receiver: "Unknown", + Body: TodayToReport(report.Daily[0]), + } } diff --git a/weatherbot/go.mod b/weatherbot/go.mod index 03b2b05..aee19cd 100644 --- a/weatherbot/go.mod +++ b/weatherbot/go.mod @@ -1,3 +1,5 @@ module git.saintnet.tech/stryan/vega/weatherbot go 1.14 + +require git.saintnet.tech/stryan/vega/botlib v0.0.0-20200421201809-03a5e56614c1