Download files from telegram and reupload to supported bridges (telegram). #278

This commit is contained in:
Wim 2017-11-04 14:50:01 +01:00
parent 7dbebd3ea7
commit 1b30575510
1 changed files with 77 additions and 8 deletions

View File

@ -1,9 +1,11 @@
package btelegram package btelegram
import ( import (
"regexp"
"strconv" "strconv"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/go-telegram-bot-api/telegram-bot-api"
) )
@ -94,6 +96,27 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
return "", nil return "", nil
} }
if msg.Extra != nil {
// check if we have files to upload (from slack, telegram or mattermost)
if len(msg.Extra["file"]) > 0 {
var c tgbotapi.Chattable
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
file := tgbotapi.FileBytes{fi.Name, *fi.Data}
re := regexp.MustCompile(".(jpg|png)$")
if re.MatchString(fi.Name) {
c = tgbotapi.NewPhotoUpload(chatid, file)
} else {
c = tgbotapi.NewDocumentUpload(chatid, file)
}
_, err := b.c.Send(c)
if err != nil {
log.Errorf("file upload failed: %#v")
}
}
}
}
m := tgbotapi.NewMessage(chatid, msg.Username+msg.Text) m := tgbotapi.NewMessage(chatid, msg.Username+msg.Text)
if b.Config.MessageFormat == "HTML" { if b.Config.MessageFormat == "HTML" {
m.ParseMode = tgbotapi.ModeHTML m.ParseMode = tgbotapi.ModeHTML
@ -113,6 +136,9 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
username := "" username := ""
channel := "" channel := ""
text := "" text := ""
fmsg := config.Message{Extra: make(map[string][]interface{})}
// handle channels // handle channels
if update.ChannelPost != nil { if update.ChannelPost != nil {
message = update.ChannelPost message = update.ChannelPost
@ -146,18 +172,17 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
if username == "" { if username == "" {
username = "unknown" username = "unknown"
} }
if message.Sticker != nil && b.Config.UseInsecureURL { if message.Sticker != nil {
text = text + " " + b.getFileDirectURL(message.Sticker.FileID) b.handleDownload(message.Sticker, &fmsg)
} }
if message.Video != nil && b.Config.UseInsecureURL { if message.Video != nil {
text = text + " " + b.getFileDirectURL(message.Video.FileID) b.handleDownload(message.Video, &fmsg)
} }
if message.Photo != nil && b.Config.UseInsecureURL { if message.Photo != nil && b.Config.UseInsecureURL {
photos := *message.Photo b.handleDownload(message.Photo, &fmsg)
// last photo is the biggest
text = text + " " + b.getFileDirectURL(photos[len(photos)-1].FileID)
} }
if message.Document != nil && b.Config.UseInsecureURL { if message.Document != nil && b.Config.UseInsecureURL {
b.handleDownload(message.Sticker, &fmsg)
text = text + " " + message.Document.FileName + " : " + b.getFileDirectURL(message.Document.FileID) text = text + " " + message.Document.FileName + " : " + b.getFileDirectURL(message.Document.FileID)
} }
@ -181,7 +206,7 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
text = text + " (re @" + usernameReply + ":" + message.ReplyToMessage.Text + ")" text = text + " (re @" + usernameReply + ":" + message.ReplyToMessage.Text + ")"
} }
if text != "" { if text != "" || len(fmsg.Extra) > 0 {
flog.Debugf("Sending message from %s on %s to gateway", username, b.Account) flog.Debugf("Sending message from %s on %s to gateway", username, b.Account)
msg := config.Message{Username: username, Text: text, Channel: channel, Account: b.Account, UserID: strconv.Itoa(message.From.ID), ID: strconv.Itoa(message.MessageID)} msg := config.Message{Username: username, Text: text, Channel: channel, Account: b.Account, UserID: strconv.Itoa(message.From.ID), ID: strconv.Itoa(message.MessageID)}
flog.Debugf("Message is %#v", msg) flog.Debugf("Message is %#v", msg)
@ -197,3 +222,47 @@ func (b *Btelegram) getFileDirectURL(id string) string {
} }
return res return res
} }
func (b *Btelegram) handleDownload(file interface{}, msg *config.Message) {
size := 0
url := ""
name := ""
text := ""
switch v := file.(type) {
case *tgbotapi.Sticker:
size = v.FileSize
url = b.getFileDirectURL(v.FileID)
name = "sticker"
text = " " + url
case *tgbotapi.Video:
size = v.FileSize
url = b.getFileDirectURL(v.FileID)
name = "video"
text = " " + url
case *[]tgbotapi.PhotoSize:
photos := *v
size = photos[len(photos)-1].FileSize
url = b.getFileDirectURL(photos[len(photos)-1].FileID)
name = "photo"
text = " " + url
case *tgbotapi.Document:
size = v.FileSize
url = b.getFileDirectURL(v.FileID)
name = v.FileName
text = " " + v.FileName + " : " + url
}
if b.Config.UseInsecureURL {
msg.Text = text
return
}
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
// limit to 1MB for now
if size <= 1000000 {
data, err := helper.DownloadFile(url)
if err != nil {
flog.Errorf("download %s failed %#v", url, err)
} else {
msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data})
}
}
}