From 9cda71653cb185ac96a4e60e592d92ac61620e48 Mon Sep 17 00:00:00 2001 From: Akshett Rai Jindal Date: Mon, 16 Jan 2023 19:45:13 +0530 Subject: [PATCH] allow to tag users from telegram --- modules/load.go | 10 ++-- telegram/handlers.go | 2 + utils/telegram.go | 113 +++++++++++++++++++++++++++++-------------- 3 files changed, 86 insertions(+), 39 deletions(-) diff --git a/modules/load.go b/modules/load.go index 840186a..b43861d 100644 --- a/modules/load.go +++ b/modules/load.go @@ -39,9 +39,13 @@ func LoadModuleHandlers() { state.State.WhatsAppClient.AddEventHandler(handler) } - log.Println("Modules loaded:") - for _, plugin := range state.State.Modules { - log.Println(plugin) + if len(state.State.Modules) > 0 { + log.Println("Modules loaded:") + for _, plugin := range state.State.Modules { + log.Println("- " + plugin) + } + } else { + log.Println("No modules loaded") } } diff --git a/telegram/handlers.go b/telegram/handlers.go index c5b5c04..88fb6ab 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -143,6 +143,8 @@ func BridgeTelegramToWhatsAppHandler(b *gotgbot.Bot, c *ext.Context) error { waChatID, err = database.ChatThreadGetWaFromTg(c.EffectiveChat.Id, c.EffectiveMessage.MessageThreadId) if err != nil { return utils.TgReplyWithErrorByContext(b, c, "Failed to find the chat pairing between this topic and a WhatsApp chat", err) + } else if waChatID == "" { + return utils.TgReplyTextByContext(b, c, "No mapping found between current topic and a WhatsApp chat", nil) } } diff --git a/utils/telegram.go b/utils/telegram.go index 2f95cbe..43ca571 100644 --- a/utils/telegram.go +++ b/utils/telegram.go @@ -9,6 +9,7 @@ import ( "os" "strings" "time" + "unicode" "watgbridge/database" "watgbridge/state" @@ -170,8 +171,31 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, var ( cfg = state.State.Config waClient = state.State.WhatsAppClient + mentions = []string{} ) + var entities []gotgbot.ParsedMessageEntity + if len(msgToForward.Entities) > 0 { + entities = msgToForward.ParseEntities() + } else if len(msgToForward.CaptionEntities) > 0 { + entities = msgToForward.ParseCaptionEntities() + } + + for _, entity := range entities { + if entity.Type == "mention" { + username := entity.Text[1:] + // Check if its a number + for _, c := range username { + if !unicode.IsDigit(c) { + continue + } + } + + parsedJID, _ := WaParseJID(username) + mentions = append(mentions, parsedJID.String()) + } + } + if msgToForward.Photo != nil && len(msgToForward.Photo) > 0 { bestPhoto := msgToForward.Photo[0] @@ -218,14 +242,16 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, ViewOnce: proto.Bool(msgToForward.HasProtectedContent), Height: proto.Uint32(uint32(bestPhoto.Height)), Width: proto.Uint32(uint32(bestPhoto.Width)), + ContextInfo: &waProto.ContextInfo{}, }, } if isReply { - msgToSend.ImageMessage.ContextInfo = &waProto.ContextInfo{ - StanzaId: proto.String(stanzaId), - Participant: proto.String(participant), - QuotedMessage: &waProto.Message{Conversation: proto.String("")}, - } + msgToSend.ImageMessage.ContextInfo.StanzaId = proto.String(stanzaId) + msgToSend.ImageMessage.ContextInfo.Participant = proto.String(participant) + msgToSend.ImageMessage.ContextInfo.QuotedMessage = &waProto.Message{Conversation: proto.String("")} + } + if len(mentions) > 0 { + msgToSend.ImageMessage.ContextInfo.MentionedJid = mentions } sentMsg, err := waClient.SendMessage(context.Background(), waChatJID, msgToSend) @@ -281,14 +307,16 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, GifPlayback: proto.Bool(false), Height: proto.Uint32(uint32(msgToForward.Video.Height)), Width: proto.Uint32(uint32(msgToForward.Video.Width)), + ContextInfo: &waProto.ContextInfo{}, }, } if isReply { - msgToSend.VideoMessage.ContextInfo = &waProto.ContextInfo{ - StanzaId: proto.String(stanzaId), - Participant: proto.String(participant), - QuotedMessage: &waProto.Message{Conversation: proto.String("")}, - } + msgToSend.VideoMessage.ContextInfo.StanzaId = proto.String(stanzaId) + msgToSend.VideoMessage.ContextInfo.Participant = proto.String(participant) + msgToSend.VideoMessage.ContextInfo.QuotedMessage = &waProto.Message{Conversation: proto.String("")} + } + if len(mentions) > 0 { + msgToSend.VideoMessage.ContextInfo.MentionedJid = mentions } sentMsg, err := waClient.SendMessage(context.Background(), waChatJID, msgToSend) @@ -341,14 +369,16 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, ViewOnce: proto.Bool(msgToForward.HasProtectedContent), Seconds: proto.Uint32(uint32(msgToForward.VideoNote.Duration)), GifPlayback: proto.Bool(false), + ContextInfo: &waProto.ContextInfo{}, }, } if isReply { - msgToSend.VideoMessage.ContextInfo = &waProto.ContextInfo{ - StanzaId: proto.String(stanzaId), - Participant: proto.String(participant), - QuotedMessage: &waProto.Message{Conversation: proto.String("")}, - } + msgToSend.VideoMessage.ContextInfo.StanzaId = proto.String(stanzaId) + msgToSend.VideoMessage.ContextInfo.Participant = proto.String(participant) + msgToSend.VideoMessage.ContextInfo.QuotedMessage = &waProto.Message{Conversation: proto.String("")} + } + if len(mentions) > 0 { + msgToSend.VideoMessage.ContextInfo.MentionedJid = mentions } sentMsg, err := waClient.SendMessage(context.Background(), waChatJID, msgToSend) @@ -404,14 +434,16 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, Width: proto.Uint32(uint32(msgToForward.Animation.Width)), Seconds: proto.Uint32(uint32(msgToForward.Animation.Duration)), GifAttribution: waProto.VideoMessage_GIPHY.Enum(), + ContextInfo: &waProto.ContextInfo{}, }, } if isReply { - msgToSend.VideoMessage.ContextInfo = &waProto.ContextInfo{ - StanzaId: proto.String(stanzaId), - Participant: proto.String(participant), - QuotedMessage: &waProto.Message{Conversation: proto.String("")}, - } + msgToSend.VideoMessage.ContextInfo.StanzaId = proto.String(stanzaId) + msgToSend.VideoMessage.ContextInfo.Participant = proto.String(participant) + msgToSend.VideoMessage.ContextInfo.QuotedMessage = &waProto.Message{Conversation: proto.String("")} + } + if len(mentions) > 0 { + msgToSend.VideoMessage.ContextInfo.MentionedJid = mentions } sentMsg, err := waClient.SendMessage(context.Background(), waChatJID, msgToSend) @@ -462,14 +494,16 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, FileLength: proto.Uint64(uint64(len(audioBytes))), Seconds: proto.Uint32(uint32(msgToForward.Audio.Duration)), Ptt: proto.Bool(false), + ContextInfo: &waProto.ContextInfo{}, }, } if isReply { - msgToSend.AudioMessage.ContextInfo = &waProto.ContextInfo{ - StanzaId: proto.String(stanzaId), - Participant: proto.String(participant), - QuotedMessage: &waProto.Message{Conversation: proto.String("")}, - } + msgToSend.AudioMessage.ContextInfo.StanzaId = proto.String(stanzaId) + msgToSend.AudioMessage.ContextInfo.Participant = proto.String(participant) + msgToSend.AudioMessage.ContextInfo.QuotedMessage = &waProto.Message{Conversation: proto.String("")} + } + if len(mentions) > 0 { + msgToSend.AudioMessage.ContextInfo.MentionedJid = mentions } sentMsg, err := waClient.SendMessage(context.Background(), waChatJID, msgToSend) @@ -520,14 +554,16 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, FileLength: proto.Uint64(uint64(len(voiceBytes))), Seconds: proto.Uint32(uint32(msgToForward.Voice.Duration)), Ptt: proto.Bool(true), + ContextInfo: &waProto.ContextInfo{}, }, } if isReply { - msgToSend.AudioMessage.ContextInfo = &waProto.ContextInfo{ - StanzaId: proto.String(stanzaId), - Participant: proto.String(participant), - QuotedMessage: &waProto.Message{Conversation: proto.String("")}, - } + msgToSend.AudioMessage.ContextInfo.StanzaId = proto.String(stanzaId) + msgToSend.AudioMessage.ContextInfo.Participant = proto.String(participant) + msgToSend.AudioMessage.ContextInfo.QuotedMessage = &waProto.Message{Conversation: proto.String("")} + } + if len(mentions) > 0 { + msgToSend.AudioMessage.ContextInfo.MentionedJid = mentions } sentMsg, err := waClient.SendMessage(context.Background(), waChatJID, msgToSend) @@ -581,14 +617,16 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, FileEncSha256: uploadedDocument.FileEncSHA256, FileSha256: uploadedDocument.FileSHA256, FileLength: proto.Uint64(uint64(len(documentBytes))), + ContextInfo: &waProto.ContextInfo{}, }, } if isReply { - msgToSend.DocumentMessage.ContextInfo = &waProto.ContextInfo{ - StanzaId: proto.String(stanzaId), - Participant: proto.String(participant), - QuotedMessage: &waProto.Message{Conversation: proto.String("")}, - } + msgToSend.DocumentMessage.ContextInfo.StanzaId = proto.String(stanzaId) + msgToSend.DocumentMessage.ContextInfo.Participant = proto.String(participant) + msgToSend.DocumentMessage.ContextInfo.QuotedMessage = &waProto.Message{Conversation: proto.String("")} + } + if len(mentions) > 0 { + msgToSend.DocumentMessage.ContextInfo.MentionedJid = mentions } sentMsg, err := waClient.SendMessage(context.Background(), waChatJID, msgToSend) @@ -696,7 +734,7 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, } msgToSend := &waProto.Message{} - if isReply { + if isReply || len(mentions) > 0 { msgToSend.ExtendedTextMessage = &waProto.ExtendedTextMessage{ Text: proto.String(msgToForward.Text), ContextInfo: &waProto.ContextInfo{ @@ -705,6 +743,9 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context, QuotedMessage: &waProto.Message{Conversation: proto.String("")}, }, } + if len(mentions) > 0 { + msgToSend.ExtendedTextMessage.ContextInfo.MentionedJid = mentions + } } else { msgToSend.Conversation = proto.String(msgToForward.Text) }