Skip to content

Commit

Permalink
update whatsapp.go improve presence, sync limit, and node binary parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaskiddo committed Dec 9, 2023
1 parent 6835e13 commit 647de74
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions internal/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var Daemon = &cobra.Command{
if pkgWhatsApp.WhatsAppClient != nil {
if pkgWhatsApp.WhatsAppClient.IsConnected() && !isHandlerOn {
log.Println(log.LogLevelInfo, "Starting WhatsApp Client Event Listener for OpenAI GPT")

pkgWhatsApp.WhatsAppClient.AddEventHandler(pkgWhatsApp.WhatsAppHandler)

isHandlerOn = true
Expand Down
32 changes: 24 additions & 8 deletions pkg/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"google.golang.org/protobuf/proto"

"go.mau.fi/whatsmeow"
wabin "go.mau.fi/whatsmeow/binary"
waproto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/store"
"go.mau.fi/whatsmeow/store/sqlstore"
Expand Down Expand Up @@ -64,6 +65,7 @@ func init() {

func WhatsAppInitClient(device *store.Device) {
var err error
wabin.IndentXML = true

if WhatsAppClient == nil {
if device == nil {
Expand All @@ -75,6 +77,11 @@ func WhatsAppInitClient(device *store.Device) {
store.DeviceProps.Os = proto.String(WhatsAppGetUserOS())
store.DeviceProps.PlatformType = WhatsAppGetUserAgent("chrome").Enum()
store.DeviceProps.RequireFullSync = proto.Bool(false)
store.DeviceProps.HistorySyncConfig = &waproto.DeviceProps_HistorySyncConfig{
FullSyncDaysLimit: proto.Uint32(1),
FullSyncSizeMbLimit: proto.Uint32(10),
StorageQuotaMb: proto.Uint32(10),
}

// Set Client Versions
version.Major, err = env.GetEnvInt("WHATSAPP_VERSION_MAJOR")
Expand Down Expand Up @@ -103,6 +110,9 @@ func WhatsAppInitClient(device *store.Device) {

// Set WhatsApp Client Auto Trust Identity
WhatsAppClient.AutoTrustIdentity = true

// Disable Self Broadcast
WhatsAppClient.DontSendSelfBroadcast = true
}
}

Expand Down Expand Up @@ -178,9 +188,6 @@ func WhatsAppLogin(jid string) (string, int, error) {
return "", 0, err
}

// Set WhatsApp Client Presence to Available
_ = WhatsAppClient.SendPresence(types.PresenceAvailable)

return code, 160, nil
} else {
// Device ID is Exist
Expand Down Expand Up @@ -211,9 +218,6 @@ func WhatsAppReconnect() error {
return err
}

// Set WhatsApp Client Presence to Available
_ = WhatsAppClient.SendPresence(types.PresenceAvailable)

return nil
}

Expand All @@ -230,7 +234,7 @@ func WhatsAppLogout() error {
var err error

// Set WhatsApp Client Presence to Unavailable
_ = WhatsAppClient.SendPresence(types.PresenceUnavailable)
WhatsAppPresence(false)

// Logout WhatsApp Client and Disconnect from WebSocket
err = WhatsAppClient.Logout()
Expand All @@ -257,6 +261,14 @@ func WhatsAppLogout() error {
return errors.New("WhatsApp Client is not Valid")
}

func WhatsAppPresence(isAvailable bool) {
if isAvailable {
_ = WhatsAppClient.SendPresence(types.PresenceAvailable)
} else {
_ = WhatsAppClient.SendPresence(types.PresenceUnavailable)
}
}

func WhatsAppComposeStatus(rjid types.JID, isComposing bool, isAudio bool) {
// Set Compose Status
var typeCompose types.ChatPresence
Expand Down Expand Up @@ -344,8 +356,12 @@ func WhatsAppHandler(event interface{}) {
log.Println(log.LogLevelInfo, "Question : "+question)

// Set Chat Presence
WhatsAppPresence(true)
WhatsAppComposeStatus(evt.Info.Chat, true, false)
defer WhatsAppComposeStatus(evt.Info.Chat, false, false)
defer func() {
WhatsAppComposeStatus(evt.Info.Chat, false, false)
WhatsAppPresence(false)
}()

response, err := gpt.GPT3Response(question)
if err != nil {
Expand Down

0 comments on commit 647de74

Please sign in to comment.