const { Client, LocalAuth } = require(
'whatsapp-web.js'
);
const qrcode = require(
'qrcode-terminal'
);
const { GoogleGenerativeAI } = require(
'@google/generative-ai'
);
const genAI =
new
GoogleGenerativeAI(API_KEY);
const client =
new
Client({
authStrategy:
new
LocalAuth(),
puppeteer: {
executablePath:
'/usr/bin/google-chrome-stable'
,
args: [
'--no-sandbox'
,
'--disable-setuid-sandbox'
],
},
});
const model = genAI.getGenerativeModel({ model:
"gemini-pro"
});
async
function
generate(prompt, message) {
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
await message.reply(text);
}
client.on(
'qr'
, (qr) => {
qrcode.generate(qr, { small:
true
});
});
client.on(
'authenticated'
, () => {
console.log(
'Client is authenticated!'
);
});
client.on(
'ready'
, () => {
console.log(
'Client is ready!'
);
});
client.on(
'disconnected'
, () => {
console.log(
'Client is disconnected!'
);
});
client.on(
'auth_failure'
, () => {
console.log(
'Client is auth_failure!'
);
});
client.on(
'message'
, async (message) => {
if
(message.body.includes(
'.bot'
)) {
var
query;
const regxmatch = message.body.match(/.bot(.+)/);
if
(regxmatch) {
query = regxmatch[1];
}
else
{
console.log(
"No regex match!"
);
query =
"Hi"
;
}
generate(query, message);
};
});
client.initialize();