package main import ( "fmt" "log" "strings" "time" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" ) //GitTag is current git tag var GitTag string //GitCommit is current git commit var GitCommit string func main() { conf := loadConfig("config.yaml") matrixClient := newMatrixClient(conf) //redditClient := newRedditClient(conf) syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer) syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) { if evt.Sender == matrixClient.UserID { return //ignore events from self } fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body) body := evt.Content.AsMessage().Body bodyS := strings.Split(body, " ") if bodyS[0] != "!nun" { return } if len(bodyS) < 2 { return //nothing to parse } switch bodyS[1] { case "version": // print version if GitTag != "" { matrixClient.SendText(evt.RoomID, "NunBot version "+GitTag) } else { matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit) } case "help": matrixClient.SendText(evt.RoomID, "Supported commands: version,stats") default: //command not found matrixClient.SendText(evt.RoomID, "command not recognized") } }) syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) { fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body) if evt.Content.AsMember().Membership.IsInviteOrJoin() { _, err := matrixClient.JoinRoomByID(evt.RoomID) if err != nil { fmt.Printf("error joining room %v", evt.RoomID) } else { fmt.Printf("joined room %v", evt.RoomID) } } }) var curPost post go func() { for { time.Sleep(30 * time.Second) newPost := getNewestPost("LittleNuns") if curPost.Title != newPost.Title { curPost = newPost roomResp, err := matrixClient.JoinedRooms() if err != nil { log.Printf("error getting joined rooms: %v", err) continue } rooms := roomResp.JoinedRooms for _, room := range rooms { matrixClient.SendText(room, fmt.Sprintf("%v\n%v", curPost.Title, curPost.Link)) } } } }() err := matrixClient.Sync() if err != nil { panic(err) } }