Comparing version
declare class GzappyClient { | ||
private userTokenId; | ||
private instanceId; | ||
private instanceToken; | ||
private baseURL; | ||
private api; | ||
constructor({ userTokenId, instanceId, instanceToken }: GzappyClientOptions); | ||
sendMessage(messages: string[], phones: string[]): Promise<{ | ||
data: any; | ||
error?: undefined; | ||
} | { | ||
error: string; | ||
data?: undefined; | ||
}>; | ||
sendMedia(message: string, mediaUrl: string, phones: string[]): Promise<{ | ||
data: any; | ||
error?: undefined; | ||
} | { | ||
error: string; | ||
data?: undefined; | ||
}>; | ||
sendGroupMessage(messages: string[], groups: string[]): Promise<{ | ||
data: any; | ||
error?: undefined; | ||
} | { | ||
error: string; | ||
data?: undefined; | ||
}>; | ||
scheduleMessage(messages: string[], phones: string[], scheduleUtcDate?: string): Promise<{ | ||
data: any; | ||
error?: undefined; | ||
} | { | ||
error: string; | ||
data?: undefined; | ||
}>; | ||
constructor({ userTokenId, instanceId }: GzappyClientOptions); | ||
sendMessage(messages: string[], phones: string[]): Promise<any>; | ||
sendMedia(message: string, mediaUrl: string, phones: string[]): Promise<any>; | ||
sendGroupMessage(messages: string[], groups: string[]): Promise<any>; | ||
scheduleMessage(messages: string[], phones: string[], scheduleUtcDate?: string): Promise<any>; | ||
} | ||
export { GzappyClient as default }; |
@@ -40,9 +40,13 @@ "use strict"; | ||
instanceId; | ||
instanceToken; | ||
baseURL; | ||
api; | ||
constructor({ userTokenId, instanceId, instanceToken }) { | ||
constructor({ userTokenId, instanceId }) { | ||
if (!userTokenId) { | ||
throw new Error("userTokenId for gzappy-js is required"); | ||
} | ||
if (!instanceId) { | ||
throw new Error("instanceId for gzappy-js is required"); | ||
} | ||
this.userTokenId = userTokenId; | ||
this.instanceId = instanceId; | ||
this.instanceToken = instanceToken; | ||
this.baseURL = "https://api.gzappy.com/v1"; | ||
@@ -58,20 +62,43 @@ this.api = import_axios.default.create({ | ||
async sendMessage(messages, phones) { | ||
if (messages.some((message) => message === "")) { | ||
return { error: "Message cannot be empty" }; | ||
} | ||
if (phones.some((phone) => phone === "")) { | ||
return { error: "Phone number cannot be empty" }; | ||
} | ||
if (messages.some((message) => message.length > 5e3)) { | ||
return { error: "Message cannot be more than 5000 characters" }; | ||
} | ||
if (messages.length > 3) { | ||
return { error: "You can only send 3 messages at a time" }; | ||
} | ||
if (phones.length > 5) { | ||
return { error: "You can only send to 5 numbers at a time" }; | ||
} | ||
if (phones.some( | ||
(phone) => phone.replace(/\D/g, "").length < 12 || phone.replace(/\D/g, "").length > 15 | ||
)) { | ||
return { error: "Phone number is invalid" }; | ||
} | ||
try { | ||
const response = await this.api.post("/message/send-message", { | ||
instance_id: this.instanceId, | ||
instance_token: this.instanceToken, | ||
message: messages, | ||
phone: phones | ||
}); | ||
return { data: response.data }; | ||
return response.data; | ||
} catch (error) { | ||
console.error(error); | ||
return { error: "Error sending message" }; | ||
return error.data; | ||
} | ||
} | ||
async sendMedia(message, mediaUrl, phones) { | ||
if (message === "") { | ||
return { error: "Message cannot be empty" }; | ||
} | ||
if (phones.some((phone) => phone === "")) { | ||
return { error: "Phone number cannot be empty" }; | ||
} | ||
try { | ||
const response = await this.api.post("/message/send-media", { | ||
instance_id: this.instanceId, | ||
instance_token: this.instanceToken, | ||
message, | ||
@@ -81,27 +108,35 @@ mediaUrl, | ||
}); | ||
return { data: response.data }; | ||
return response.data; | ||
} catch (error) { | ||
console.error(error); | ||
return { error: "Error sending message" }; | ||
return error.data; | ||
} | ||
} | ||
async sendGroupMessage(messages, groups) { | ||
if (messages.some((message) => message === "")) { | ||
return { error: "Message cannot be empty" }; | ||
} | ||
if (groups.some((group) => group === "")) { | ||
return { error: "Group number cannot be empty" }; | ||
} | ||
try { | ||
const response = await this.api.post("/message/send-group-message", { | ||
instance_id: this.instanceId, | ||
instance_token: this.instanceToken, | ||
message: messages, | ||
group: groups | ||
}); | ||
return { data: response.data }; | ||
return response.data; | ||
} catch (error) { | ||
console.error(error); | ||
return { error: "Error sending message" }; | ||
return error.data; | ||
} | ||
} | ||
async scheduleMessage(messages, phones, scheduleUtcDate) { | ||
if (messages.some((message) => message === "")) { | ||
return { error: "Message cannot be empty" }; | ||
} | ||
if (phones.some((phone) => phone === "")) { | ||
return { error: "Phone number cannot be empty" }; | ||
} | ||
try { | ||
const response = await this.api.post("/message/schedule-message", { | ||
instance_id: this.instanceId, | ||
instance_token: this.instanceToken, | ||
message: messages, | ||
@@ -111,6 +146,5 @@ phone: phones, | ||
}); | ||
return { data: response.data }; | ||
return response.data; | ||
} catch (error) { | ||
console.error(error); | ||
return { error: "Error sending message" }; | ||
return error.data; | ||
} | ||
@@ -117,0 +151,0 @@ } |
{ | ||
"name": "gzappy-js", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "GZAPPY-JS é um pacote JavaScript que facilita a integração com a API de WhatsApp da GZAPPY.", | ||
@@ -15,3 +15,4 @@ "main": "dist/index.js", | ||
"lint:check": "prettier --check .", | ||
"lint:fix": "prettier --write ." | ||
"lint:fix": "prettier --write .", | ||
"npm:deploy": "npm run build && npm publish" | ||
}, | ||
@@ -18,0 +19,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14051
9.63%278
18.3%