Skip to content

Commit 95d01d4

Browse files
committed
fix:[skip ci] add example bot list message interaction things and other wapi.js bug fixes
Signed-off-by: sarthakjdev <jsarthak448@gmail.com>
1 parent 2610c66 commit 95d01d4

4 files changed

Lines changed: 781 additions & 125 deletions

File tree

packages/example-chat-bot/src/index.ts

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,122 @@
11
import { whatsappClient } from './utils/client'
2-
import { AudioMessage, TextMessage } from '@wapijs/wapi.js'
2+
import { AudioMessage, ListInteractionMessage, TextMessage } from '@wapijs/wapi.js'
33

44
function init() {
55
whatsappClient.on('Ready', () => {
66
console.log('Client is ready')
77
})
88

9+
const faq = {
10+
"What is wapijs": [
11+
{
12+
question: "What is wapi.js?",
13+
answer: "wapi.js is a Tyepscript library for building WhatsApp chatbots."
14+
},
15+
{
16+
question: "Main features of wapi.js?",
17+
answer: "Object-oriented design, single client, easy messaging, event handling, media upload."
18+
},
19+
{
20+
question: "Can I build AI chatbots with wapi.js? ewfjbesjkfbewjfgbjwekfknwekjnfwefwefwef",
21+
answer: "wapi.js itself doesn't have AI, but you can integrate with NLU services."
22+
},
23+
],
24+
"Getting Started": [
25+
{
26+
question: "How do I get started with wapi.js?",
27+
answer: "Check out the docs at https://wapijs.co/docs and use the \"create-wapi-bot\" template."
28+
},
29+
{
30+
question: "Is wapi.js easy to learn?",
31+
answer: "Yes, designed for all levels. Docs and examples help you get started quickly."
32+
},
33+
],
34+
"Capabilities": [
35+
{
36+
question: "What kind of chatbots can I build?",
37+
answer: "Customer support, marketing, notifications, and more! Leverage WhatsApp Business API."
38+
},
39+
{
40+
question: "Can I integrate wapi.js with other systems?",
41+
answer: "Absolutely! Integrate with existing backend system."
42+
},
43+
{
44+
question: "Are there examples of chatbots built with wapi.js?",
45+
answer: "It's in beta, so not many yet. Be among the first to build and share yours!"
46+
}
47+
],
48+
"Help & Support": [
49+
{
50+
question: "Is wapi.js free and open-source?",
51+
answer: "Yes, it's completely free and open-source under the Apache 2.0 License."
52+
},
53+
{
54+
question: "Where can I get help or support for wapi.js?",
55+
answer: "Create an issue on our GitHub repository: https://github.com/sarthakjdev/wapi.js/issues."
56+
},
57+
]
58+
}
59+
60+
61+
62+
63+
64+
const listMessage = new ListInteractionMessage({
65+
bodyText: 'Welcome to Wapi.js',
66+
buttonText: 'Ask questions',
67+
footerText: 'Beta version',
68+
sections: Object.keys(faq).map((section, sectionIndex) => {
69+
return {
70+
// @ts-ignore
71+
rows: faq[section].map((question, index) => {
72+
return {
73+
description: question.question,
74+
id: `section-${sectionIndex + 1}-question-${index + 1}`,
75+
title: `FAQ ${index + 1}`
76+
}
77+
}),
78+
title: section
79+
}
80+
})
81+
})
82+
983
whatsappClient.on('TextMessage', async message => {
1084
console.log('Text Message')
11-
const response = await message.reply({
12-
message: new TextMessage({
13-
text: 'Hello, World!'
85+
if (message.text.data.text.toLowerCase() === 'hello') {
86+
const response = await message.client.message.send({
87+
message: listMessage,
88+
phoneNumber: message.context.from
1489
})
15-
})
16-
const audioResponse = await message.reply({
17-
message: new AudioMessage({
18-
link: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3'
90+
console.log({ response: JSON.stringify(response) })
91+
} else {
92+
await message.reply({
93+
message: new TextMessage({
94+
text: 'Please say "hello" to proceed.'
95+
})
1996
})
20-
})
97+
}
98+
})
99+
100+
whatsappClient.on('ListInteraction', async (message) => {
101+
console.log('List Interaction', message)
102+
103+
// it would be something like : section-1-question-1
104+
const messageListId = message.listId
105+
106+
const sectionIndex = parseInt(messageListId.split('-')[1]) - 1
107+
const questionIndex = parseInt(messageListId.split('-')[3]) - 1
21108

22-
console.log({ audioResponse })
109+
console.log({ messageListId, sectionIndex, questionIndex })
110+
111+
// @ts-ignore
112+
const answerToReply = faq[Object.keys(faq)[sectionIndex]][questionIndex].answer
113+
114+
await message.reply({
115+
message: new TextMessage({
116+
text: answerToReply
117+
})
118+
})
23119

24-
console.log(response)
25120
})
26121

27122
whatsappClient.on('Error', error => {

packages/wapi.js/src/api-request-payload-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ export const ListInteractiveMessageSection = z.object({
343343
z.object({
344344
id: z.string(),
345345
title: z.string(),
346-
description: z.string(),
346+
description: z.string().max(72, "Max length of a row description in list interaction message, can be 72 only"),
347347
}),
348348
),
349349
});

0 commit comments

Comments
 (0)