evolution-api-sdk
Version:
Unofficial SDK for the Evolution Whatsapp API v2
765 lines (755 loc) • 22.1 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/modules/index.ts
var modules_exports = {};
__export(modules_exports, {
ChatsModule: () => ChatsModule,
GroupsModule: () => GroupsModule,
InstanceModule: () => InstanceModule,
MessagesModule: () => MessagesModule,
ProfileModule: () => ProfileModule,
SettingsModule: () => SettingsModule,
WebhookModule: () => WebhookModule
});
module.exports = __toCommonJS(modules_exports);
// src/api/routes.ts
var Routes = {
Message: {
SendText: "message/sendText",
SendMedia: "message/sendMedia",
SendVoice: "message/sendWhatsAppAudio",
SendSticker: "message/sendSticker",
SendLocation: "message/sendLocation",
SendContact: "message/sendContact",
SendPoll: "message/sendPoll",
SendReaction: "message/sendReaction",
SendTemplate: "message/sendTemplate",
SendStatus: "message/sendStatus",
SendList: "message/sendList"
},
Chats: {
Check: "chat/whatsappNumbers",
FindAll: "chat/findChats",
SendPresence: "chat/sendPresence",
MarkAsRead: "chat/markMessageAsRead",
MarkAsUnread: "chat/markChatUnread",
Archive: "chat/archive",
DeleteMessage: "chat/deleteMessageForEveryone",
FetchProfilePicture: "chat/fetchProfilePictureUrl",
FindContacts: "chat/findContacts",
FindMessages: "chat/findMessages",
FindStatusMessage: "chat/findStatusMessage",
UpdateMessage: "chat/updateMessage"
},
Groups: {
FindAll: "group/fetchAllGroups",
FindByJid: "group/findGroupInfos",
FindByInviteCode: "group/inviteInfo",
Create: "group/create",
UpdatePicture: "group/updateGroupPicture",
UpdateSubject: "group/updateGroupSubject",
UpdateDescription: "group/updateGroupDescription",
FetchInviteCode: "group/fetchInviteCode",
AcceptInviteCode: "group/acceptInviteCode",
RevokeInviteCode: "group/revokeInviteCode",
SendGroupInvite: "group/sendGroupInvite",
FindMembers: "group/findGroupMembers",
UpdateMembers: "group/updateGroupMembers",
UpdateSetting: "group/updateGroupSetting",
ToggleEphemeral: "group/toggleEphemeral",
Leave: "group/leaveGroup"
},
Profile: {
FetchBusinessProfile: "chat/fetchBusinessProfile",
FetchProfile: "chat/fetchProfile",
UpdateName: "chat/updateProfileName",
UpdateStatus: "chat/updateProfileStatus",
UpdatePicture: "chat/updateProfilePicture",
RemovePicture: "chat/removeProfilePicture",
FetchPrivacySettings: "chat/fetchPrivacySettings",
UpdatePrivacySettings: "chat/updatePrivacySettings"
},
Webhook: {
Set: "webhook/set",
Find: "webhook/find"
},
Settings: {
Set: "settings/set",
Find: "settings/find"
},
Instance: {
Create: "instance/create",
FetchAll: "instance/fetchInstances",
Connect: "instance/connect",
Restart: "instance/restart",
ConnectionState: "instance/connectionState",
Logout: "instance/logout",
Delete: "instance/delete",
SetPresence: "instance/setPresence"
}
};
// src/schemas/common.ts
var import_libphonenumber_js = require("libphonenumber-js");
var validateJid = (value) => value.endsWith("@s.whatsapp.net");
var validateGroupJid = (value) => value.endsWith("@g.us");
// src/modules/chats/index.ts
var ChatsModule = class {
constructor(api) {
this.api = api;
}
/**
* Checks if phone numbers are registered on WhatsApp
* @param numbers - Array of phone numbers to check
* @param methodOptions - Method-specific options (instance override)
*/
async check(numbers, methodOptions) {
const body = {
numbers: Array.isArray(numbers) ? numbers : [numbers]
};
const response = await this.api.post(Routes.Chats.Check, {
body,
...methodOptions
});
return response;
}
/**
* Gets all chats
* @param methodOptions - Method-specific options (instance override)
*/
async findAll(methodOptions) {
const response = await this.api.get(Routes.Chats.FindAll, methodOptions);
return response;
}
/**
* Updates presence status
* @param params - Presence parameters
* @param methodOptions - Method-specific options (instance override)
*/
async updatePresence(options, methodOptions) {
if (!options.number) {
throw new Error("Number is required");
}
if (!validateJid(options.number) && !validateGroupJid(options.number)) {
options.number = `${options.number}.whatsapp.net`;
}
await this.api.post(Routes.Chats.SendPresence, {
body: options,
...methodOptions
});
}
/**
* Marks messages as read
* @param options - Mark as read options
* @param methodOptions - Method-specific options (instance override)
*/
async markAsRead(options, methodOptions) {
const response = await this.api.post(Routes.Chats.MarkAsRead, {
body: options,
...methodOptions
});
return response;
}
/**
* Marks messages as unread
* @param options - Mark as unread options
* @param methodOptions - Method-specific options (instance override)
*/
async markAsUnread(options, methodOptions) {
const response = await this.api.post(Routes.Chats.MarkAsUnread, {
body: options,
...methodOptions
});
return response;
}
/**
* Archives a chat
* @param options - Archive options
* @param methodOptions - Method-specific options (instance override)
*/
async archive(options, methodOptions) {
const response = await this.api.post(Routes.Chats.Archive, {
body: options,
...methodOptions
});
return response;
}
/**
* Deletes a message
* @param options - Delete message options
* @param methodOptions - Method-specific options (instance override)
*/
async deleteMessage(options, methodOptions) {
const response = await this.api.delete(Routes.Chats.DeleteMessage, {
body: options,
...methodOptions
});
return response;
}
/**
* Fetches profile picture
* @param options - Fetch profile picture options
* @param methodOptions - Method-specific options (instance override)
*/
async fetchProfilePicture(options, methodOptions) {
const response = await this.api.post(Routes.Chats.FetchProfilePicture, {
body: options,
...methodOptions
});
return response;
}
/**
* Finds contacts
* @param options - Find contacts options
* @param methodOptions - Method-specific options (instance override)
*/
async findContacts(options, methodOptions) {
const response = await this.api.post(Routes.Chats.FindContacts, {
body: options,
...methodOptions
});
return response;
}
/**
* Finds messages
* @param options - Find messages options
* @param methodOptions - Method-specific options (instance override)
*/
async findMessages(options, methodOptions) {
const response = await this.api.post(Routes.Chats.FindMessages, {
body: options,
...methodOptions
});
return response;
}
/**
* Finds status messages
* @param options - Find status message options
* @param methodOptions - Method-specific options (instance override)
*/
async findStatusMessage(options, methodOptions) {
const response = await this.api.post(Routes.Chats.FindStatusMessage, {
body: options,
...methodOptions
});
return response;
}
/**
* Updates a message
* @param options - Update message options
* @param methodOptions - Method-specific options (instance override)
*/
async updateMessage(options, methodOptions) {
const response = await this.api.put(Routes.Chats.UpdateMessage, {
body: options,
...methodOptions
});
return response;
}
};
// src/modules/groups/index.ts
var GroupsModule = class {
constructor(api) {
this.api = api;
}
async findAll(getParticipants = false, methodOptions) {
const response = await this.api.get(Routes.Groups.FindAll, {
params: { getParticipants },
...methodOptions
});
if (getParticipants) {
return response;
}
return response;
}
/**
* Gets a group by invite code
* @param inviteCode - The group invite code (not the URL)
* @param methodOptions - Method-specific options (instance override)
*/
async findByInviteCode(inviteCode, methodOptions) {
const response = await this.api.get(Routes.Groups.FindByInviteCode, {
params: { inviteCode },
...methodOptions
});
return response;
}
/**
* Gets a group by JID
* @param groupJid - The group JID terminated with \@g.us
* @param methodOptions - Method-specific options (instance override)
*/
async findByJid(groupJid, methodOptions) {
const response = await this.api.get(Routes.Groups.FindByJid, {
params: { groupJid },
...methodOptions
});
return response;
}
async create(options, methodOptions) {
const response = await this.api.post(Routes.Groups.Create, {
body: options,
...methodOptions
});
return response;
}
async updatePicture(options, methodOptions) {
const response = await this.api.post(Routes.Groups.UpdatePicture, {
body: options,
...methodOptions
});
return response;
}
async updateSubject(options, methodOptions) {
const response = await this.api.post(Routes.Groups.UpdateSubject, {
body: options,
...methodOptions
});
return response;
}
async updateDescription(options, methodOptions) {
const response = await this.api.post(Routes.Groups.UpdateDescription, {
body: options,
...methodOptions
});
return response;
}
async fetchInviteCode(options, methodOptions) {
const response = await this.api.get(Routes.Groups.FetchInviteCode, {
params: options,
...methodOptions
});
return response;
}
async acceptInviteCode(options, methodOptions) {
const response = await this.api.post(Routes.Groups.AcceptInviteCode, {
body: options,
...methodOptions
});
return response;
}
async revokeInviteCode(options, methodOptions) {
const response = await this.api.post(Routes.Groups.RevokeInviteCode, {
body: options,
...methodOptions
});
return response;
}
async sendGroupInvite(options, methodOptions) {
const response = await this.api.post(Routes.Groups.SendGroupInvite, {
body: options,
...methodOptions
});
return response;
}
async findMembers(options, methodOptions) {
const response = await this.api.get(Routes.Groups.FindMembers, {
params: options,
...methodOptions
});
return response;
}
async updateMembers(options, methodOptions) {
const response = await this.api.post(Routes.Groups.UpdateMembers, {
body: options,
...methodOptions
});
return response;
}
async updateSetting(options, methodOptions) {
const response = await this.api.post(Routes.Groups.UpdateSetting, {
body: options,
...methodOptions
});
return response;
}
async toggleEphemeral(options, methodOptions) {
const response = await this.api.post(Routes.Groups.ToggleEphemeral, {
body: options,
...methodOptions
});
return response;
}
async leave(options, methodOptions) {
const response = await this.api.post(Routes.Groups.Leave, {
body: options,
...methodOptions
});
return response;
}
};
// src/modules/instance/index.ts
var InstanceModule = class {
constructor(api) {
this.api = api;
}
async create(options) {
const response = await this.api.post(Routes.Instance.Create, {
body: options,
isInstanceUrl: false
});
return response;
}
async connect(options, methodOptions) {
const { instanceName } = options;
const instance = methodOptions?.instance ?? instanceName;
if (!instance) {
throw new Error("Instance name is required");
}
const response = await this.api.get(Routes.Instance.Connect, {
instance,
isInstanceUrl: true
});
return response;
}
async connectionState(options, methodOptions) {
const { instanceName } = options;
const instance = methodOptions?.instance ?? instanceName;
if (!instance) {
throw new Error("Instance name is required");
}
const response = await this.api.get(Routes.Instance.ConnectionState, {
instance,
isInstanceUrl: true
});
return response;
}
async logout(options, methodOptions) {
const { instanceName } = options;
const instance = methodOptions?.instance ?? instanceName;
if (!instance) {
throw new Error("Instance name is required");
}
const response = await this.api.delete(Routes.Instance.Logout, {
instance,
isInstanceUrl: true
});
return response;
}
async delete(options) {
const response = await this.api.delete(Routes.Instance.Delete, {
instance: options.instanceName,
isInstanceUrl: true
});
return response;
}
async restart(options, methodOptions) {
const { instanceName } = options;
const instance = methodOptions?.instance ?? instanceName;
if (!instance) {
throw new Error("Instance name is required");
}
const response = await this.api.post(Routes.Instance.Restart, {
instance,
isInstanceUrl: true
});
return response;
}
async fetchAll(options) {
const response = await this.api.get(Routes.Instance.FetchAll, {
params: options,
isInstanceUrl: false
});
return response;
}
async setPresence(options, methodOptions) {
const { instanceName, ...rest } = options;
const instance = methodOptions?.instance ?? instanceName;
if (!instance) {
throw new Error("Instance name is required");
}
const response = await this.api.post(Routes.Instance.SetPresence, {
body: rest,
instance,
isInstanceUrl: true
});
return response;
}
};
// src/modules/messages/index.ts
var MessagesModule = class {
constructor(api) {
this.api = api;
}
/**
* Sends a text message
* @param options - Text message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendText(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendText, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends an image
* @param options - Image message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendImage(options, methodOptions) {
options.mediatype = "image";
const response = await this.api.post(Routes.Message.SendMedia, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a video
* @param options - Video message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendVideo(options, methodOptions) {
options.mediatype = "video";
const response = await this.api.post(Routes.Message.SendMedia, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a document
* @param options - Document message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendDocument(options, methodOptions) {
options.mediatype = "document";
const response = await this.api.post(Routes.Message.SendMedia, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends an audio
* @param options - Audio message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendAudio(options, methodOptions) {
options.mediatype = "audio";
const response = await this.api.post(Routes.Message.SendMedia, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a voice message
* @param options - Voice message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendVoice(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendVoice, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a sticker
* @param options - Sticker message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendSticker(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendSticker, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a location
* @param options - Location message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendLocation(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendLocation, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a contact
* @param options - Contact message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendContact(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendContact, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a reaction
* @param options - Reaction message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendReaction(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendReaction, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a template
* @param options - Template message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendTemplate(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendTemplate, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a status
* @param options - Status message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendStatus(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendStatus, {
body: options,
...methodOptions
});
return response;
}
/**
* Sends a list
* @param options - List message options
* @param methodOptions - Method-specific options (instance override)
*/
async sendList(options, methodOptions) {
const response = await this.api.post(Routes.Message.SendList, {
body: options,
...methodOptions
});
return response;
}
};
// src/modules/profile/index.ts
var ProfileModule = class {
constructor(api) {
this.api = api;
}
async fetchBusinessProfile(options, methodOptions) {
const response = await this.api.post(Routes.Profile.FetchBusinessProfile, {
body: options,
...methodOptions
});
return response;
}
async fetchProfile(options, methodOptions) {
const response = await this.api.post(Routes.Profile.FetchProfile, {
body: options,
...methodOptions
});
return response;
}
async updateName(options, methodOptions) {
const response = await this.api.post(Routes.Profile.UpdateName, {
body: options,
...methodOptions
});
return response;
}
async updateStatus(options, methodOptions) {
const response = await this.api.post(Routes.Profile.UpdateStatus, {
body: options,
...methodOptions
});
return response;
}
async updatePicture(options, methodOptions) {
const response = await this.api.post(Routes.Profile.UpdatePicture, {
body: options,
...methodOptions
});
return response;
}
async removePicture(methodOptions) {
const response = await this.api.delete(
Routes.Profile.RemovePicture,
methodOptions
);
return response;
}
async fetchPrivacySettings(methodOptions) {
const response = await this.api.get(
Routes.Profile.FetchPrivacySettings,
methodOptions
);
return response;
}
async updatePrivacySettings(options, methodOptions) {
const response = await this.api.put(Routes.Profile.UpdatePrivacySettings, {
body: options,
...methodOptions
});
return response;
}
};
// src/modules/settings/index.ts
var SettingsModule = class {
constructor(api) {
this.api = api;
}
async set(options, methodOptions) {
const response = await this.api.post(Routes.Settings.Set, {
body: options,
...methodOptions
});
return response;
}
async find(methodOptions) {
const response = await this.api.get(Routes.Settings.Find, methodOptions);
return response;
}
};
// src/modules/webhook/index.ts
var WebhookModule = class {
constructor(api) {
this.api = api;
}
async set(options, methodOptions) {
const response = await this.api.post(Routes.Webhook.Set, {
body: options,
...methodOptions
});
return response;
}
async find(methodOptions) {
const response = await this.api.get(Routes.Webhook.Find, methodOptions);
return response;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ChatsModule,
GroupsModule,
InstanceModule,
MessagesModule,
ProfileModule,
SettingsModule,
WebhookModule
});
//# sourceMappingURL=index.js.map