Skip to content

Commit 6acb5c3

Browse files
authored
Add method to query business profile (#559)
1 parent 051606b commit 6acb5c3

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

types/user.go

+25
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,28 @@ type Blocklist struct {
145145
DHash string // TODO is this just a timestamp?
146146
JIDs []JID
147147
}
148+
149+
// BusinessHoursConfig contains business operating hours of a WhatsApp business.
150+
type BusinessHoursConfig struct {
151+
DayOfWeek string
152+
Mode string
153+
OpenTime string
154+
CloseTime string
155+
}
156+
157+
// Category contains a WhatsApp business category.
158+
type Category struct {
159+
ID string
160+
Name string
161+
}
162+
163+
// BusinessProfile contains the profile information of a WhatsApp business.
164+
type BusinessProfile struct {
165+
JID JID
166+
Address string
167+
Email string
168+
Categories []Category
169+
ProfileOptions map[string]string
170+
BusinessHoursTimeZone string
171+
BusinessHours []BusinessHoursConfig
172+
}

user.go

+85
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,91 @@ func (cli *Client) GetUserInfo(jids []types.JID) (map[types.JID]types.UserInfo,
227227
return respData, nil
228228
}
229229

230+
func (cli *Client) parseBusinessProfile(node *waBinary.Node) (*types.BusinessProfile, error) {
231+
profileNode := node.GetChildByTag("profile")
232+
jid, ok := profileNode.AttrGetter().GetJID("jid", true)
233+
if !ok {
234+
return nil, errors.New("missing jid in business profile")
235+
}
236+
address := string(profileNode.GetChildByTag("address").Content.([]byte))
237+
email := string(profileNode.GetChildByTag("email").Content.([]byte))
238+
businessHour := profileNode.GetChildByTag("business_hours")
239+
businessHourTimezone := businessHour.AttrGetter().String("timezone")
240+
businessHoursConfigs := businessHour.GetChildren()
241+
businessHours := make([]types.BusinessHoursConfig, 0)
242+
for _, config := range businessHoursConfigs {
243+
if config.Tag != "business_hours_config" {
244+
continue
245+
}
246+
dow := config.AttrGetter().String("dow")
247+
mode := config.AttrGetter().String("mode")
248+
openTime := config.AttrGetter().String("open_time")
249+
closeTime := config.AttrGetter().String("close_time")
250+
businessHours = append(businessHours, types.BusinessHoursConfig{
251+
DayOfWeek: dow,
252+
Mode: mode,
253+
OpenTime: openTime,
254+
CloseTime: closeTime,
255+
})
256+
}
257+
categoriesNode := profileNode.GetChildByTag("categories")
258+
categories := make([]types.Category, 0)
259+
for _, category := range categoriesNode.GetChildren() {
260+
if category.Tag != "category" {
261+
continue
262+
}
263+
id := category.AttrGetter().String("id")
264+
name := string(category.Content.([]byte))
265+
categories = append(categories, types.Category{
266+
ID: id,
267+
Name: name,
268+
})
269+
}
270+
profileOptionsNode := profileNode.GetChildByTag("profile_options")
271+
profileOptions := make(map[string]string)
272+
for _, option := range profileOptionsNode.GetChildren() {
273+
profileOptions[option.Tag] = string(option.Content.([]byte))
274+
}
275+
return &types.BusinessProfile{
276+
JID: jid,
277+
Email: email,
278+
Address: address,
279+
Categories: categories,
280+
ProfileOptions: profileOptions,
281+
BusinessHoursTimeZone: businessHourTimezone,
282+
BusinessHours: businessHours,
283+
}, nil
284+
}
285+
286+
// GetBusinessProfile gets the profile info of a WhatsApp business account
287+
func (cli *Client) GetBusinessProfile(jid types.JID) (*types.BusinessProfile, error) {
288+
resp, err := cli.sendIQ(infoQuery{
289+
Type: iqGet,
290+
To: types.ServerJID,
291+
Namespace: "w:biz",
292+
Content: []waBinary.Node{{
293+
Tag: "business_profile",
294+
Attrs: waBinary.Attrs{
295+
"v": "244",
296+
},
297+
Content: []waBinary.Node{{
298+
Tag: "profile",
299+
Attrs: waBinary.Attrs{
300+
"jid": jid,
301+
},
302+
}},
303+
}},
304+
})
305+
if err != nil {
306+
return nil, err
307+
}
308+
node, ok := resp.GetOptionalChildByTag("business_profile")
309+
if !ok {
310+
return nil, &ElementMissingError{Tag: "business_profile", In: "response to business profile query"}
311+
}
312+
return cli.parseBusinessProfile(&node)
313+
}
314+
230315
// GetUserDevices gets the list of devices that the given user has. The input should be a list of
231316
// regular JIDs, and the output will be a list of AD JIDs. The local device will not be included in
232317
// the output even if the user's JID is included in the input. All other devices will be included.

0 commit comments

Comments
 (0)