Skip to content

Commit

Permalink
feat: add support for sending locations from TG
Browse files Browse the repository at this point in the history
Currently live locations are not live
  • Loading branch information
akshettrj committed Nov 1, 2024
1 parent 37f0f22 commit 4f8f506
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions utils/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,74 @@ func TgSendToWhatsApp(b *gotgbot.Bot, c *ext.Context,
return TgReplyWithErrorByContext(b, c, "Failed to add to database", err)
}

} else if msgToForward.Location != nil {

location := msgToForward.Location
isLive := (location.LivePeriod > 0)

msgToSend := &waE2E.Message{}
if isLive {
// TODO: make this live
msgToSend.LiveLocationMessage = &waE2E.LiveLocationMessage{
DegreesLatitude: &location.Latitude,
DegreesLongitude: &location.Longitude,
AccuracyInMeters: proto.Uint32(uint32(location.HorizontalAccuracy)),
DegreesClockwiseFromMagneticNorth: proto.Uint32(uint32(location.Heading)),
ContextInfo: &waE2E.ContextInfo{},
}
if isReply {
msgToSend.LiveLocationMessage.ContextInfo.StanzaID = proto.String(stanzaId)
msgToSend.LiveLocationMessage.ContextInfo.Participant = proto.String(participant)
msgToSend.LiveLocationMessage.ContextInfo.QuotedMessage = &waE2E.Message{Conversation: proto.String("")}
}
if isEphemeral {
msgToSend.LiveLocationMessage.ContextInfo.Expiration = &ephemeralTimer
}
} else {
msgToSend.LocationMessage = &waE2E.LocationMessage{
DegreesLatitude: &location.Latitude,
DegreesLongitude: &location.Longitude,
DegreesClockwiseFromMagneticNorth: proto.Uint32(uint32(location.Heading)),
AccuracyInMeters: proto.Uint32(uint32(location.HorizontalAccuracy)),
ContextInfo: &waE2E.ContextInfo{},
}
if isReply {
msgToSend.LocationMessage.ContextInfo.StanzaID = proto.String(stanzaId)
msgToSend.LocationMessage.ContextInfo.Participant = proto.String(participant)
msgToSend.LocationMessage.ContextInfo.QuotedMessage = &waE2E.Message{Conversation: proto.String("")}
}
if isEphemeral {
msgToSend.LocationMessage.ContextInfo.Expiration = &ephemeralTimer
}
}

sentMsg, err := waClient.SendMessage(context.Background(), waChatJID, msgToSend)
if err != nil {
return TgReplyWithErrorByContext(b, c, "Failed to send sticker to WhatsApp", err)
}
revokeKeyboard := TgMakeRevokeKeyboard(sentMsg.ID, waChatJID.String(), false)
if cfg.Telegram.EmojiConfirmation {
b.SetMessageReaction(
msgToForward.Chat.Id,
msgToForward.MessageId,
&gotgbot.SetMessageReactionOpts{Reaction: []gotgbot.ReactionType{gotgbot.ReactionTypeEmoji{Emoji: "👍"}}},
)
} else {
msg, err := TgReplyTextByContext(b, c, "Successfully sent", revokeKeyboard, cfg.Telegram.SilentConfirmation)
if err == nil {
go func(_b *gotgbot.Bot, _m *gotgbot.Message) {
time.Sleep(15 * time.Second)
_b.DeleteMessage(_m.Chat.Id, _m.MessageId, &gotgbot.DeleteMessageOpts{})
}(b, msg)
}
}

err = database.MsgIdAddNewPair(sentMsg.ID, waClient.Store.ID.String(), waChatJID.String(),
cfg.Telegram.TargetChatID, msgToForward.MessageId, msgToForward.MessageThreadId)
if err != nil {
return TgReplyWithErrorByContext(b, c, "Failed to add to database", err)
}

} else if msgToForward.Text != "" {

if emojis := gomoji.CollectAll(msgToForward.Text); isReply && len(emojis) == 1 && gomoji.RemoveEmojis(msgToForward.Text) == "" {
Expand Down

0 comments on commit 4f8f506

Please sign in to comment.