forked from kokonior/PHP-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGIZI
332 lines (253 loc) · 15.8 KB
/
GIZI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import {
WAConnection,
MessageType,
Presence,
Mimetype,
GroupSettingChange,
ReconnectMode
} from '@adiwajshing/baileys';
import { writeFileSync, existsSync } from 'fs';
import { Say } from 'say';
import { Lame } from 'node-lame';
import TextToSpeechV1 from 'ibm-watson/text-to-speech/v1.js';
import { IamAuthenticator } from 'ibm-watson/auth/index.js';
import fs from 'fs';
import NodeCache from 'node-cache';
const myCache = new NodeCache();
const messageMapping = fs.readFileSync('./message.json', 'utf-8');
const textToSpeech = (textMsg) => new Promise((resolve, reject) => {
const textToSpeech = new TextToSpeechV1({
authenticator: new IamAuthenticator({ apikey: 'baHxY7LYxlFEx-_ks5NbxrCdfnvKsQHqE-ZQYzk5ZasM' }),
serviceUrl: 'https://api.us-south.text-to-speech.watson.cloud.ibm.com'
});
const params = {
text: textMsg,
voice: 'en-US_EmilyV3Voice', // Optional voice
accept: 'audio/wav'
};
textToSpeech
.synthesize(params)
.then(response => {
const audio = response.result;
return textToSpeech.repairWavHeaderStream(audio);
})
.then(async repairedFile => {
writeFileSync('output.wav', repairedFile);
const encoder = new Lame({
output: "./output.mp3",
bitrate: 192,
}).setFile("./output.wav");
await encoder
.encode()
.then(() => {
resolve(true)
// Encoding finished
})
.catch((error) => {
console.log(error)
// Something went wrong
});
console.log('audio.wav written with a corrected wav header');
})
.catch(err => {
console.log(err);
});
})
async function starts() {
const client = new WAConnection()
client.version = [2, 2140, 12];
client.autoReconnect = ReconnectMode.onConnectionLost;
client.logger.level = 'warn';
client.on('qr', (qr) => {
console.log(qr)
console.log('scan qr code above');
});
client.on('credentials-updated', () => {
writeFileSync('./auth.json', JSON.stringify(client.base64EncodedAuthInfo(), null, '\t'));
});
existsSync('./auth.json') && client.loadAuthInfo('./auth.json');
client.on('connecting', () => {
console.log('connecting')
});
client.on('open', () => {
!existsSync('./auth.json') && writeFileSync('./auth.json', JSON.stringify(client.base64EncodedAuthInfo(), null, '\t'));
console.log('connected')
});
await client.connect({ timeoutMs: 30 * 1000 });
client.on('chat-update', async (msg) => {
try {
const jsonMessageMapping = JSON.parse(messageMapping);
if (!msg.messages) return
const content = JSON.parse(JSON.stringify(msg.messages))[0];
if (content.key && content.key.remoteJid == 'status@broadcast' || !content.key.remoteJid == '6282214899172@s.whatsapp.net') return
if (content.key.fromMe) return
const from = content.key.remoteJid;
const realMessage = content.message.conversation.toLowerCase();
client.chatRead(from);
if (content.key.remoteJid.includes('s.whatsapp.net')) {
const haveSession = myCache.get(`session:${content.key.remoteJid}`);
if (!haveSession) {
await client.sendMessage(from, jsonMessageMapping.title, MessageType.text);
await client.sendMessage(
from,
{ url: './voices/voice1.mp4' },
MessageType.audio,
{ mimetype: Mimetype.mp4Audio }
);
await client.sendMessage(from, 'Silahkan tekan tombol PLAY untuk mendengarkan suara', MessageType.text);
let messageText = jsonMessageMapping.body.title + '\n\n';
for (let index = 0; index < jsonMessageMapping.body.menu.length; index++) {
const elementMenu = jsonMessageMapping.body.menu[index];
let newIndex = index + 1;
messageText += `${newIndex}. ${elementMenu.question}\n`;
}
messageText += '\n\n\n' + '99. Keluar\n' + '\n\nTerimakasih sudah menggunakan Layanan Halo Nutricia , informasi seputar Ibu Hamil KEK Puskesmas Unit I Sumbawa. Ketik SAHABAT GIZI jika anda ingin berinteraksi langsung dengan petugas Gizi Puskesmas Unit I Sumbawa';
await client.sendMessage(from, messageText, MessageType.text);
myCache.set(`session:${content.key.remoteJid}`, {
menu: 2,
answer: null,
answerIndex: null
})
} else {
const activeSahabatGizi = myCache.get(`session:sahabatgizi:${content.key.remoteJid}`);
if (!activeSahabatGizi) {
if (realMessage == 'sahabat gizi') {
await client.sendMessage(from, 'Mohon tunggu sebentar, sedang kita panggilkan petugas terbaik kita....', MessageType.text);
await client.sendMessage('120363019363293785@g.us', `Ada yang mau chatan sama petugas nih https://wa.me/${content.key.remoteJid.split('@')[0]}?text=dengan%20petugas%20gizi%20unit%201%20sumbawa,%20ada%20yang%20bisa%20dibantu?`, MessageType.text);
myCache.set(`session:sahabatgizi:${content.key.remoteJid}`, 'active');
} else if (parseInt(realMessage) == 99) {
let messageText = '';
messageText += 'Terimakasih sudah menggunakan Layanan Halo Nutricia , informasi seputar Ibu Hamil KEK Puskesmas Unit I Sumbawa. Ketik SAHABAT GIZI jika anda ingin berinteraksi langsung dengan petugas Gizi Puskesmas Unit I Sumbawa';
await client.sendMessage(from, messageText, MessageType.text);
myCache.del(`session:${content.key.remoteJid}`);
} else {
if (parseInt(realMessage) == 9) {
const isMenuAvail = jsonMessageMapping.body.menu.filter(x => x.answer.menuType == parseInt(haveSession.menu));
let messageText = isMenuAvail[0].answer.title + '\n\n';
for (let index = 0; index < isMenuAvail[0].answer.body.length; index++) {
const elementMenu = isMenuAvail[0].answer.body[index];
let newIndex = index + 1;
messageText += `${newIndex}. ${elementMenu.question}\n`;
}
messageText += '\n\n\n' + '99. Keluar\n' + `0. Kembali kemenu utama`;
await client.sendMessage(from, messageText, MessageType.text);
myCache.set(`session:${content.key.remoteJid}`, {
menu: haveSession.menu,
answer: realMessage,
answerIndex: null
});
} else if (parseInt(realMessage) == 0) {
await client.sendMessage(from, jsonMessageMapping.title, MessageType.text);
await client.sendMessage(
from,
{ url: './voices/voice1.mp4' },
MessageType.audio,
{ mimetype: Mimetype.mp4Audio }
);
await client.sendMessage(from, 'Silahkan tekan tombol PLAY untuk mendengarkan suara', MessageType.text);
let messageText = jsonMessageMapping.body.title + '\n\n';
for (let index = 0; index < jsonMessageMapping.body.menu.length; index++) {
const elementMenu = jsonMessageMapping.body.menu[index];
let newIndex = index + 1;
messageText += `${newIndex}. ${elementMenu.question}\n`;
}
messageText += '\n\n\n' + '99. Keluar\n' + '\n\nTerimakasih sudah menggunakan Layanan Halo Nutricia , informasi seputar Ibu Hamil KEK Puskesmas Unit I Sumbawa. Ketik SAHABAT GIZI jika anda ingin berinteraksi langsung dengan petugas Gizi Puskesmas Unit I Sumbawa';
await client.sendMessage(from, messageText, MessageType.text);
myCache.set(`session:${content.key.remoteJid}`, {
menu: 2,
answer: realMessage,
answerIndex: null
});
} else {
try {
const haveSession = myCache.get(`session:${content.key.remoteJid}`);
if (haveSession.menu == 2) {
let messageText = jsonMessageMapping.body.menu[parseInt(realMessage) - 1].answer.title + '\n\n';
for (let index = 0; index < jsonMessageMapping.body.menu[parseInt(realMessage) - 1].answer.body.length; index++) {
const elementMenu = jsonMessageMapping.body.menu[parseInt(realMessage) - 1].answer.body[index];
let newIndex = index + 1;
messageText += `${newIndex}. ${elementMenu.question}\n`;
}
messageText += '\n\n\n' + `0. Kembali kemenu utama` + '\n\nTerimakasih sudah menggunakan Layanan Halo Nutricia , informasi seputar Ibu Hamil KEK Puskesmas Unit I Sumbawa. Ketik SAHABAT GIZI jika anda ingin berinteraksi langsung dengan petugas Gizi Puskesmas Unit I Sumbawa';
await client.sendMessage(from, messageText, MessageType.text);
myCache.set(`session:${content.key.remoteJid}`, {
menu: jsonMessageMapping.body.menu[parseInt(realMessage) - 1].answer.menuType,
answer: '',
answerIndex: null
})
}
if (haveSession.menu > 2) {
myCache.set(`session:${content.key.remoteJid}`, {
menu: haveSession.menu,
answer: realMessage,
answerIndex: null
});
const checkHaveSession = myCache.get(`session:${content.key.remoteJid}`);
const isMenuAvail = jsonMessageMapping.body.menu.filter(x => x.answer.menuType == parseInt(checkHaveSession.menu));
if (isMenuAvail.length === 0) {
await client.sendMessage(from, 'Menu tidak tersedia\n\n' + `0. Kembali kemenu utama`, MessageType.text);
};
if (isMenuAvail.length > 0 && !checkHaveSession.answer) {
let messageText = isMenuAvail[0].answer.title + '\n\n';
messageText += '\n\n\n' + `9. Kembali ke menu ${isMenuAvail[0].question}\n` + `0. Kembali kemenu utama`;
for (let index = 0; index < isMenuAvail[0].answer.body.length; index++) {
const elementMenu = isMenuAvail[0].answer.body[index];
let newIndex = index + 1;
messageText += `${newIndex}. ${elementMenu.question}\n`;
}
await client.sendMessage(from, messageText, MessageType.text);
myCache.set(`session:${content.key.remoteJid}`, {
menu: haveSession.menu,
answer: realMessage,
answerIndex: null
});
}
if (isMenuAvail.length > 0 && checkHaveSession.answer) {
let messageText = isMenuAvail[0].answer.body[parseInt(checkHaveSession.answer) - 1].question + '\n\n' + isMenuAvail[0].answer.body[parseInt(checkHaveSession.answer) - 1].answer;
messageText += '\n\n\n' + `9. Kembali ke menu ${isMenuAvail[0].answer.title}\n` + `0. Kembali kemenu utama` + '\n\nTerimakasih sudah menggunakan Layanan Halo Nutricia , informasi seputar Ibu Hamil KEK Puskesmas Unit I Sumbawa. Ketik SAHABAT GIZI jika anda ingin berinteraksi langsung dengan petugas Gizi Puskesmas Unit I Sumbawa';
await client.sendMessage(from, messageText, MessageType.text);
myCache.set(`session:${content.key.remoteJid}`, {
menu: checkHaveSession.menu,
answer: realMessage,
answerIndex: null
});
}
}
} catch (e) {
console.log(e)
await client.sendMessage(from, 'Menu tidak tersedia\n\n' + `0. Kembali kemenu utama`, MessageType.text);
}
}
}
}else{
if (parseInt(realMessage) == 99) {
let messageText = '';
messageText += 'Terimakasih sudah menggunakan Layanan Halo Nutricia , informasi seputar Ibu Hamil KEK Puskesmas Unit I Sumbawa.';
await client.sendMessage(from, messageText, MessageType.text);
myCache.del(`session:sahabatgizi:${content.key.remoteJid}`);
}
}
}
}
// switch (realMessage) {
// case 'gizi unit 1':
// await client.sendMessage(from, 'Selamat Datang, di Layanan Halo Nutricia , informasi seputar Ibu Hamil KEK Puskesmas Unit I Sumbawa', MessageType.text);
// await client.sendMessage(
// from,
// { url: './voices/voice1.mp4' },
// MessageType.audio,
// { mimetype: Mimetype.mp4Audio }
// );
// await client.sendMessage(from, 'Silahkan tekan tombol PLAY untuk mendengarkan suara', MessageType.text);
// break;
// default:
// break;
// }
} catch (e) {
console.log(e)
}
})
}
(async () => {
await starts()
})();