summaryrefslogtreecommitdiffstats
path: root/bridge/telegram/handlers.go
diff options
context:
space:
mode:
authorThom Dickson <td3of4@gmail.com>2023-03-14 18:03:04 -0400
committerGitHub <noreply@github.com>2023-03-14 23:03:04 +0100
commit601f48a50ea31a9631011766c680ecef931ddafc (patch)
treeaa88bff487f12a05d70c7677ddc2a3e14bbd9fbf /bridge/telegram/handlers.go
parentc2b8e298d8c0a15a0e6489b658efe58030a18164 (diff)
downloadmatterbridge-msglm-601f48a50ea31a9631011766c680ecef931ddafc.tar.gz
matterbridge-msglm-601f48a50ea31a9631011766c680ecef931ddafc.tar.bz2
matterbridge-msglm-601f48a50ea31a9631011766c680ecef931ddafc.zip
Add support for Telegram topics (telegram) (#1942)
Topics are surfaced by appending /<topic-id> to the channel setting for the gateway. An example for the topic with ID of 16 would be: ``` [[gateway.inout]] account="telegram.mytelegram" channel="-100xxxxxxxxxx/16" ```
Diffstat (limited to 'bridge/telegram/handlers.go')
-rw-r--r--bridge/telegram/handlers.go27
1 files changed, 19 insertions, 8 deletions
diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go
index 5ffb1eca..985397ef 100644
--- a/bridge/telegram/handlers.go
+++ b/bridge/telegram/handlers.go
@@ -11,7 +11,7 @@ import (
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper"
"github.com/davecgh/go-spew/spew"
- tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
+ tgbotapi "github.com/matterbridge/telegram-bot-api/v6"
)
func (b *Btelegram) handleUpdate(rmsg *config.Message, message, posted, edited *tgbotapi.Message) *tgbotapi.Message {
@@ -20,6 +20,11 @@ func (b *Btelegram) handleUpdate(rmsg *config.Message, message, posted, edited *
if posted.Text == "/chatId" {
chatID := strconv.FormatInt(posted.Chat.ID, 10)
+ // Handle chat topics
+ if posted.IsTopicMessage {
+ chatID = chatID + "/" + strconv.Itoa(posted.MessageThreadID)
+ }
+
_, err := b.Send(config.Message{
Channel: chatID,
Text: fmt.Sprintf("ID of this chat: %s", chatID),
@@ -91,7 +96,8 @@ func (b *Btelegram) handleForwarded(rmsg *config.Message, message *tgbotapi.Mess
// handleQuoting handles quoting of previous messages
func (b *Btelegram) handleQuoting(rmsg *config.Message, message *tgbotapi.Message) {
- if message.ReplyToMessage != nil {
+ // Used to check if the message was a reply to the root topic
+ if message.ReplyToMessage != nil && !(message.ReplyToMessage.MessageID == message.MessageThreadID) { //nolint:nestif
usernameReply := ""
if message.ReplyToMessage.From != nil {
if b.GetBool("UseFirstName") {
@@ -211,9 +217,14 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
// set the ID's from the channel or group message
rmsg.ID = strconv.Itoa(message.MessageID)
rmsg.Channel = strconv.FormatInt(message.Chat.ID, 10)
+ if message.MessageThreadID != 0 {
+ rmsg.Channel += "/" + strconv.Itoa(message.MessageThreadID)
+ }
// preserve threading from telegram reply
- if message.ReplyToMessage != nil {
+ if message.ReplyToMessage != nil &&
+ // Used to check if the message was a reply to the root topic
+ !(message.ReplyToMessage.MessageID == message.MessageThreadID) {
rmsg.ParentID = strconv.Itoa(message.ReplyToMessage.MessageID)
}
@@ -326,12 +337,12 @@ func (b *Btelegram) maybeConvertWebp(name *string, data *[]byte) {
// handleDownloadFile handles file download
func (b *Btelegram) handleDownload(rmsg *config.Message, message *tgbotapi.Message) error {
- size := 0
+ size := int64(0)
var url, name, text string
switch {
case message.Sticker != nil:
text, name, url = b.getDownloadInfo(message.Sticker.FileID, ".webp", true)
- size = message.Sticker.FileSize
+ size = int64(message.Sticker.FileSize)
case message.Voice != nil:
text, name, url = b.getDownloadInfo(message.Voice.FileID, ".ogg", true)
size = message.Voice.FileSize
@@ -348,7 +359,7 @@ func (b *Btelegram) handleDownload(rmsg *config.Message, message *tgbotapi.Messa
text = " " + message.Document.FileName + " : " + url
case message.Photo != nil:
photos := message.Photo
- size = photos[len(photos)-1].FileSize
+ size = int64(photos[len(photos)-1].FileSize)
text, name, url = b.getDownloadInfo(photos[len(photos)-1].FileID, "", true)
}
@@ -452,7 +463,7 @@ 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, parentID int) (string, error) {
+func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64, threadid int, parentID int) (string, error) {
var media []interface{}
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
@@ -502,7 +513,7 @@ func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64, parentID
}
}
- return b.sendMediaFiles(msg, chatid, parentID, media)
+ return b.sendMediaFiles(msg, chatid, threadid, parentID, media)
}
func (b *Btelegram) handleQuote(message, quoteNick, quoteMessage string) string {