Fix ContactsArrayMessage and add getBusinessProfile (#1074)

* Fix: ContactsArrayMessage and add getBusinessProfile

* delete package-lock.json

* edit readme.md

* add bussines hours

* make type same with leagcy

* revert
This commit is contained in:
BochilGaming
2022-01-06 23:45:52 +07:00
committed by GitHub
parent 7ffa10a5c5
commit 0b5d772b08
4 changed files with 61 additions and 5 deletions

View File

@@ -582,6 +582,11 @@ await sock.sendMessage(
await sock.updateBlockStatus("xyz@s.whatsapp.net", "block") // Block user
await sock.updateBlockStatus("xyz@s.whatsapp.net", "unblock") // Unblock user
```
- To get a business profile, such as description, category
```ts
const profile = await sock.getBusinessProfile("xyz@s.whatsapp.net")
console.log("business description: " + profile.description + ", category: " + profile.category)
```
Of course, replace ``` xyz ``` with an actual ID.
## Groups
@@ -627,6 +632,11 @@ Of course, replace ``` xyz ``` with an actual ID.
const code = await sock.groupInviteCode("abcd-xyz@g.us")
console.log("group code: " + code)
```
- To revoke the invite code in a group
```ts
const code = await sock.groupRevokeInvite("abcd-xyz@g.us")
console.log("New group code: " + code)
```
- To query the metadata of a group
``` ts
const metadata = await sock.groupMetadata("abcd-xyz@g.us")

View File

@@ -1,4 +1,4 @@
import { SocketConfig, WAPresence, PresenceData, Chat, WAPatchCreate, WAMediaUpload, ChatMutation, WAPatchName, AppStateChunk, LTHashState, ChatModification, Contact } from "../Types";
import { SocketConfig, WAPresence, PresenceData, Chat, WAPatchCreate, WAMediaUpload, ChatMutation, WAPatchName, AppStateChunk, LTHashState, ChatModification, Contact, WABusinessProfile, WABusinessHoursConfig } from "../Types";
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, jidNormalizedUser, S_WHATSAPP_NET, reduceBinaryNodeToDictionary } from "../WABinary";
import { proto } from '../../WAProto'
import { generateProfilePicture, toNumber, encodeSyncdPatch, decodePatches, extractSyncdPatches, chatModificationToAppPatch, decodeSyncdSnapshot, newLTHashState } from "../Utils";
@@ -159,6 +159,50 @@ export const makeChatsSocket = (config: SocketConfig) => {
})
}
const getBusinessProfile = async (jid: string): Promise<WABusinessProfile | void> => {
const results = await query({
tag: 'iq',
attrs: {
to: 's.whatsapp.net',
xmlns: 'w:biz',
type: 'get'
},
content: [{
tag: 'business_profile',
attrs: { v: '244' },
content: [{
tag: 'profile',
attrs: { jid }
}]
}]
})
const profiles = getBinaryNodeChild(getBinaryNodeChild(results, 'business_profile'), 'profile')
if (!profiles) {
// if not bussines
if (logger.level == 'trace') logger.trace({ jid }, 'Not bussines')
return
}
const address = getBinaryNodeChild(profiles, 'address')
const description = getBinaryNodeChild(profiles, 'description')
const website = getBinaryNodeChild(profiles, 'website')
const email = getBinaryNodeChild(profiles, 'email')
const category = getBinaryNodeChild(getBinaryNodeChild(profiles, 'categories'), 'category')
const business_hours = getBinaryNodeChild(profiles, 'business_hours')
const business_hours_config = business_hours && getBinaryNodeChildren(business_hours, 'business_hours_config')
return {
wid: profiles.attrs?.jid,
address: address?.content.toString(),
description: description?.content.toString(),
website: [website?.content.toString()],
email: email?.content.toString(),
category: category?.content.toString(),
business_hours: {
timezone: business_hours?.attrs?.timezone,
business_config: business_hours_config?.map(({ attrs }) => attrs as unknown as WABusinessHoursConfig)
}
} as unknown as WABusinessProfile
}
const updateAccountSyncTimestamp = async(fromTimestamp: number | string) => {
logger.info({ fromTimestamp }, 'requesting account sync')
await sendNode({
@@ -621,8 +665,9 @@ export const makeChatsSocket = (config: SocketConfig) => {
fetchStatus,
updateProfilePicture,
updateBlockStatus,
getBusinessProfile,
resyncAppState,
chatModify,
resyncMainAppState,
}
}
}

View File

@@ -43,7 +43,7 @@ export type WAInitResponse = {
status: 200
}
type WABusinessHoursConfig = {
export type WABusinessHoursConfig = {
day_of_week: string
mode: string
open_time?: number
@@ -53,7 +53,7 @@ export type WABusinessProfile = {
description: string
email: string
business_hours: {
timezone: string
timezone?: string
config?: WABusinessHoursConfig[]
business_config?: WABusinessHoursConfig[]
}
@@ -65,4 +65,5 @@ export type WABusinessProfile = {
wid?: string
}
export type CurveKeyPair = { private: Uint8Array; public: Uint8Array }

View File

@@ -253,7 +253,7 @@ export const generateWAMessageContent = async(
if(contactLen === 1) {
m.contactMessage = WAProto.ContactMessage.fromObject(message.contacts.contacts[0])
} else {
m.contactsArrayMessage = WAProto.ContactsArrayMessage.fromObject({ contacts: message.contacts })
m.contactsArrayMessage = WAProto.ContactsArrayMessage.fromObject(message.contacts)
}
} else if('location' in message) {
m.locationMessage = WAProto.LocationMessage.fromObject(message.location)