From 430725c04aa0e827667c97e93a691b574048ecd0 Mon Sep 17 00:00:00 2001 From: ivanik Date: Sun, 24 Jan 2021 15:08:11 +0500 Subject: [PATCH] Vk bridge sample config and code cleanup --- README.md | 2 ++ bridge/vk/vk.go | 22 +++++++++++++++------- matterbridge.toml.sample | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8c631a56..71fec31c 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ And more... - Not supported anymore, see [here](https://github.com/Philipp15b/go-steam/issues/94) for more info. - [Telegram](https://telegram.org) - [Twitch](https://twitch.tv) +- [VK](https://vk.com/) - [WhatsApp](https://www.whatsapp.com/) - [XMPP](https://xmpp.org) - [Zulip](https://zulipchat.com) @@ -348,6 +349,7 @@ Matterbridge wouldn't exist without these libraries: - steam - - telegram - - tengo - +- vk - - whatsapp - - xmpp - - zulip - diff --git a/bridge/vk/vk.go b/bridge/vk/vk.go index 3ed3cf89..d624e21e 100644 --- a/bridge/vk/vk.go +++ b/bridge/vk/vk.go @@ -24,7 +24,7 @@ type user struct { type Bvk struct { c *api.VK - usernamesMap map[int]user + usernamesMap map[int]user // cache of user names and avatar URLs *bridge.Config } @@ -37,7 +37,7 @@ func (b *Bvk) Connect() error { b.c = api.NewVK(b.GetString("Token")) lp, err := longpoll.NewLongPoll(b.c, b.GetInt("GroupID")) if err != nil { - b.Log.Error(err) + b.Log.Debugf("%#v", err) return err } @@ -45,6 +45,8 @@ func (b *Bvk) Connect() error { b.handleMessage(obj.Message, false) }) + b.Log.Info("Connection succeeded") + go lp.Run() return nil @@ -59,9 +61,9 @@ func (b *Bvk) JoinChannel(channel config.ChannelInfo) error { } func (b *Bvk) Send(msg config.Message) (string, error) { - b.Log.Debug(msg.Text) + b.Log.Debugf("=> Receiving %#v", msg) - peerId, err := strconv.ParseInt(msg.Channel, 10, 64) + peerID, err := strconv.ParseInt(msg.Channel, 10, 64) if err != nil { return "", err } @@ -73,13 +75,14 @@ func (b *Bvk) Send(msg config.Message) (string, error) { } params := api.Params{ - "peer_id": peerId, + "peer_id": peerID, "message": text, "random_id": time.Now().Unix(), } if msg.Extra != nil { if len(msg.Extra["file"]) > 0 { + // generate attachments string var attachments []string for _, f := range msg.Extra["file"] { @@ -87,7 +90,7 @@ func (b *Bvk) Send(msg config.Message) (string, error) { photoRE := regexp.MustCompile(".(jpg|jpe|png)$") if photoRE.MatchString(fi.Name) { r := bytes.NewReader(*fi.Data) - photo, err := b.c.UploadMessagesPhoto(int(peerId), r) + photo, err := b.c.UploadMessagesPhoto(int(peerID), r) if err != nil { b.Log.Error("Failad uploading photo") b.Log.Error(err) @@ -107,7 +110,7 @@ func (b *Bvk) Send(msg config.Message) (string, error) { doctype = "doc" } - doc, err := b.c.UploadMessagesDoc(int(peerId), doctype, fi.Name, "", r) + doc, err := b.c.UploadMessagesDoc(int(peerID), doctype, fi.Name, "", r) if err != nil { b.Log.Error("Failad uploading file") b.Log.Error(err) @@ -153,6 +156,7 @@ func (b *Bvk) getUser(id int) user { func (b *Bvk) handleMessage(msg object.MessagesMessage, isFwd bool) { b.Log.Debug("ChatID: ", msg.PeerID) + // fetch user info u := b.getUser(msg.FromID) rmsg := config.Message{ @@ -178,6 +182,7 @@ func (b *Bvk) handleMessage(msg object.MessagesMessage, isFwd bool) { if len(msg.Attachments) > 0 { var urls []string + // get URLs for attachments for _, a := range msg.Attachments { if a.Type == "photo" { var resolution float64 = 0 @@ -220,8 +225,10 @@ func (b *Bvk) handleMessage(msg object.MessagesMessage, isFwd bool) { } if b.GetBool("UseFileURL") { + // add url to message text rmsg.Text += "\n" + strings.Join(urls, "\n") } else { + // download for _, url := range urls { data, err := helper.DownloadFile(url) if err == nil { @@ -240,6 +247,7 @@ func (b *Bvk) handleMessage(msg object.MessagesMessage, isFwd bool) { b.Remote <- rmsg if len(msg.FwdMessages) > 0 { + // recursive processing of forwarded messages for _, m := range msg.FwdMessages { m.PeerID = msg.PeerID b.handleMessage(m, true) diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index a398961a..92db8aee 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1486,6 +1486,21 @@ TLSCACertificate=mumble-ca.crt # OPTIONAL (default false) SkipTLSVerify=false +################################################################### +#VK +################################################################### +[vk.myvk] +#Group access token +#See https://vk.com/dev/bots_docs?f=1.1.%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D0%BA%D0%BB%D1%8E%D1%87%D0%B0%20%D0%B4%D0%BE%D1%81%D1%82%D1%83%D0%BF%D0%B0 +Token="Yourtokenhere" + +#Group ID +#For example in URL https://vk.com/public168963511 group ID is 168963511 +GroupID=123456789 + +#If enabled this will relay attachments as URLs +UseFileURL=false + ################################################################### # # WhatsApp @@ -1843,6 +1858,8 @@ enable=true # ------------------------------------------------------------------------------------------------------------------------------------- # telegram | chatid | -123456789 | A large negative number. see https://www.linkedin.com/pulse/telegram-bots-beginners-marco-frau # ------------------------------------------------------------------------------------------------------------------------------------- + # vk | peerid | 2000000002 | A number that starts form 2000000000. Use --debug and send any message in chat to get PeerID in the logs + # ------------------------------------------------------------------------------------------------------------------------------------- # whatsapp | group JID | 48111222333-123455678999@g.us | A unique group JID. If you specify an empty string, bridge will list all the possibilities # | "Group Name" | "Family Chat" | if you specify a group name, the bridge will find hint the JID to specify. Names can change over time and are not stable. # -------------------------------------------------------------------------------------------------------------------------------------