From ac2b7301fafd51b820241edd28272b207cf5eb48 Mon Sep 17 00:00:00 2001 From: ivanik Date: Fri, 22 Jan 2021 21:23:21 +0500 Subject: [PATCH] Add vk bridge --- bridge/vk/vk.go | 116 +++++++++++++++++++++++++++++++++++++++ gateway/bridgemap/bvk.go | 11 ++++ go.mod | 1 + go.sum | 2 + 4 files changed, 130 insertions(+) create mode 100644 bridge/vk/vk.go create mode 100644 gateway/bridgemap/bvk.go diff --git a/bridge/vk/vk.go b/bridge/vk/vk.go new file mode 100644 index 00000000..1d6f1742 --- /dev/null +++ b/bridge/vk/vk.go @@ -0,0 +1,116 @@ +package bvk + +import ( + "context" + "strconv" + "time" + + "github.com/42wim/matterbridge/bridge" + "github.com/42wim/matterbridge/bridge/config" + + "github.com/SevereCloud/vksdk/v2/api" + "github.com/SevereCloud/vksdk/v2/events" + longpoll "github.com/SevereCloud/vksdk/v2/longpoll-bot" +) + +type user struct { + lastname, firstname, avatar string +} + +type Bvk struct { + c *api.VK + usernamesMap map[int]user + *bridge.Config +} + +func New(cfg *bridge.Config) bridge.Bridger { + return &Bvk{usernamesMap: make(map[int]user), Config: cfg} +} + +func (b *Bvk) Connect() error { + b.Log.Info("Connecting") + b.c = api.NewVK(b.GetString("Token")) + lp, err := longpoll.NewLongPoll(b.c, b.GetInt("GroupID")) + if err != nil { + b.Log.Error(err) + return err + } + + lp.MessageNew(b.handleMessage) + + go lp.Run() + + return nil +} + +func (b *Bvk) Disconnect() error { + return nil +} + +func (b *Bvk) JoinChannel(channel config.ChannelInfo) error { + return nil +} + +func (b *Bvk) Send(msg config.Message) (string, error) { + b.Log.Debug(msg.Text) + + peerId, err := strconv.ParseInt(msg.Channel, 10, 64) + if err != nil { + return "", err + } + + if msg.Text != "" { + text := msg.Username + msg.Text + + res, err := b.c.MessagesSend(api.Params{ + "peer_id": peerId, + "message": text, + "random_id": time.Now().Unix(), + }) + + if err != nil { + return "", err + } + + return string(res), nil + } + + return "", nil +} + +func (b *Bvk) getUser(id int) user { + u, found := b.usernamesMap[id] + if !found { + b.Log.Debug("Fetching username for ", id) + + result, _ := b.c.UsersGet(api.Params{ + "user_ids": id, + "fields": "photo_200", + }) + + resUser := result[0] + u = user{lastname: resUser.LastName, firstname: resUser.FirstName, avatar: resUser.Photo200} + b.usernamesMap[id] = u + } + + return u +} + +func (b *Bvk) handleMessage(ctx context.Context, obj events.MessageNewObject) { + msg := obj.Message + b.Log.Debug("ChatID: ", msg.PeerID) + u := b.getUser(msg.FromID) + + rmsg := config.Message{ + Event: config.EventUserAction, + Text: msg.Text, + Username: u.firstname + " " + u.lastname, + Avatar: u.avatar, + Channel: strconv.Itoa(msg.PeerID), + Account: b.Account, + UserID: strconv.Itoa(msg.FromID), + ID: strconv.Itoa(msg.ConversationMessageID), + } + + b.Remote <- rmsg +} diff --git a/gateway/bridgemap/bvk.go b/gateway/bridgemap/bvk.go new file mode 100644 index 00000000..ea3de19e --- /dev/null +++ b/gateway/bridgemap/bvk.go @@ -0,0 +1,11 @@ +// +build !novk + +package bridgemap + +import ( + bvk "github.com/42wim/matterbridge/bridge/vk" +) + +func init() { + FullMap["vk"] = bvk.New +} diff --git a/go.mod b/go.mod index a4c9cd92..2aa6546f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/Philipp15b/go-steam v1.0.1-0.20200727090957-6ae9b3c0a560 github.com/Rhymen/go-whatsapp v0.1.2-0.20201226125722-8029c28f5c5a github.com/RocketChat/Rocket.Chat.Go.SDK v0.0.0-20200922220614-e4a51dfb52e4 // indirect + github.com/SevereCloud/vksdk/v2 v2.9.0 github.com/d5/tengo/v2 v2.6.2 github.com/davecgh/go-spew v1.1.1 github.com/fsnotify/fsnotify v1.4.9 diff --git a/go.sum b/go.sum index 3a240988..b729ac27 100644 --- a/go.sum +++ b/go.sum @@ -87,6 +87,8 @@ github.com/RoaringBitmap/roaring v0.4.23/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06 github.com/RoaringBitmap/roaring v0.5.1/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06Mq5mKs52e1TwOo= github.com/RocketChat/Rocket.Chat.Go.SDK v0.0.0-20200922220614-e4a51dfb52e4 h1:u7UvmSK6McEMXFZB310/YZ6uvfDaSFrSoqWoy/qaOW0= github.com/RocketChat/Rocket.Chat.Go.SDK v0.0.0-20200922220614-e4a51dfb52e4/go.mod h1:wVff6N8s2foRPCYeynerOM/FF44uyI60/HMiboL0SXw= +github.com/SevereCloud/vksdk/v2 v2.9.0 h1:39qjzmozK5FDfnDkfA+YN0CtKi4mDrzjPtoT5GN9Xg0= +github.com/SevereCloud/vksdk/v2 v2.9.0/go.mod h1:IBmfJ3rs+zDLD9NHCoJEpgg5A4UOoxgUU/g8p5lYb48= github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=