|
1 | 1 | import { whatsappClient } from './utils/client' |
2 | | -import { AudioMessage, TextMessage } from '@wapijs/wapi.js' |
| 2 | +import { AudioMessage, ListInteractionMessage, TextMessage } from '@wapijs/wapi.js' |
3 | 3 |
|
4 | 4 | function init() { |
5 | 5 | whatsappClient.on('Ready', () => { |
6 | 6 | console.log('Client is ready') |
7 | 7 | }) |
8 | 8 |
|
| 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 | + |
9 | 83 | whatsappClient.on('TextMessage', async message => { |
10 | 84 | 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 |
14 | 89 | }) |
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 | + }) |
19 | 96 | }) |
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 |
21 | 108 |
|
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 | + }) |
23 | 119 |
|
24 | | - console.log(response) |
25 | 120 | }) |
26 | 121 |
|
27 | 122 | whatsappClient.on('Error', error => { |
|
0 commit comments