Skip to content

Commit

Permalink
allow to tag users from telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
akshettrj committed Jan 16, 2023
1 parent c870bf2 commit 9cda716
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 39 deletions.
10 changes: 7 additions & 3 deletions modules/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
2 changes: 2 additions & 0 deletions telegram/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
113 changes: 77 additions & 36 deletions utils/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strings"
"time"
"unicode"

"watgbridge/database"
"watgbridge/state"
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down

0 comments on commit 9cda716

Please sign in to comment.