Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,37 @@ curl -X POST -H 'Token: 1234ABCD' -H 'Content-Type: application/json' --data '{"

---

## Subscribe to Contact Presence

Subscribes to a contact's presence updates (online/offline and last seen). After
subscribing, your configured webhook receives `Presence` events for that contact. You
should be online yourself to receive presence (wuzapi sends an available presence on
connect). Whether `last_seen` is available depends on the contact's privacy settings.

endpoint: _/user/presence/subscribe_

method: **POST**

```
curl -X POST -H 'Token: 1234ABCD' -H 'Content-Type: application/json' --data '{"Phone":"5491155554444"}' http://localhost:8080/user/presence/subscribe
```

The resulting webhook `Presence` event looks like:

```json
{
"type": "Presence",
"state": "offline",
"from": "5491155554444@s.whatsapp.net",
"last_seen": 1750000000
}
```

`last_seen` is a Unix timestamp, present only when the contact is offline and shares
their last seen. When the contact is online, only `type`, `from` and `state` are sent.

---

## Mark message(s) as read

Indicates that one or more messages were read. Id is an array of messages Ids.
Expand Down
58 changes: 58 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3439,6 +3439,64 @@ func (s *server) SendPresence() http.HandlerFunc {
}
}

// Subscribes to a contact's presence (online/offline + last seen).
// After subscribing, the configured webhook receives "Presence" events for that
// contact (with state and, when offline, a last_seen unix timestamp). Requires the
// session to be available/online (wuzapi sends available presence on connect).
func (s *server) SubscribePresence() http.HandlerFunc {

type subscribePresenceStruct struct {
Phone string
}

return func(w http.ResponseWriter, r *http.Request) {

txtid := r.Context().Value("userinfo").(Values).Get("Id")

if clientManager.GetWhatsmeowClient(txtid) == nil {
s.Respond(w, r, http.StatusInternalServerError, errors.New("no session"))
return
}

decoder := json.NewDecoder(r.Body)
var t subscribePresenceStruct
err := decoder.Decode(&t)
if err != nil {
s.Respond(w, r, http.StatusBadRequest, errors.New("could not decode Payload"))
return
}

if len(t.Phone) < 1 {
s.Respond(w, r, http.StatusBadRequest, errors.New("missing Phone in Payload"))
return
}

jid, ok := parseJID(t.Phone)
if !ok {
s.Respond(w, r, http.StatusBadRequest, errors.New("could not parse Phone"))
return
}

err = clientManager.GetWhatsmeowClient(txtid).SubscribePresence(r.Context(), jid)
if err != nil {
s.Respond(w, r, http.StatusInternalServerError, errors.New("failure subscribing to presence"))
return
}

log.Info().Str("jid", jid.String()).Msg("Subscribed to presence")

response := map[string]interface{}{"Details": "Presence subscription requested successfully"}
responseJson, err := json.Marshal(response)
if err != nil {
s.Respond(w, r, http.StatusInternalServerError, err)
} else {
s.Respond(w, r, http.StatusOK, string(responseJson))
}
return

}
}

// Gets avatar info for user
func (s *server) GetAvatar() http.HandlerFunc {

Expand Down
1 change: 1 addition & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (s *server) routes() {
s.router.Handle("/call/reject", c.Then(s.RejectCall())).Methods("POST")

s.router.Handle("/user/presence", c.Then(s.SendPresence())).Methods("POST")
s.router.Handle("/user/presence/subscribe", c.Then(s.SubscribePresence())).Methods("POST")
s.router.Handle("/user/info", c.Then(s.GetUser())).Methods("POST")
s.router.Handle("/user/check", c.Then(s.CheckUser())).Methods("POST")
s.router.Handle("/user/avatar", c.Then(s.GetAvatar())).Methods("POST")
Expand Down
2 changes: 2 additions & 0 deletions wmiau.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,11 +1158,13 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
case *events.Presence:
postmap["type"] = "Presence"
dowebhook = 1
postmap["from"] = evt.From.String()
if evt.Unavailable {
postmap["state"] = "offline"
if evt.LastSeen.IsZero() {
log.Info().Str("from", evt.From.String()).Msg("User is now offline")
} else {
postmap["last_seen"] = evt.LastSeen.Unix()
log.Info().Str("from", evt.From.String()).Str("lastSeen", fmt.Sprintf("%v", evt.LastSeen)).Msg("User is now offline")
}
} else {
Expand Down
Loading