From 1bbbf055459209948200df599a76e5f4d016392d Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Sun, 27 Nov 2022 23:08:18 +0100 Subject: [PATCH] commit --- app.js | 49 ++++++++++++++++++++++++++++--------------------- libs/openAi.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 libs/openAi.js diff --git a/app.js b/app.js index b322b5c..a2ccee2 100644 --- a/app.js +++ b/app.js @@ -6,6 +6,7 @@ const { getJoke } = require('./libs/dadJokes'); const { rtag, r34 } = require('./libs/rule34'); const { addToLogs, isTrue, image_search } = require('./libs/botTools'); const { rockPaperScissorsAgainstBot } = require('./libs/games'); +const { generateImage } = require('./libs/openAi'); //bot initialization const bot = new Telegraf(process.env.TELEGRAM); @@ -20,27 +21,29 @@ bot.command('start', ctx => { bot.help(ctx => { const helpMessage = ` - This is the help message : - Help command : - -/help - Anime command : - -/anime - Image search command : - -\`/s \` - Dad jokes command : - -/dadjoke - Rock Paper Scissors command : - -\`/rps \` - Rule34 tag command : - -\`/rtag \` - Rule 34 image search : - -\`/r34 \` - Truce command : - -/truce (reply to a message with that command to verify it) - Suggest command : - -\`/suggest \` (allows you to add a suggestion to the chanel t.me/+SrzC81CGyusyODNk) - Github link command : - -/github +This is the help message : +Help command : + -/help +Anime command : + -/anime +AI Generated image command : + -\`/g \` +Image search command : + -\`/s \` +Dad jokes command : + -/dadjoke +Rock Paper Scissors command : + -\`/rps \` +Rule34 tag command : + -\`/rtag \` +Rule 34 image search : + -\`/r34 \` +Truce command : + -/truce (reply to a message with that command to verify it) +Suggest command : + -\`/suggest \` (allows you to add a suggestion to the chanel t.me/+SrzC81CGyusyODNk) +Github link command : + -/github ` bot.telegram.sendMessage(ctx.chat.id, helpMessage, {parse_mode: "Markdown"}) console.log('--> sent the help message') @@ -95,5 +98,9 @@ bot.command('rps', ctx => { rockPaperScissorsAgainstBot(ctx.message.text.slice(+5), ctx, bot) }) +bot.command('g', ctx => { + generateImage(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 new file mode 100644 index 0000000..31659ba --- /dev/null +++ b/libs/openAi.js @@ -0,0 +1,32 @@ +const { Configuration, OpenAIApi } = require("openai"); +const { addToLogs } = require('./botTools'); + +const configuration = new Configuration({ + apiKey: process.env.OPENAI, +}); + +const openai = new OpenAIApi(configuration); + +function generateImage(query, ctx, bot) { + const image = openai.createImage({ + prompt: query, + n: 1, + size: "1024x1024", + response_format : 'url' + }) + + console.log("--> generating image for the querry" + query); + addToLogs("--> generating image for the querry" + query) + bot.telegram.sendMessage(ctx.chat.id, "Generating the image.", {}); + + image.then((res) => { + url = res.data.data[0].url + + bot.telegram.sendPhoto(ctx.chat.id, url, {"caption": "This is a generated image for the querry : " + query}).catch((err) => { + bot.telegram.sendMessage(ctx.chat.id, "Something went wrong.", {}); + console.log("--> error while sending the image : " + err); + }) + }) +} + +module.exports = { generateImage }; \ No newline at end of file