diff --git a/plugins/GURU-Openai.js b/plugins/GURU-Openai.js index 203f06cdb3..f176972a6f 100644 --- a/plugins/GURU-Openai.js +++ b/plugins/GURU-Openai.js @@ -1,6 +1,5 @@ import fetch from 'node-fetch'; - let handler = async (m, { text, conn, usedPrefix, command }) => { if (!text && !(m.quoted && m.quoted.text)) { throw `Please provide some text or quote a message to get a response.`; @@ -19,34 +18,52 @@ let handler = async (m, { text, conn, usedPrefix, command }) => { conn.sendPresenceUpdate('composing', m.chat); const prompt = encodeURIComponent(text); - const guru1 = `${gurubot}/chatgpt?text=${prompt}`; - let response = await fetch(guru1); - let data = await response.json(); - let result = data.result; + + try { + let response = await fetch(guru1); + let data = await response.json(); + let result = data.result; - - if (!result) { + if (!result) { + + throw new Error('No valid JSON response from the first API'); + } + + await conn.relayMessage(m.chat, { + protocolMessage: { + key, + type: 14, + editedMessage: { + imageMessage: { caption: result } + } + } + }, {}); + m.react(done); + } catch (error) { + console.error('Error from the first API:', error); + + const model = 'llama'; const senderNumber = m.sender.replace(/[^0-9]/g, ''); const session = `GURU_BOT_${senderNumber}`; const guru2 = `https://ultimetron.guruapi.tech/gpt3?prompt=${prompt}`; - response = await fetch(guru2); - data = await response.json(); - result = data.completion; - } + let response = await fetch(guru2); + let data = await response.json(); + let result = data.completion; - await conn.relayMessage(m.chat, { - protocolMessage: { - key, - type: 14, - editedMessage: { - imageMessage: { caption: result } + await conn.relayMessage(m.chat, { + protocolMessage: { + key, + type: 14, + editedMessage: { + imageMessage: { caption: result } + } } - } - }, {}); - m.react(done) + }, {}); + m.react(done); + } } catch (error) { console.error('Error:', error);