Vk bridge attachments
This commit is contained in:
146
bridge/vk/vk.go
146
bridge/vk/vk.go
@@ -1,12 +1,16 @@
|
||||
package bvk
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/42wim/matterbridge/bridge"
|
||||
"github.com/42wim/matterbridge/bridge/config"
|
||||
"github.com/42wim/matterbridge/bridge/helper"
|
||||
|
||||
"github.com/SevereCloud/vksdk/v2/api"
|
||||
"github.com/SevereCloud/vksdk/v2/events"
|
||||
@@ -59,23 +63,71 @@ func (b *Bvk) Send(msg config.Message) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
text := msg.Username
|
||||
|
||||
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
|
||||
text += msg.Text
|
||||
}
|
||||
|
||||
return "", nil
|
||||
params := api.Params{
|
||||
"peer_id": peerId,
|
||||
"message": text,
|
||||
"random_id": time.Now().Unix(),
|
||||
}
|
||||
|
||||
if msg.Extra != nil {
|
||||
if len(msg.Extra["file"]) > 0 {
|
||||
var attachments []string
|
||||
|
||||
for _, f := range msg.Extra["file"] {
|
||||
fi := f.(config.FileInfo)
|
||||
photoRE := regexp.MustCompile(".(jpg|jpe|png)$")
|
||||
if photoRE.MatchString(fi.Name) {
|
||||
r := bytes.NewReader(*fi.Data)
|
||||
photo, err := b.c.UploadMessagesPhoto(int(peerId), r)
|
||||
if err != nil {
|
||||
b.Log.Error("Failad uploading photo")
|
||||
b.Log.Error(err)
|
||||
} else {
|
||||
attachments = append(attachments, "photo"+strconv.Itoa(photo[0].OwnerID)+"_"+strconv.Itoa(photo[0].ID))
|
||||
if fi.Comment != "" {
|
||||
text += fi.Comment + "\n"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
r := bytes.NewReader(*fi.Data)
|
||||
|
||||
var doctype string
|
||||
if strings.Contains(fi.Name, ".ogg") {
|
||||
doctype = "audio_message"
|
||||
} else {
|
||||
doctype = "doc"
|
||||
}
|
||||
|
||||
doc, err := b.c.UploadMessagesDoc(int(peerId), doctype, fi.Name, "", r)
|
||||
if err != nil {
|
||||
b.Log.Error("Failad uploading file")
|
||||
b.Log.Error(err)
|
||||
} else if doc.Type == "audio_message" {
|
||||
attachments = append(attachments, "doc"+strconv.Itoa(doc.AudioMessage.OwnerID)+"_"+strconv.Itoa(doc.AudioMessage.ID))
|
||||
} else if doc.Type == "doc" {
|
||||
attachments = append(attachments, "doc"+strconv.Itoa(doc.Doc.OwnerID)+"_"+strconv.Itoa(doc.Doc.ID))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
params["attachment"] = strings.Join(attachments, ",")
|
||||
params["message"] = text
|
||||
}
|
||||
}
|
||||
|
||||
res, err := b.c.MessagesSend(params)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(res), nil
|
||||
}
|
||||
|
||||
func (b *Bvk) getUser(id int) user {
|
||||
@@ -102,7 +154,6 @@ func (b *Bvk) handleMessage(ctx context.Context, obj events.MessageNewObject) {
|
||||
u := b.getUser(msg.FromID)
|
||||
|
||||
rmsg := config.Message{
|
||||
Event: config.EventUserAction,
|
||||
Text: msg.Text,
|
||||
Username: u.firstname + " " + u.lastname,
|
||||
Avatar: u.avatar,
|
||||
@@ -110,6 +161,71 @@ func (b *Bvk) handleMessage(ctx context.Context, obj events.MessageNewObject) {
|
||||
Account: b.Account,
|
||||
UserID: strconv.Itoa(msg.FromID),
|
||||
ID: strconv.Itoa(msg.ConversationMessageID),
|
||||
Extra: make(map[string][]interface{}),
|
||||
}
|
||||
|
||||
if msg.ReplyMessage != nil {
|
||||
ur := b.getUser(msg.ReplyMessage.FromID)
|
||||
rmsg.Text = "Re: " + ur.firstname + " " + ur.lastname + "\n" + rmsg.Text
|
||||
}
|
||||
|
||||
if len(msg.Attachments) > 0 {
|
||||
var urls []string
|
||||
|
||||
for _, a := range msg.Attachments {
|
||||
if a.Type == "photo" {
|
||||
var resolution float64 = 0
|
||||
url := a.Photo.Sizes[0].URL
|
||||
for _, size := range a.Photo.Sizes {
|
||||
r := size.Height * size.Width
|
||||
if resolution < r {
|
||||
resolution = r
|
||||
url = size.URL
|
||||
}
|
||||
}
|
||||
|
||||
urls = append(urls, url)
|
||||
} else if a.Type == "doc" {
|
||||
urls = append(urls, a.Doc.URL)
|
||||
} else if a.Type == "graffiti" {
|
||||
// Untested
|
||||
urls = append(urls, a.Graffiti.URL)
|
||||
} else if a.Type == "audio_message" {
|
||||
urls = append(urls, a.AudioMessage.DocsDocPreviewAudioMessage.LinkOgg)
|
||||
} else if a.Type == "sticker" {
|
||||
var resolution float64 = 0
|
||||
url := a.Sticker.Images[0].URL
|
||||
for _, size := range a.Sticker.Images {
|
||||
r := size.Height * size.Width
|
||||
if resolution < r {
|
||||
resolution = r
|
||||
url = size.URL
|
||||
}
|
||||
}
|
||||
urls = append(urls, url+".png")
|
||||
} else if a.Type == "doc" {
|
||||
urls = append(urls, a.Doc.URL)
|
||||
} else if a.Type == "video" {
|
||||
rmsg.Text += "https://vk.com/video" + strconv.Itoa(a.Video.OwnerID) + "_" + strconv.Itoa(a.Video.ID)
|
||||
} else if a.Type == "wall" {
|
||||
rmsg.Text += "https://vk.com/wall" + strconv.Itoa(a.Wall.FromID) + "_" + strconv.Itoa(a.Wall.ID)
|
||||
} else {
|
||||
rmsg.Text += "This attachment is not supported (" + a.Type + ")"
|
||||
}
|
||||
}
|
||||
|
||||
if b.GetBool("UseFileURL") {
|
||||
rmsg.Text += "\n" + strings.Join(urls, "\n")
|
||||
} else {
|
||||
for _, url := range urls {
|
||||
data, err := helper.DownloadFile(url)
|
||||
if err == nil {
|
||||
urlPart := strings.Split(url, "/")
|
||||
name := strings.Split(urlPart[len(urlPart)-1], "?")[0]
|
||||
helper.HandleDownloadData(b.Log, &rmsg, name, "", url, data, b.General)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b.Remote <- rmsg
|
||||
|
||||
10
vendor/modules.txt
vendored
10
vendor/modules.txt
vendored
@@ -29,6 +29,14 @@ github.com/Rhymen/go-whatsapp/crypto/curve25519
|
||||
github.com/Rhymen/go-whatsapp/crypto/hkdf
|
||||
# github.com/RocketChat/Rocket.Chat.Go.SDK v0.0.0-20200922220614-e4a51dfb52e4
|
||||
## explicit
|
||||
# github.com/SevereCloud/vksdk/v2 v2.9.0
|
||||
## explicit
|
||||
github.com/SevereCloud/vksdk/v2
|
||||
github.com/SevereCloud/vksdk/v2/api
|
||||
github.com/SevereCloud/vksdk/v2/events
|
||||
github.com/SevereCloud/vksdk/v2/internal
|
||||
github.com/SevereCloud/vksdk/v2/longpoll-bot
|
||||
github.com/SevereCloud/vksdk/v2/object
|
||||
# github.com/blang/semver v3.5.1+incompatible
|
||||
github.com/blang/semver
|
||||
# github.com/d5/tengo/v2 v2.6.2
|
||||
@@ -357,6 +365,7 @@ golang.org/x/sys/unix
|
||||
golang.org/x/sys/windows
|
||||
# golang.org/x/text v0.3.4
|
||||
golang.org/x/text/encoding
|
||||
golang.org/x/text/encoding/charmap
|
||||
golang.org/x/text/encoding/internal
|
||||
golang.org/x/text/encoding/internal/identifier
|
||||
golang.org/x/text/encoding/japanese
|
||||
@@ -367,6 +376,7 @@ golang.org/x/text/internal/language
|
||||
golang.org/x/text/internal/language/compact
|
||||
golang.org/x/text/internal/tag
|
||||
golang.org/x/text/language
|
||||
golang.org/x/text/runes
|
||||
golang.org/x/text/secure/bidirule
|
||||
golang.org/x/text/transform
|
||||
golang.org/x/text/unicode/bidi
|
||||
|
||||
Reference in New Issue
Block a user