Skip to content

Commit 0c7da35

Browse files
authored
feat(telegram): Redirect status replies to user threads (#78)
Fixes an issue where replies to a status in Telegram were incorrectly routed. Now, replies made in the "Status" thread are forwarded to the correct user-specific chat thread, both in Telegram and in the database, ensuring proper conversation flow.
1 parent 09720f1 commit 0c7da35

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

telegram/handlers.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,29 @@ func BridgeTelegramToWhatsAppHandler(b *gotgbot.Bot, c *ext.Context) error {
186186
// Status Update
187187
if strings.HasSuffix(waChatID, "@broadcast") {
188188
waChatID = participantID
189+
190+
waChatJID, _ := utils.WaParseJID(participantID)
191+
contactName := utils.WaGetContactName(waChatJID)
192+
193+
contactThreadID, err := utils.TgGetOrMakeThreadFromWa(participantID, c.EffectiveChat.Id, contactName)
194+
if err != nil {
195+
return utils.TgReplyWithErrorByContext(b, c, "Failed to get or create a thread for the contact", err)
196+
}
197+
198+
forwardedMsg, err := b.ForwardMessage(c.EffectiveChat.Id, c.EffectiveChat.Id, c.EffectiveMessage.MessageId, &gotgbot.ForwardMessageOpts{
199+
MessageThreadId: contactThreadID,
200+
})
201+
if err != nil {
202+
return utils.TgReplyWithErrorByContext(b, c, "Failed to forward the message to the contact's thread", err)
203+
}
204+
205+
msgCopy := *msgToForward
206+
msgCopy.MessageId = forwardedMsg.MessageId
207+
msgCopy.MessageThreadId = forwardedMsg.MessageThreadId
208+
209+
finalWaChatJID, _ := utils.WaParseJID(waChatID)
210+
return utils.TgSendToWhatsApp(b, c, &msgCopy, msgToReplyTo, finalWaChatJID, participantID, stanzaID, true)
211+
189212
} else if participantID != "" {
190213
participant, _ := utils.WaParseJID(participantID)
191214
participantID = participant.ToNonAD().String()

0 commit comments

Comments
 (0)