From b0bbcb032f94dbd530ffc176ad4a48cf87cb8f1d Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Thu, 5 Jan 2023 01:31:15 -0500 Subject: [PATCH] cleanup chatid/topicid parsing --- bridge/telegram/telegram.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 62775309..ba0785cb 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -86,36 +86,44 @@ func TGGetParseMode(b *Btelegram, username string, text string) (textout string, return textout, parsemode } -func (b *Btelegram) Send(msg config.Message) (string, error) { - b.Log.Debugf("=> Receiving %#v", msg) - +func (b *Btelegram) getIds(channel string) (int64, int, error) { var chatid int64 topicid := 0 // get the chatid - if strings.Contains(msg.Channel, "/") { - s := strings.Split(msg.Channel, "/") + if strings.Contains(channel, "/") { + s := strings.Split(channel, "/") if len(s) < 2 { - b.Log.Errorf("Invalid channel format: %#v\n", msg.Channel) - return "", nil + b.Log.Errorf("Invalid channel format: %#v\n", channel) + return 0, 0, nil } id, err := strconv.ParseInt(s[0], 10, 64) if err != nil { - return "", err + return 0, 0, err } chatid = id tid, err := strconv.Atoi(s[1]) if err != nil { - return "", err + return 0, 0, err } topicid = tid } else { - id, err := strconv.ParseInt(msg.Channel, 10, 64) + id, err := strconv.ParseInt(channel, 10, 64) if err != nil { - return "", err + return 0, 0, err } chatid = id } + return chatid, topicid, nil +} + +func (b *Btelegram) Send(msg config.Message) (string, error) { + b.Log.Debugf("=> Receiving %#v", msg) + + chatid, topicid, err := b.getIds(msg.Channel) + if err != nil { + return "", err + } // map the file SHA to our user (caches the avatar) if msg.Event == config.EventAvatarDownload {