diff options
Diffstat (limited to 'bridge')
-rw-r--r-- | bridge/telegram/telegram.go | 85 |
1 files changed, 77 insertions, 8 deletions
diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 00ed6deb..917fc86f 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -1,9 +1,11 @@ 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" ) @@ -94,6 +96,27 @@ 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 @@ -113,6 +136,9 @@ 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 @@ -146,18 +172,17 @@ 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) } @@ -181,7 +206,7 @@ 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) @@ -197,3 +222,47 @@ 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}) + } + } +} |