Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 39ec0d73b84d55f78e87e0c53ad07022fcee9506 Author: Wim <wim@42.be> Date: Sat Nov 04 14:50:01 2017 +0000 Download files from telegram and reupload to supported bridges (telegram). #278 diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 00ed6de..917fc86 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -19 +111 @@  package btelegram    import ( + "regexp"   "strconv"     "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper"   log "github.com/Sirupsen/logrus"   "github.com/go-telegram-bot-api/telegram-bot-api"  ) @@ -946 +9627 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {   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)   if b.Config.MessageFormat == "HTML" {   m.ParseMode = tgbotapi.ModeHTML @@ -1136 +1369 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {   username := ""   channel := ""   text := "" + + fmsg := config.Message{Extra: make(map[string][]interface{})} +   // handle channels   if update.ChannelPost != nil {   message = update.ChannelPost @@ -14618 +17217 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {   if username == "" {   username = "unknown"   } - if message.Sticker != nil && b.Config.UseInsecureURL { - text = text + " " + b.getFileDirectURL(message.Sticker.FileID) + if message.Sticker != nil { + b.handleDownload(message.Sticker, &fmsg)   } - if message.Video != nil && b.Config.UseInsecureURL { - text = text + " " + b.getFileDirectURL(message.Video.FileID) + if message.Video != nil { + b.handleDownload(message.Video, &fmsg)   }   if message.Photo != nil && b.Config.UseInsecureURL { - photos := *message.Photo - // last photo is the biggest - text = text + " " + b.getFileDirectURL(photos[len(photos)-1].FileID) + b.handleDownload(message.Photo, &fmsg)   }   if message.Document != nil && b.Config.UseInsecureURL { + b.handleDownload(message.Sticker, &fmsg)   text = text + " " + message.Document.FileName + " : " + b.getFileDirectURL(message.Document.FileID)   }   @@ -1817 +2067 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {   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)   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) @@ -1973 +22247 @@ func (b *Btelegram) getFileDirectURL(id string) string {   }   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}) + } + } +}