Used tgbotapi caption option to attach caption to photos / documents

This commit is contained in:
Humorhenker
2021-01-11 10:41:00 +00:00
parent 8fcd0f3b6f
commit f29e419885

View File

@@ -5,6 +5,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"text/template/parse"
"unicode/utf16" "unicode/utf16"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
@@ -383,27 +384,44 @@ func (b *Btelegram) handleEdit(msg *config.Message, chatid int64) (string, error
// handleUploadFile handles native upload of files // handleUploadFile handles native upload of files
func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) string { func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) string {
var c tgbotapi.Chattable var c tgbotapi.Chattable
var captiontext string
var parsemode string
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
file := tgbotapi.FileBytes{ file := tgbotapi.FileBytes{
Name: fi.Name, Name: fi.Name,
Bytes: *fi.Data, Bytes: *fi.Data,
} }
captiontext = msg.Username + fi.Comment
if b.GetString("MessageFormat") == HTMLFormat {
b.Log.Debug("Using mode HTML")
parsemode = tgbotapi.ModeHTML
}
if b.GetString("MessageFormat") == "Markdown" {
b.Log.Debug("Using mode markdown")
parsemode = tgbotapi.ModeMarkdown
}
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
b.Log.Debug("Using mode HTML - nick only")
captiontext = msg.Username + html.EscapeString(fi.Comment)
parsemode = tgbotapi.ModeHTML
}
re := regexp.MustCompile(".(jpg|png)$") re := regexp.MustCompile(".(jpg|png)$")
if re.MatchString(fi.Name) { if re.MatchString(fi.Name) {
c = tgbotapi.NewPhotoUpload(chatid, file) pc := tgbotapi.NewPhotoUpload(chatid, file)
pc.Caption = captiontext
pc.ParseMode = parsemode
c = pc
} else { } else {
c = tgbotapi.NewDocumentUpload(chatid, file) dc := tgbotapi.NewDocumentUpload(chatid, file)
dc.Caption = captiontext
dc.ParseMode = parsemode
c = dc
} }
_, err := b.c.Send(c) _, err := b.c.Send(c)
if err != nil { if err != nil {
b.Log.Errorf("file upload failed: %#v", err) b.Log.Errorf("file upload failed: %#v", err)
} }
if fi.Comment != "" {
if _, err := b.sendMessage(chatid, msg.Username, fi.Comment); err != nil {
b.Log.Errorf("posting file comment %s failed: %s", fi.Comment, err)
}
}
} }
return "" return ""
} }