diff --git a/testbot/action.go b/testbot/action.go index f5b3eab..1ae8f28 100644 --- a/testbot/action.go +++ b/testbot/action.go @@ -1,5 +1,20 @@ package main +import ( + "log" + + "git.saintnet.tech/stryan/vega/weatherbot" + "github.com/spf13/viper" +) + type Action func(inputs ...string) string 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) + if err != nil { + log.Fatal(err) + } +} diff --git a/testbot/go.mod b/testbot/go.mod index b47234f..3fcd011 100644 --- a/testbot/go.mod +++ b/testbot/go.mod @@ -4,10 +4,15 @@ go 1.14 replace git.saintnet.tech/stryan/vega/botlib => /home/stryan/code/vega/botlib +replace git.saintnet.tech/stryan/vega/weatherbot => /home/stryan/code/vega/weatherbot + 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/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 - github.com/spf13/viper v1.6.3 // indirect + github.com/spf13/viper v1.6.3 golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect maunium.net/go/mautrix v0.1.0-beta.2 ) diff --git a/testbot/go.sum b/testbot/go.sum index f1e5791..4a2ebe4 100644 --- a/testbot/go.sum +++ b/testbot/go.sum @@ -1,5 +1,9 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= git.saintnet.tech/stryan/vega v0.0.0-20200411185307-3aa502fe6aad h1:h4sRW4NzLjrd+zXHj0zmqpO6jtn3BKLxt+42BH3TxEw= +git.saintnet.tech/stryan/vega v0.0.0-20200411212447-da39ba7b18d1 h1:0tW3qN/ViFPkY9lUHmUNuJj8RZ5AK3pAD3lbI/e6Zh8= +git.saintnet.tech/stryan/vega v0.0.0-20200411212447-da39ba7b18d1/go.mod h1:3gGNkjcJk35ZpQu1J5Md+ps1Y9VNyVWHIyCrPYRZ2m4= +git.saintnet.tech/stryan/vega v0.0.0-20200415191842-4fc91fae8f17 h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU= +git.saintnet.tech/stryan/vega v0.0.0-20200415191842-4fc91fae8f17/go.mod h1:3gGNkjcJk35ZpQu1J5Md+ps1Y9VNyVWHIyCrPYRZ2m4= git.saintnet.tech/stryan/vega/botlib v0.0.0-20200411185307-3aa502fe6aad h1:ZwDMH9F1tWA4ijx8nxeXf11/0qxwL2hld+kv4BZgtDI= git.saintnet.tech/stryan/vega/botlib v0.0.0-20200411185307-3aa502fe6aad/go.mod h1:G8KYwuUjGyRIvV61r6l705DpvrLK1UxrCsT0PdOmCOU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -24,9 +28,12 @@ github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8 github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-co-op/gocron v0.1.1 h1:OfDmkqkCguFtFMsm6Eaayci3DADLa8pXvdmOlPU/JcU= +github.com/go-co-op/gocron v0.1.1/go.mod h1:Y9PWlYqDChf2Nbgg7kfS+ZsXHDTZbMZYPEQ0MILqH+M= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-redis/redis v6.15.5+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= @@ -45,6 +52,7 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= @@ -66,6 +74,9 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -104,6 +115,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -118,10 +131,12 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k= @@ -130,8 +145,10 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -151,11 +168,14 @@ google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/testbot/main.go b/testbot/main.go index ec3f8e9..01cc78e 100644 --- a/testbot/main.go +++ b/testbot/main.go @@ -8,17 +8,26 @@ import ( "syscall" "git.saintnet.tech/stryan/vega/botlib" + "github.com/spf13/viper" "maunium.net/go/mautrix" ) func main() { + viper.SetConfigName("config") + viper.AddConfigPath("/etc/testbot") + viper.AddConfigPath(".") + err := viper.ReadInConfig() + if err != nil { + log.Fatalf("Fatal error config file: %v \n", err) + } + viper.SetConfigType("yaml") c := &botlib.Config{ - Userid: "@testbot:saintnet.tech", - Server: "matrix.saintnet.tech", - Token: "MDAxYmxvY2F0aW9uIHNhaW50bmV0LnRlY2gKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDI5Y2lkIHVzZXJfaWQgPSBAdGVzdGJvdDpzYWludG5ldC50ZWNoCjAwMTZjaWQgdHlwZSA9IGFjY2VzcwowMDIxY2lkIG5vbmNlID0gemlKZGs9bmJhcjp1eThIXgowMDJmc2lnbmF0dXJlIE4x0sbUS9lN-fi0KjJmEmpx6_wpYzgvk4k2Eugtkva7Cg", - Name: "TestBot", - Owner: "@stryan:saintnet.tech", - Prefix: "!tb", + Userid: viper.GetString("userid"), + Server: viper.GetString("server"), + Token: viper.GetString("token"), + Name: viper.GetString("name"), + Owner: viper.GetString("owner"), + Prefix: viper.GetString("prefix"), } b := botlib.NewBot(c) tb := &TestBot{ @@ -27,6 +36,7 @@ func main() { Jobs: make(chan *mautrix.Event, 100), } tb.InitActions() + tb.InitScheduler() syncer := botlib.NewCustomSyncer("@testbot:saintnet.tech", b.Client.Store) syncer.OnEventType(mautrix.EventMessage, tb.QueueMessage) syncer.OnEventType(mautrix.StateMember, b.DefaultHandleMember) @@ -44,7 +54,7 @@ func main() { wg.Done() }() log.Println("Syncing enabled") - err := b.LeaveEmptyRooms() + err = b.LeaveEmptyRooms() if err != nil { log.Fatal(err) } diff --git a/testbot/messages.go b/testbot/messages.go index 0aad54d..0658925 100644 --- a/testbot/messages.go +++ b/testbot/messages.go @@ -16,11 +16,18 @@ func (t *TestBot) Message(roomid, message string) error { return err } +func (t *TestBot) TestMessage() { + _, err := t.Bot.Client.SendText(t.Bot.ManagementRoomID, "TestBot Online!") + if err != nil { + log.Fatal(err) + } +} + func (t *TestBot) HandleMessage(e *mautrix.Event) { if e.Sender == t.Conf.Userid { return //we don't care about our own messages } - log.Printf("Handling message:'%v'\n", e.Content.Body) + log.Printf("Handling room %v message:'%v'\n", e.RoomID, e.Content.Body) body := e.Content.Body body_s := strings.Split(body, " ") diff --git a/testbot/testbot.go b/testbot/testbot.go index 0c81ee3..3a09af2 100644 --- a/testbot/testbot.go +++ b/testbot/testbot.go @@ -5,20 +5,30 @@ import ( "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 + Bot *botlib.Bot + Conf *botlib.Config + Jobs chan *mautrix.Event + Scheduler *gocron.Scheduler + schan chan struct{} //for stoping scheduler } func (t *TestBot) Shutdown() { - log.Printf("%v is shutting down", t.Conf.Name) + 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) } @@ -37,13 +47,29 @@ func (t *TestBot) LoadState() { if err != nil { log.Fatal(err) } - t.Bot.ManagementRoomID = strings.Trim(string(dat), "") + 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() } diff --git a/weatherbot/bot.go b/weatherbot/bot.go index c3c6c45..7fa7bcb 100644 --- a/weatherbot/bot.go +++ b/weatherbot/bot.go @@ -8,12 +8,8 @@ import ( "net/http" ) -var apiKey = "7501365e9803ca244bf100fb41084538" -var lat = "39.004608" -var long = "-76.875671" - -func GetDailyReport() string { - oneCallUrl := fmt.Sprintf("https://api.openweathermap.org/data/2.5/onecall?lat=%v&lon=%v&units=imperial&appid=%v", lat, long, apiKey) +func GetDailyReport(apikey, lat, long string) string { + 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) if err != nil {