From 0b5d772b081804c6c2e232700a8c1b8ca9d62ff6 Mon Sep 17 00:00:00 2001 From: BochilGaming <79433517+BochilGaming@users.noreply.github.com> Date: Thu, 6 Jan 2022 23:45:52 +0700 Subject: [PATCH] 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 --- README.md | 10 +++++++++ src/Socket/chats.ts | 49 +++++++++++++++++++++++++++++++++++++++++-- src/Types/index.ts | 5 +++-- src/Utils/messages.ts | 2 +- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 06eda13..c755d60 100644 --- a/README.md +++ b/README.md @@ -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") diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 91a9ee1..6b55ec5 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -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 => { + 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, } -} +} \ No newline at end of file diff --git a/src/Types/index.ts b/src/Types/index.ts index 9dbc635..1edee43 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -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 } \ No newline at end of file diff --git a/src/Utils/messages.ts b/src/Utils/messages.ts index bd91f93..cbb6dab 100644 --- a/src/Utils/messages.ts +++ b/src/Utils/messages.ts @@ -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)