Skip to content

Commit

Permalink
whatsapp: create missing threads on Picture or Group events
Browse files Browse the repository at this point in the history
Now if a Picture or Group event comes and the corresponding thread does
not exist, you can enable creation of such threads by setting
`whatsapp.create_thread_for_info_updates` to true in your config
  • Loading branch information
akshettrj committed Jul 12, 2024
1 parent 676bf73 commit 0228012
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ whatsapp:
send_revoked_message_updates: false
whatsmeow_debug_mode: false
send_my_messages_from_other_devices: false # If set to true, the messages sent by you from other devices will be sent to Telgram as well
create_thread_for_info_updates: false # If set to true, new thread will be created (if it doesn't exist) when profile picture changes for group/someone and when group metadata/members changes
#login_database: # Uncomment only if you want to use something other than sqlite
# type: sqlite3
# url: file:wawebstore.db?foreign_keys=on
Expand Down
3 changes: 2 additions & 1 deletion state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Config struct {
SendMyPresence bool `yaml:"send_my_presence"`
SendMyReadReceipts bool `yaml:"send_my_read_receipts"`
SilentConfirmation bool `yaml:"silent_confirmation"`
EmojiConfirmation bool `yaml:"emoji_confirmation"`
EmojiConfirmation bool `yaml:"emoji_confirmation"`
} `yaml:"telegram"`

WhatsApp struct {
Expand Down Expand Up @@ -64,6 +64,7 @@ type Config struct {
SendRevokedMessageUpdates bool `yaml:"send_revoked_message_updates"`
WhatsmeowDebugMode bool `yaml:"whatsmeow_debug_mode"`
SendMyMessagesFromOtherDevices bool `yaml:"send_my_messages_from_other_devices"`
CreateThreadForInfoUpdates bool `yaml:"create_thread_for_info_updates"`
} `yaml:"whatsapp"`

Database map[string]string `yaml:"database"`
Expand Down
38 changes: 35 additions & 3 deletions whatsapp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,21 @@ func PictureEventHandler(v *events.Picture) {
"no thread found for a WhatsApp chat (handling Picture event)",
zap.String("chat", v.JID.String()),
)
return
if !cfg.WhatsApp.CreateThreadForInfoUpdates {
return
}
}

if v.JID.Server == waTypes.GroupServer {
tgThreadId, err = utils.TgGetOrMakeThreadFromWa(v.JID.ToNonAD().String(), cfg.Telegram.TargetChatID, utils.WaGetGroupName(v.JID))
if err != nil {
logger.Warn(
"failed to create a new thread for a WhatsApp chat (handling Picture event)",
zap.String("chat", v.JID.String()),
zap.Error(err),
)
return
}
changer := utils.WaGetContactName(v.Author)
if v.Remove {
updateText := fmt.Sprintf("The profile picture was removed by %s", html.EscapeString(changer))
Expand Down Expand Up @@ -1353,8 +1364,17 @@ func PictureEventHandler(v *events.Picture) {
}
}
} else if v.JID.Server == waTypes.DefaultUserServer {
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 Picture event)",
zap.String("chat", v.JID.String()),
zap.Error(err),
)
return
}
if v.Remove {
updateText := fmt.Sprintf("The profile picture was removed")
updateText := "The profile picture was removed"
err = utils.TgSendTextById(
tgBot, cfg.Telegram.TargetChatID, tgThreadId,
updateText,
Expand Down Expand Up @@ -1424,7 +1444,19 @@ func GroupInfoEventHandler(v *events.GroupInfo) {
"no thread found for a WhatsApp chat (handling GroupInfo event)",
zap.String("chat", v.JID.String()),
)
return
if cfg.WhatsApp.CreateThreadForInfoUpdates {
tgThreadId, err = utils.TgGetOrMakeThreadFromWa(v.JID.ToNonAD().String(), cfg.Telegram.TargetChatID, utils.WaGetGroupName(v.JID))
if err != nil {
logger.Warn(
"failed to create a new thread for a WhatsApp chat (handling GroupInfo event)",
zap.String("chat", v.JID.String()),
zap.Error(err),
)
return
}
} else {
return
}
}

if v.Announce != nil {
Expand Down

0 comments on commit 0228012

Please sign in to comment.