added TGGetParseMode to clean up. Added tg upload function for video, audio and voice
This commit is contained in:
@@ -383,38 +383,35 @@ 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
|
reimg := regexp.MustCompile(".(jpg|jpe|png)$")
|
||||||
if b.GetString("MessageFormat") == HTMLFormat {
|
revideo := regexp.MustCompile(".(mp4|m4v)$")
|
||||||
b.Log.Debug("Using mode HTML")
|
reaudio := regexp.MustCompile(".(mp3|oga)$")
|
||||||
parsemode = tgbotapi.ModeHTML
|
revoice := regexp.MustCompile(".(ogg)$")
|
||||||
}
|
if reimg.MatchString(fi.Name) {
|
||||||
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) {
|
|
||||||
pc := tgbotapi.NewPhotoUpload(chatid, file)
|
pc := tgbotapi.NewPhotoUpload(chatid, file)
|
||||||
pc.Caption = captiontext
|
pc.Caption, pc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
|
||||||
pc.ParseMode = parsemode
|
|
||||||
c = pc
|
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 {
|
} else {
|
||||||
dc := tgbotapi.NewDocumentUpload(chatid, file)
|
dc := tgbotapi.NewDocumentUpload(chatid, file)
|
||||||
dc.Caption = captiontext
|
dc.Caption, dc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
|
||||||
dc.ParseMode = parsemode
|
|
||||||
c = dc
|
c = dc
|
||||||
}
|
}
|
||||||
_, err := b.c.Send(c)
|
_, err := b.c.Send(c)
|
||||||
|
|||||||
@@ -69,6 +69,28 @@ func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
|
|||||||
return nil
|
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) {
|
func (b *Btelegram) Send(msg config.Message) (string, error) {
|
||||||
b.Log.Debugf("=> Receiving %#v", msg)
|
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) {
|
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
|
||||||
m := tgbotapi.NewMessage(chatid, "")
|
m := tgbotapi.NewMessage(chatid, "")
|
||||||
m.Text = username + text
|
m.Text, m.ParseMode = TGGetParseMode(b, 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.DisableWebPagePreview = b.GetBool("DisableWebPagePreview")
|
m.DisableWebPagePreview = b.GetBool("DisableWebPagePreview")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user