diff --git a/index.d.ts b/index.d.ts index 54e82aa..560a94c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -197,6 +197,12 @@ declare namespace WAWebJS { /** Sync history conversation of the Chat */ syncHistory(chatId: string): Promise + + /** Save new contact to user's addressbook or edit the existing one */ + saveOrEditAddressbookContact(phoneNumber: string, firstName: string, lastName: string, syncToAddressbook?: boolean): Promise + + /** Deletes the contact from user's addressbook */ + deleteAddressbookContact(honeNumber: string): Promise /** Changes and returns the archive state of the Chat */ unarchiveChat(chatId: string): Promise diff --git a/src/Client.js b/src/Client.js index 3f339ce..e6de355 100644 --- a/src/Client.js +++ b/src/Client.js @@ -1773,6 +1773,39 @@ class Client extends EventEmitter { return false; }, chatId); } + + /** + * Save new contact to user's addressbook or edit the existing one + * @param {string} phoneNumber The contact's phone number in a format "17182222222", where "1" is a country code + * @param {string} firstName + * @param {string} lastName + * @param {boolean} [syncToAddressbook = false] If set to true, the contact will also be saved to the user's address book on their phone. False by default + * @returns {Promise} Object in a wid format + */ + async saveOrEditAddressbookContact(phoneNumber, firstName, lastName, syncToAddressbook = false) + { + return await this.pupPage.evaluate(async (phoneNumber, firstName, lastName, syncToAddressbook) => { + return await window.Store.AddressbookContactUtils.saveContactAction( + phoneNumber, + null, + firstName, + lastName, + syncToAddressbook + ); + }, phoneNumber, firstName, lastName, syncToAddressbook); + } + + /** + * Deletes the contact from user's addressbook + * @param {string} phoneNumber The contact's phone number in a format "17182222222", where "1" is a country code + * @returns {Promise} + */ + async deleteAddressbookContact(phoneNumber) + { + return await this.pupPage.evaluate(async (phoneNumber) => { + return await window.Store.AddressbookContactUtils.deleteContactAction(phoneNumber); + }, phoneNumber); + } } module.exports = Client; diff --git a/src/util/Injected/Store.js b/src/util/Injected/Store.js index 84e3c37..fa65252 100644 --- a/src/util/Injected/Store.js +++ b/src/util/Injected/Store.js @@ -141,6 +141,10 @@ exports.ExposeStore = () => { ...window.require('WAWebApiMembershipApprovalRequestStore'), ...window.require('WASmaxGroupsMembershipRequestsActionRPC') }; + window.Store.AddressbookContactUtils = { + ...window.require('WAWebSaveContactAction'), + ...window.require('WAWebDeleteContactAction') + }; if (!window.Store.Chat._find || !window.Store.Chat.findImpl) { window.Store.Chat._find = e => {