I can receive a message via WhatsApp and send an automatic response, so this part is working normally.
when I try to integrate with chatGPT via openai, I can’t figure out why it doesn’t work.
Could anyone kindly help me?
Below, my complete code:
const venom = require('venom-bot');
const dotenv = require('dotenv');
const { Configuration, OpenAI } = require("openai");
require('dotenv').config()
const openai = new OpenAI({
apiKey: 'I have this key working normally'
});
exports.response = async (prompt) =>{
return await openai.createChatCompletion({
model:'gpt-3.5-turbo',
messages: prompt,
});
}
venom.create({
session: 'chatbot',
multidevice: true})
.then((client) => start(client))
.catch((erro) => {
console.log(erro);
});
function start(client) {
client.onMessage((message) => {
// Ignorar mensagens de grupo e respostas de status.
if (message.body && message.isGroupMsg === false && message.from !== 'status@broadcast') {
response(message.body).then(data => {
if (data && data.data && data.data.choices && data.data.choices.length > 0) {
let content = data.data.choices[0].message.content;
client.sendText(message.from, content).then( (result) => { console.log(`Result:\n\n ${result}`)}).catch((erro) => { console.log(`Error client:\n\n ${erro}`)});
}
}).catch(err =>{ console.log(`Error response:\n\n ${erro}`)});
}
});
}