Skip to content

Commit

Permalink
whatsapp: handle user about message events
Browse files Browse the repository at this point in the history
  • Loading branch information
akshettrj committed Oct 9, 2024
1 parent 3680eaa commit 10bbb42
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions whatsapp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func WhatsAppEventHandler(evt interface{}) {
case *events.PushName:
PushNameEventHandler(v)

case *events.UserAbout:
UserAboutEventHandler(v)

case *events.CallOffer:
CallOfferEventHandler(v)

Expand Down Expand Up @@ -1245,6 +1248,72 @@ func PushNameEventHandler(v *events.PushName) {
database.ContactUpdatePushName(v.JID.User, v.NewPushName)
}

func UserAboutEventHandler(v *events.UserAbout) {
var (
cfg = state.State.Config
logger = state.State.Logger
tgBot = state.State.TelegramBot
)
defer logger.Sync()

logger.Debug("new user_about update",
zap.String("jid", v.JID.String()),
zap.String("new_status", v.Status),
zap.Time("updated_at", v.Timestamp),
)

tgThreadId, threadFound, err := database.ChatThreadGetTgFromWa(v.JID.ToNonAD().String(), cfg.Telegram.TargetChatID)
if err != nil {
logger.Warn(
"failed to find thread for a WhatsApp chat (handling UserAbout event)",
zap.String("chat", v.JID.String()),
zap.Error(err),
)
return
}
if !threadFound || tgThreadId == 0 {
logger.Warn(
"no thread found for a WhatsApp chat (handling UserAbout event)",
zap.String("chat", v.JID.String()),
)
if !cfg.WhatsApp.CreateThreadForInfoUpdates {
return
}
}

tgThreadId, err = utils.TgGetOrMakeThreadFromWa(v.JID.ToNonAD().String(), cfg.Telegram.TargetChatID, utils.WaGetContactName(v.JID.ToNonAD()))
if err != nil {
logger.Warn(
"failed to create a new thread for a WhatsApp chat (handling UserAbout event)",
zap.String("chat", v.JID.String()),
zap.Error(err),
)
return
}

updateMessageText := "User's about message was updated"
if time.Since(v.Timestamp).Seconds() > 60 {
updateMessageText += fmt.Sprintf(
"at %s:\n\n",
html.EscapeString(
v.Timestamp.
In(state.State.LocalLocation).
Format(cfg.TimeFormat),
),
)
} else {
updateMessageText += ":\n\n"
}

updateMessageText += fmt.Sprintf("<code>%s</code>", html.EscapeString(v.Status))

tgBot.SendMessage(
cfg.Telegram.TargetChatID,
updateMessageText,
&gotgbot.SendMessageOpts{MessageThreadId: tgThreadId},
)
}

func RevokedMessageEventHandler(v *events.Message) {
var (
cfg = state.State.Config
Expand Down

0 comments on commit 10bbb42

Please sign in to comment.