Skip to content

Commit 5809e4f

Browse files
authored
Merge pull request #246 from Secreto31126/show-yourself
Added reply method to OnMessage callback
2 parents 4859426 + de200ce commit 5809e4f

File tree

6 files changed

+71
-18
lines changed

6 files changed

+71
-18
lines changed

ENVIRONMENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ const Whatsapp = new WhatsAppAPI({
203203
}
204204
});
205205

206-
Whatsapp.on.message = ({ phoneID, from, name }) => {
207-
Whatsapp.sendMessage(phoneID, from, new Text(`Hello ${name}!`));
206+
Whatsapp.on.message = ({ name, reply }) => {
207+
reply(new Text(`Hello ${name}!`));
208208
};
209209

210210
async function doPost(e) {

README.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async function post(e) {
5555
);
5656
}
5757

58-
Whatsapp.on.message = async ({ phoneID, from, message, name }) => {
58+
Whatsapp.on.message = async ({ phoneID, from, message, name, reply }) => {
5959
console.log(
6060
`User ${name} (${from}) sent to bot ${phoneID} ${JSON.stringify(
6161
message
@@ -65,34 +65,26 @@ Whatsapp.on.message = async ({ phoneID, from, message, name }) => {
6565
let promise;
6666

6767
if (message.type === "text") {
68-
promise = Whatsapp.sendMessage(
69-
phoneID,
70-
from,
68+
promise = reply(
7169
new Text(`*${name}* said:\n\n${message.text.body}`),
72-
message.id
70+
true
7371
);
7472
}
7573

7674
if (message.type === "image") {
77-
promise = Whatsapp.sendMessage(
78-
phoneID,
79-
from,
75+
promise = reply(
8076
new Image(message.image.id, true, `Nice photo, ${name}`)
8177
);
8278
}
8379

8480
if (message.type === "document") {
85-
promise = Whatsapp.sendMessage(
86-
phoneID,
87-
from,
81+
promise = reply(
8882
new Document(message.document.id, true, undefined, "Our document")
8983
);
9084
}
9185

9286
if (message.type === "contacts") {
93-
promise = Whatsapp.sendMessage(
94-
phoneID,
95-
from,
87+
promise = reply(
9688
new Contacts.Contacts(
9789
[
9890
new Contacts.Name(name, "First name", "Last name"),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "whatsapp-api-js",
3-
"version": "2.1.2",
3+
"version": "2.2.0",
44
"author": "Secreto31126",
55
"description": "A TypeScript server agnostic Whatsapp's Official API framework",
66
"license": "MIT",

src/emitters.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Response } from "undici";
12
import type {
23
ClientMessage,
34
ClientMessageRequest,
@@ -83,6 +84,17 @@ export type OnMessageArgs = {
8384
* The raw data from the API
8485
*/
8586
raw: PostData;
87+
/**
88+
* A method to easily reply to the user who sent the message
89+
*
90+
* @param response - The message to send as a reply
91+
* @param context - Wether to mention the current message
92+
* @returns WhatsAppAPI.sendMessage return value
93+
*/
94+
reply: (
95+
response: ClientMessage,
96+
context: boolean
97+
) => Promise<ServerMessageResponse | Response>;
8698
};
8799

88100
/**

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,14 @@ export default class WhatsAppAPI {
778778
from,
779779
message,
780780
name,
781-
raw: data
781+
raw: data,
782+
reply: (response, context = false) =>
783+
this.sendMessage(
784+
phoneID,
785+
from,
786+
response,
787+
context ? message.id : undefined
788+
)
782789
};
783790

784791
this.offload(this.on?.message, args);

test/index.test.cjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,21 @@ describe("WhatsAppAPI", function () {
16351635
});
16361636

16371637
describe("Messages", function () {
1638+
const expectedResponse = {
1639+
messaging_product: "whatsapp",
1640+
contacts: [
1641+
{
1642+
input: user,
1643+
wa_id: user
1644+
}
1645+
],
1646+
messages: [
1647+
{
1648+
id
1649+
}
1650+
]
1651+
};
1652+
16381653
let spy_on_message;
16391654
this.beforeEach(function () {
16401655
spy_on_message = sinon_spy();
@@ -1662,6 +1677,33 @@ describe("WhatsAppAPI", function () {
16621677
});
16631678
});
16641679

1680+
it("should reply to a message with the method reply", async function () {
1681+
const spy_on_sent = sinon_spy();
1682+
Whatsapp.on.sent = spy_on_sent;
1683+
1684+
clientFacebook
1685+
.intercept({
1686+
path: `/${Whatsapp.v}/${phoneID}/messages`,
1687+
method: "POST",
1688+
headers: {
1689+
Authorization: `Bearer ${token}`
1690+
}
1691+
})
1692+
.reply(200, expectedResponse)
1693+
.times(1);
1694+
1695+
Whatsapp.on.message = async ({ reply }) => {
1696+
reply(new Text("Hello World"));
1697+
};
1698+
1699+
Whatsapp.post(valid_message_mock);
1700+
1701+
// Callbacks are executed in the next tick
1702+
await new Promise((resolve) => setTimeout(resolve, 0));
1703+
1704+
sinon_assert.calledOnce(spy_on_sent);
1705+
});
1706+
16651707
it("should not block the main thread with the user's callback", async function () {
16661708
// Emulates a blocking function
16671709
function block(delay) {

0 commit comments

Comments
 (0)