173 lines
3.8 KiB
Go
173 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/matrix-org/gomatrix"
|
|
log "github.com/sirupsen/logrus"
|
|
flag "github.com/spf13/pflag"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
Homeserver, User, Password string
|
|
)
|
|
|
|
type Actions struct {
|
|
Type string
|
|
}
|
|
|
|
type Subscription struct {
|
|
Echoes []string `json:"echoes"`
|
|
}
|
|
|
|
// getMessages ...
|
|
func (a Actions) getMessages() string {
|
|
m := strings.Join([]string{"From: Difrex", "Address: dynamic,4"}, "\n")
|
|
return m
|
|
}
|
|
|
|
// init ...
|
|
func init() {
|
|
flag.StringVarP(&Homeserver, "homeserver", "h", "https://matrix.lessmore.pw", "The base homeserver URL")
|
|
flag.StringVarP(&User, "user", "u", "@idec-bot:matrix.lessmore.pw", "User ID")
|
|
flag.StringVarP(&Password, "password", "p", "", "User password")
|
|
flag.Parse()
|
|
}
|
|
|
|
type Dialog struct {
|
|
RoomID string `json:"room_id"`
|
|
Cli *gomatrix.Client
|
|
}
|
|
|
|
// startDialog ...
|
|
func (d Dialog) startDialog() {
|
|
room := d.RoomID
|
|
|
|
log.Print("Join in ", room)
|
|
_, err := d.Cli.JoinRoom(room, Homeserver, nil)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
|
|
var ID []string
|
|
for {
|
|
msg, err := d.Cli.Messages(room, "", "", 'b', 1)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
for _, chunk := range msg.Chunk {
|
|
if !inSlice(ID, chunk.ID) && chunk.Sender != User {
|
|
log.Print("[MESSAGE] From ", chunk.Sender)
|
|
if _, ok := chunk.Content["body"]; ok {
|
|
log.Print("[MESSAGE] Body: ", chunk.Content["body"].(string))
|
|
|
|
// Main methods
|
|
switch method := chunk.Content["body"].(string); method {
|
|
case "list":
|
|
d.Cli.SendText(room, "ii.14")
|
|
ID = append(ID, chunk.ID)
|
|
time.Sleep(1 * time.Second)
|
|
case "help":
|
|
d.Cli.SendText(room, "Помощь уже в пути!")
|
|
ID = append(ID, chunk.ID)
|
|
time.Sleep(1 * time.Second)
|
|
case "помощь":
|
|
d.Cli.SendText(room, "Помощь уже в пути!")
|
|
ID = append(ID, chunk.ID)
|
|
time.Sleep(1 * time.Second)
|
|
case "пока":
|
|
d.Cli.SendText(room, "T_T")
|
|
d.Cli.LeaveRoom(room)
|
|
return
|
|
default:
|
|
d.Cli.SendText(room, `Хмм... Попробуй "помощь"/"help"`)
|
|
ID = append(ID, chunk.ID)
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
|
|
// main ...
|
|
func main() {
|
|
cli, err := gomatrix.NewClient(Homeserver, "", "")
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
|
|
cli.Client = http.DefaultClient
|
|
|
|
customStore := gomatrix.NewInMemoryStore()
|
|
cli.Store = customStore
|
|
|
|
resp, err := cli.Login(&gomatrix.ReqLogin{
|
|
Type: "m.login.password",
|
|
User: User,
|
|
Password: Password,
|
|
})
|
|
|
|
syncer := cli.Syncer.(*gomatrix.DefaultSyncer)
|
|
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
cli.SetCredentials(resp.UserID, resp.AccessToken)
|
|
|
|
s, err := cli.JoinRoom("#ii.test.14:matrix.lessmore.pw", "matrix.lessmore.pw", "")
|
|
if err != nil {
|
|
log.Fatal(err.Error(), s)
|
|
log.Print(s.RoomID)
|
|
}
|
|
|
|
cli.SetDisplayName("IDEC Bot")
|
|
|
|
go func(cli *gomatrix.Client, syncer *gomatrix.DefaultSyncer) {
|
|
var eventID []string
|
|
var roomID []string
|
|
var event *gomatrix.Event
|
|
for {
|
|
syncer.OnEventType("m.room.member", func(ev *gomatrix.Event) {
|
|
if !inSlice(eventID, ev.ID) {
|
|
roomID = append(roomID, ev.RoomID)
|
|
eventID = append(eventID, ev.ID)
|
|
event = ev
|
|
|
|
log.Print("Invited to ", ev.RoomID)
|
|
|
|
log.Print("Start dialog")
|
|
go Dialog{
|
|
RoomID: ev.RoomID,
|
|
Cli: cli,
|
|
}.startDialog()
|
|
return
|
|
}
|
|
})
|
|
time.Sleep(250 * time.Millisecond)
|
|
}
|
|
}(cli, syncer)
|
|
|
|
for {
|
|
if err := cli.Sync(); err != nil {
|
|
log.Error("Sync() returned ", err.Error())
|
|
}
|
|
time.Sleep(250 * time.Millisecond)
|
|
}
|
|
// Optional: Wait a period of time before trying to sync again.
|
|
}
|
|
|
|
// notInSlice return true if string founded in slice
|
|
func inSlice(s []string, k string) bool {
|
|
for _, i := range s {
|
|
if i == k {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|