use botlib.Message internally

This commit is contained in:
stryan 2020-04-21 16:42:39 -04:00
parent 03a5e56614
commit 8fc88fbb64
6 changed files with 45 additions and 12 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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 {

View File

@ -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]),
}
}

View File

@ -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