diff --git a/app.js b/app.js index 6220997..b409d52 100644 --- a/app.js +++ b/app.js @@ -7,7 +7,7 @@ const { getJoke } = require('./libs/dadJokes'); const { rtag, r34 } = require('./libs/rule34'); const { addToLogs, isTrue, image_search, getHelp } = require('./libs/botTools'); const { rockPaperScissorsAgainstBot } = require('./libs/games'); -const { generateImage } = require('./libs/openAi'); +const { generateImage, answerQuestion } = require('./libs/openAi'); //bot initialization const bot = new Telegraf(process.env.TELEGRAM); @@ -88,5 +88,9 @@ bot.command('g', ctx => { generateImage(ctx.message.text.slice(+3), ctx, bot) }) +bot.command('q', ctx => { + answerQuestion(ctx.message.text.slice(+3), ctx, bot) +}) + //bot launch bot.launch(); \ No newline at end of file diff --git a/libs/openAi.js b/libs/openAi.js index 597f30b..dbed5ff 100644 --- a/libs/openAi.js +++ b/libs/openAi.js @@ -33,4 +33,28 @@ function generateImage(query, ctx, bot) { }) } -module.exports = { generateImage }; \ No newline at end of file +function answerQuestion(query, ctx, bot) { + response = openai.createCompletion({ + model: "text-davinci-003", + prompt: query, + max_tokens: 500, + temperature: 0.9, + }).catch((err) => { + console.log(err); + }) + + console.log("--> answering the question " + query); + addToLogs("--> answering the question " + query) + bot.telegram.sendMessage(ctx.chat.id, "Generating the answer.", {}); + + response.then((res) => { + const text = res.data.choices[0].text.slice(+2); + + bot.telegram.sendMessage(ctx.chat.id, text, {}).catch((err) => { + bot.telegram.sendMessage(ctx.chat.id, "Something went wrong.", {}); + console.log("--> error while sending the answer : " + err); + }) + }) +} + +module.exports = { generateImage, answerQuestion }; \ No newline at end of file diff --git a/src/helps/openAI.txt b/src/helps/openAI.txt index 142df30..ef29674 100644 --- a/src/helps/openAI.txt +++ b/src/helps/openAI.txt @@ -2,4 +2,5 @@ This is the opanAI help message : list of opanAI commands : - -`/g ` (generate an image with DALL-E with your query) \ No newline at end of file + -`/g ` (generate an image with DALL-E with your query) + -`/q ` (generate an answer for your question) \ No newline at end of file