mirror of
https://github.com/pinpox/gitea-matrix-bot
synced 2024-11-22 19:31:58 +01:00
Bot follows invites and can respond to some commands
This commit is contained in:
parent
88a5996249
commit
46ddf1ba06
195
bot.go
Normal file
195
bot.go
Normal file
@ -0,0 +1,195 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/matrix-org/gomatrix"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//GiteaBot struct to hold the bot and it's methods
|
||||
type GiteaBot struct {
|
||||
//Map a repository to matrix rooms
|
||||
Subscriptions map[string][]string
|
||||
Client *gomatrix.Client
|
||||
matrixPass string
|
||||
matrixUser string
|
||||
}
|
||||
|
||||
func (gb *GiteaBot) handleCommands(message, room, sender string) {
|
||||
|
||||
//Don't do anything if the sender is the bot itself
|
||||
//TODO edge-case: bot has the same name as a user but on a different server
|
||||
if strings.Contains(sender, gb.matrixUser) {
|
||||
return
|
||||
}
|
||||
|
||||
//Admin commands
|
||||
if gb.isAdmin(sender) {
|
||||
|
||||
//Add a subsription
|
||||
rAddSub, _ := regexp.Compile("!sub")
|
||||
if rAddSub.MatchString(message) {
|
||||
repo := message[4:]
|
||||
fmt.Println("You want " + room + " to subscribe to" + repo)
|
||||
gb.handleCommandAddSub(room, repo)
|
||||
return
|
||||
}
|
||||
|
||||
//Remove a subscription
|
||||
rRemoveSub, _ := regexp.Compile("!unsub .*")
|
||||
if rRemoveSub.MatchString(message) {
|
||||
repo := message[6:]
|
||||
fmt.Println("You want " + room + " to un-subscribe from " + repo)
|
||||
gb.handleCommandRemoveSub(room, repo)
|
||||
return
|
||||
}
|
||||
|
||||
//List room subs
|
||||
rListSubs, _ := regexp.Compile("!listsubs")
|
||||
if rListSubs.MatchString(message) {
|
||||
fmt.Println("You want to list subs")
|
||||
gb.handleCommandListSubs(room)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//Non-Admin commands
|
||||
|
||||
//Get help
|
||||
rHelp, _ := regexp.Compile("!help")
|
||||
if rHelp.MatchString(message) {
|
||||
fmt.Println("You want to get help")
|
||||
gb.handleCommandHelp(room)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (gb GiteaBot) isAdmin(user string) bool {
|
||||
//TODO check if admin
|
||||
//Check for power in room
|
||||
return true
|
||||
}
|
||||
|
||||
func (gb *GiteaBot) handleCommandListSubs(room string) {
|
||||
gb.SendToRoom(room, "Not implemented yet")
|
||||
}
|
||||
|
||||
func (gb *GiteaBot) handleCommandAddSub(room, repo string) {
|
||||
gb.SendToRoom(room, "Not implemented yet")
|
||||
}
|
||||
|
||||
func (gb *GiteaBot) handleCommandMakeAdmin(room, repo string) {
|
||||
gb.SendToRoom(room, "Not implemented yet")
|
||||
}
|
||||
|
||||
func (gb *GiteaBot) handleCommandRemoveSub(room, repo string) {
|
||||
gb.SendToRoom(room, "Not implemented yet")
|
||||
}
|
||||
|
||||
func (gb *GiteaBot) handleCommandHelp(room string) {
|
||||
helpMsg := `
|
||||
|
||||
I'm your friendly Gitea Bot!
|
||||
|
||||
You can invite me to any matrix room to get updates on subscribed gitea repositorys.
|
||||
The following commands are avaitible:
|
||||
|
||||
!sub user/repo Subscribe to a repository
|
||||
!unsub user/repo Remove subscription to a repository
|
||||
!listsubs List the room's subscriptions
|
||||
!help Display this message
|
||||
|
||||
Some of the commands might require admin powers!
|
||||
|
||||
`
|
||||
gb.SendToRoom(room, helpMsg)
|
||||
}
|
||||
|
||||
func (gb *GiteaBot) login() {
|
||||
|
||||
}
|
||||
|
||||
//SendToRoom sends a message to a specified room
|
||||
func (gb *GiteaBot) SendToRoom(room, message string) {
|
||||
_, err = gb.Client.SendText(room, message)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
//SendMessageToRooms sends a message to all romes that have subscribed to the repo
|
||||
func (gb *GiteaBot) SendMessageToRooms(repo, message string) {
|
||||
for _, v := range gb.Subscriptions[repo] {
|
||||
_, err = gb.Client.SendText(v, message)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//NewGiteaBot creates a new bot form user credentials
|
||||
func NewGiteaBot(user, pass string) (*GiteaBot, error) {
|
||||
|
||||
fmt.Println("Logging in")
|
||||
|
||||
cli, _ := gomatrix.NewClient("http://matrix.org", "", "")
|
||||
|
||||
resp, err := cli.Login(&gomatrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
User: user,
|
||||
Password: pass,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cli.SetCredentials(resp.UserID, resp.AccessToken)
|
||||
|
||||
bot := &GiteaBot{
|
||||
matrixPass: pass,
|
||||
matrixUser: user,
|
||||
Subscriptions: make(map[string][]string),
|
||||
Client: cli,
|
||||
}
|
||||
|
||||
//Setup Syncer and to handle events
|
||||
syncer := cli.Syncer.(*gomatrix.DefaultSyncer)
|
||||
|
||||
//Handle messages send to the channel
|
||||
syncer.OnEventType("m.room.message", func(ev *gomatrix.Event) {
|
||||
// fmt.Println("\nMessage: ", ev)
|
||||
fmt.Println(ev.Sender + " said: \"" + ev.Content["body"].(string) + "\" in room : " + ev.RoomID)
|
||||
bot.handleCommands(ev.Content["body"].(string), ev.RoomID, ev.Sender)
|
||||
|
||||
})
|
||||
|
||||
//Handle member events (kick, invite)
|
||||
syncer.OnEventType("m.room.member", func(ev *gomatrix.Event) {
|
||||
fmt.Println(ev.Sender + " invited bot to " + ev.RoomID)
|
||||
|
||||
if ev.Content["membership"] == "invite" {
|
||||
|
||||
fmt.Println("Joining Room")
|
||||
|
||||
if resp, err := cli.JoinRoom(ev.RoomID, "", nil); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
fmt.Println(resp.RoomID)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
//Spawn goroutine to keep checking for events
|
||||
go func() {
|
||||
for {
|
||||
if err := cli.Sync(); err != nil {
|
||||
fmt.Println("Sync() returned ", err)
|
||||
}
|
||||
// Optional: Wait a period of time before trying to sync again.
|
||||
}
|
||||
}()
|
||||
|
||||
return bot, nil
|
||||
}
|
@ -49,7 +49,9 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("=================================================")
|
||||
fmt.Println(string(body))
|
||||
fmt.Println("=================================================")
|
||||
fmt.Println(generateMessage(postData, r.Header.Get("X-Gitea-Event")))
|
||||
message := generateMessage(postData, r.Header.Get("X-Gitea-Event"))
|
||||
bot.SendMessageToRooms(postData.Repository.Name, message)
|
||||
|
||||
fmt.Println("=================================================")
|
||||
fmt.Fprint(w, "POST done")
|
||||
} else {
|
||||
|
54
main.go
54
main.go
@ -4,66 +4,36 @@ import (
|
||||
"fmt"
|
||||
"github.com/go-ini/ini"
|
||||
"os"
|
||||
|
||||
_ "github.com/matrix-org/gomatrix"
|
||||
)
|
||||
|
||||
// import "net/http"
|
||||
|
||||
var cfg *ini.File
|
||||
var err error
|
||||
var bot *GiteaBot
|
||||
|
||||
func init() {
|
||||
|
||||
//Lonad config
|
||||
cfg, err = ini.Load("config.ini")
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Fail to read file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
matrixUser := cfg.Section("matrix").Key("matrix_user").String()
|
||||
matrixPass := cfg.Section("matrix").Key("matrix_pass").String()
|
||||
// botDB := cfg.Section("bot").Key("").String()
|
||||
|
||||
fmt.Println("Creating Bot")
|
||||
bot, err = NewGiteaBot(matrixUser, matrixPass)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
//TODO Check if already logged in
|
||||
//TODO Check if In room
|
||||
//TODO Send
|
||||
|
||||
// fmt.Println("Logging in")
|
||||
|
||||
// cli, _ := gomatrix.NewClient("http://matrix.org", "", "")
|
||||
// resp, err := cli.Login(&gomatrix.ReqLogin{
|
||||
// Type: "m.login.password",
|
||||
// User: matrixUser,
|
||||
// Password: matrixPass,
|
||||
// })
|
||||
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// cli.SetCredentials(resp.UserID, resp.AccessToken)
|
||||
|
||||
// fmt.Println("Joining Room")
|
||||
|
||||
// roomID := ""
|
||||
|
||||
// if resp, err := cli.JoinRoom("#test-reminder-bot:matrix.org", "", nil); err != nil {
|
||||
// panic(err)
|
||||
// } else {
|
||||
// roomID = resp.RoomID
|
||||
// }
|
||||
// fmt.Println("Sending a message")
|
||||
// _, err = cli.SendText(roomID, "test message 0")
|
||||
// TODO maybe notice is better?
|
||||
// func (cli *Client) SendNotice(roomID, text string) (*RespSendEvent, error)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
|
||||
// }
|
||||
|
||||
fmt.Println("Setting up POST-listener")
|
||||
setupListener()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user