added TGGetParseMode to clean up. Added tg upload function for video, audio and voice

This commit is contained in:
humorhenker
2021-02-17 16:52:45 +01:00
parent b9cdc91860
commit 429cf04223
2 changed files with 42 additions and 40 deletions

View File

@@ -383,38 +383,35 @@ func (b *Btelegram) handleEdit(msg *config.Message, chatid int64) (string, error
// handleUploadFile handles native upload of files
func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) string {
var c tgbotapi.Chattable
var captiontext string
var parsemode string
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
file := tgbotapi.FileBytes{
Name: fi.Name,
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|jpe|png)$")
if re.MatchString(fi.Name) {
reimg := regexp.MustCompile(".(jpg|jpe|png)$")
revideo := regexp.MustCompile(".(mp4|m4v)$")
reaudio := regexp.MustCompile(".(mp3|oga)$")
revoice := regexp.MustCompile(".(ogg)$")
if reimg.MatchString(fi.Name) {
pc := tgbotapi.NewPhotoUpload(chatid, file)
pc.Caption = captiontext
pc.ParseMode = parsemode
pc.Caption, pc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
c = pc
} else if revideo.MatchString(fi.Name) {
vc := tgbotapi.NewVideoUpload(chatid, file)
vc.Caption, vc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
c = vc
} else if reaudio.MatchString(fi.Name) {
ac := tgbotapi.NewAudioUpload(chatid, file)
ac.Caption, ac.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
c = ac
} else if revoice.MatchString(fi.Name) {
voc := tgbotapi.NewVoiceUpload(chatid, file)
voc.Caption, voc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
c = voc
} else {
dc := tgbotapi.NewDocumentUpload(chatid, file)
dc.Caption = captiontext
dc.ParseMode = parsemode
dc.Caption, dc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
c = dc
}
_, err := b.c.Send(c)

View File

@@ -69,6 +69,28 @@ func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
return nil
}
func TGGetParseMode(b *Btelegram, username string, text string) (TextOut string, ParseMode string) {
TextOut = username + text
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 b.GetString("MessageFormat") == MarkdownV2 {
b.Log.Debug("Using mode MarkdownV2")
ParseMode = MarkdownV2
}
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
b.Log.Debug("Using mode HTML - nick only")
TextOut = username + html.EscapeString(text)
ParseMode = tgbotapi.ModeHTML
}
return TextOut, ParseMode
}
func (b *Btelegram) Send(msg config.Message) (string, error) {
b.Log.Debugf("=> Receiving %#v", msg)
@@ -131,24 +153,7 @@ func (b *Btelegram) getFileDirectURL(id string) string {
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
m := tgbotapi.NewMessage(chatid, "")
m.Text = username + text
if b.GetString("MessageFormat") == HTMLFormat {
b.Log.Debug("Using mode HTML")
m.ParseMode = tgbotapi.ModeHTML
}
if b.GetString("MessageFormat") == "Markdown" {
b.Log.Debug("Using mode markdown")
m.ParseMode = tgbotapi.ModeMarkdown
}
if b.GetString("MessageFormat") == MarkdownV2 {
b.Log.Debug("Using mode MarkdownV2")
m.ParseMode = MarkdownV2
}
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
b.Log.Debug("Using mode HTML - nick only")
m.Text = username + html.EscapeString(text)
m.ParseMode = tgbotapi.ModeHTML
}
m.Text, m.ParseMode = TGGetParseMode(b, username, text)
m.DisableWebPagePreview = b.GetBool("DisableWebPagePreview")