5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 16:20:26 +00:00

Refactor telegram (#649)

* Decrease complexity in Send() (makes codeclimate happy)
This commit is contained in:
Wim 2018-12-12 23:50:08 +01:00 committed by GitHub
parent 2f7b4d7f68
commit bfa9a83d31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 35 deletions

View File

@ -1,6 +1,7 @@
package btelegram package btelegram
import ( import (
"html"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -262,6 +263,49 @@ func (b *Btelegram) getDownloadInfo(id string, suffix string, urlpart bool) (str
return text, name, url return text, name, url
} }
// handleDelete handles message deleting
func (b *Btelegram) handleDelete(msg *config.Message, chatid int64) (string, error) {
if msg.ID == "" {
return "", nil
}
msgid, err := strconv.Atoi(msg.ID)
if err != nil {
return "", err
}
_, err = b.c.DeleteMessage(tgbotapi.DeleteMessageConfig{ChatID: chatid, MessageID: msgid})
return "", err
}
// handleEdit handles message editing.
func (b *Btelegram) handleEdit(msg *config.Message, chatid int64) (string, error) {
msgid, err := strconv.Atoi(msg.ID)
if err != nil {
return "", err
}
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
b.Log.Debug("Using mode HTML - nick only")
msg.Text = html.EscapeString(msg.Text)
}
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
switch b.GetString("MessageFormat") {
case HTMLFormat:
b.Log.Debug("Using mode HTML")
m.ParseMode = tgbotapi.ModeHTML
case "Markdown":
b.Log.Debug("Using mode markdown")
m.ParseMode = tgbotapi.ModeMarkdown
}
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
b.Log.Debug("Using mode HTML - nick only")
m.ParseMode = tgbotapi.ModeHTML
}
_, err = b.c.Send(m)
if err != nil {
return "", err
}
return "", nil
}
// 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

View File

@ -75,15 +75,7 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
// Delete message // Delete message
if msg.Event == config.EventMsgDelete { if msg.Event == config.EventMsgDelete {
if msg.ID == "" { return b.handleDelete(&msg, chatid)
return "", nil
}
msgid, err := strconv.Atoi(msg.ID)
if err != nil {
return "", err
}
_, err = b.c.DeleteMessage(tgbotapi.DeleteMessageConfig{ChatID: chatid, MessageID: msgid})
return "", err
} }
// Upload a file if it exists // Upload a file if it exists
@ -101,32 +93,7 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
// edit the message if we have a msg ID // edit the message if we have a msg ID
if msg.ID != "" { if msg.ID != "" {
msgid, err := strconv.Atoi(msg.ID) return b.handleEdit(&msg, chatid)
if err != nil {
return "", err
}
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
b.Log.Debug("Using mode HTML - nick only")
msg.Text = html.EscapeString(msg.Text)
}
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.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 strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
b.Log.Debug("Using mode HTML - nick only")
m.ParseMode = tgbotapi.ModeHTML
}
_, err = b.c.Send(m)
if err != nil {
return "", err
}
return "", nil
} }
// Post normal message // Post normal message