├── logo.png ├── .gitignore ├── package.json ├── README.md └── Bot_EloniumMeme.js /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Bot_EloniumMeme/HEAD/logo.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #Environment variables 2 | .env 3 | 4 | #Node.js 5 | package-lock.json 6 | node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "module", 3 | "scripts": { 4 | "start": "node Bot_EloniumMeme.js" 5 | }, 6 | "dependencies": { 7 | "axios": "^1.3.1", 8 | "dotenv": "^16.0.3", 9 | "jimp": "^0.22.7", 10 | "node-telegram-bot-api": "^0.59.0", 11 | "request": "^2.88.2", 12 | "sharp": "^0.32.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Telegram DALL·E bot 2 | ![GitHub](https://img.shields.io/github/license/javitoro/telegram-dalle-bot) [![GitHub issues](https://img.shields.io/github/issues/JaviToro/telegram-dalle-bot)](https://github.com/JaviToro/telegram-dalle-bot/issues) [![GitHub forks](https://img.shields.io/github/forks/JaviToro/telegram-dalle-bot)](https://github.com/JaviToro/telegram-dalle-bot/network) 3 | 4 | ## Introduction 5 | This is a simple implementation of the OpenAI API, a Telegram bot which will generate an image using AI for the prompt you send to it using the command `/image [prompt]`. Like, for example, `/image bots getting to the Moon`. 6 | 7 | ## Set-up guide 8 | 9 | ### Getting keys 10 | In order to use this bot yourself, you'll need to get two keys, one for the OpenAI API authorization and other one as a token for the Telegram bot. As for OpenAI, you can register and get your API key [here](https://beta.openai.com/docs/introduction/overview). For the Telegram side, you can create a bot and get its token using [@BotFather](https://t.me/botfather) inside the app. 11 | 12 | ### Setting up the environment 13 | 14 | Once you've got both keys, create a .env file in the root of the project and then add the tokens there, the following way: 15 | ``` 16 | OPENAI_API_KEY="your token as string" 17 | TELEGRAM_BOT_TOKEN="your token as string" 18 | ``` 19 | Save the file and remember adding it to your .gitignore if you're pushing the code, otherwise you'd be sharing your keys. 20 | 21 | ### Running the bot 22 | 23 | First, we need to install the dependencies in order for the bot to run correctly. From the root, just execute the following command. 24 | ``` 25 | npm install 26 | ``` 27 | 28 | That's it, we're ready to run the bot, just use this script: 29 | ``` 30 | npm run start 31 | ``` 32 | 33 | And that's all, you can now interact with your bot using Telegram. 34 | -------------------------------------------------------------------------------- /Bot_EloniumMeme.js: -------------------------------------------------------------------------------- 1 | import { createRequire } from 'module' 2 | import axios from 'axios' 3 | const require = createRequire(import.meta.url) 4 | const TelegramBot = require('node-telegram-bot-api') 5 | const Jimp = require('jimp'); 6 | const dotenv = require('dotenv') 7 | 8 | const userMessageTime = new Map() 9 | 10 | dotenv.config() 11 | const token = process.env.TELEGRAM_BOT_TOKEN 12 | const bot = new TelegramBot(token, { polling: true }) 13 | let lastMessageTime = 0 14 | async function createPrediction (text) { 15 | let promptText = text.replace("elon musk", ""); 16 | promptText = promptText.replace("Elon musk", ""); 17 | promptText = promptText.replace("Elon Musk", ""); 18 | promptText = promptText.replace("ELON MUSK", ""); 19 | promptText = promptText.replace("Elon", ""); 20 | promptText = promptText.replace("elon", ""); 21 | 22 | console.log(promptText); 23 | 24 | const response = await axios.post( 25 | 'https://api.replicate.com/v1/predictions', 26 | { 27 | // Pinned to a specific version of Stable Diffusion 28 | // See https://replicate.com/stability-ai/stable-diffussion/versions 29 | version: 30 | '9936c2001faa2194a261c01381f90e65261879985476014a0a37a334593a05eb', //stable-diffussion 31 | input: { prompt: 'Elon Musk, Elon Musk ' + promptText + ', Elon Musk himself, funny meme, close up, concept art, intricate details, highly detailed'} 32 | }, 33 | { 34 | headers: { 35 | Authorization: `Token ${process.env.REPLICATE_API_TOKEN}`, 36 | 'Content-Type': 'application/json' 37 | } 38 | } 39 | ) 40 | 41 | const prediction = response.data 42 | return prediction 43 | } 44 | 45 | async function getPredictionStatus (id) { 46 | const response = await axios.get( 47 | 'https://api.replicate.com/v1/predictions/' + id, 48 | { 49 | headers: { 50 | 'Content-Type': 'application/json', 51 | Authorization: `Token ${process.env.REPLICATE_API_TOKEN}` 52 | } 53 | } 54 | ) 55 | 56 | const prediction = response.data 57 | console.log(response) 58 | return prediction 59 | } 60 | const sleep = ms => new Promise(r => setTimeout(r, ms)) 61 | const pending = async (sentMessage, chatId, username) => { 62 | let index = 59 63 | while (index > 0) { 64 | index-- 65 | await sleep(1000) 66 | bot.editMessageText( 67 | '@' + 68 | username + 69 | " You're in cooldown mode please wait " + 70 | index + 71 | ' seconds.', 72 | { 73 | chat_id: chatId, 74 | message_id: sentMessage.message_id 75 | } 76 | ) 77 | } 78 | } 79 | bot.onText(/\/elonium (.+)/, async (msg, match) => { 80 | const chatId = msg.chat.id 81 | const username = msg.from.username 82 | const now = Date.now() 83 | 84 | if (userMessageTime.has(chatId)) { 85 | lastMessageTime = userMessageTime.get(chatId) 86 | const timeDifference = now - lastMessageTime 87 | lastMessageTime = now 88 | 89 | if (timeDifference < 15 * 1000) { 90 | bot 91 | .sendMessage( 92 | chatId, 93 | '@' + 94 | username + 95 | " You're in cooldown mode please wait 14 seconds." 96 | ) 97 | .then(sentMessage => { 98 | pending(sentMessage, chatId, username) 99 | }) 100 | return 101 | } 102 | } 103 | // Update the last message time for this user 104 | userMessageTime.set(chatId, now) 105 | bot.sendMessage( 106 | chatId, "Generating Image for @" + username 107 | ) 108 | //"Generating Image for @" + username 109 | //"I hope to discuss in telegram with you. My telegram id is GloryDream413." 110 | // const image = await generateImage(match[1]); 111 | const prediction = await createPrediction(match[1]) 112 | let response = null 113 | let nCount = 0; 114 | while (prediction.status !== 'succeeded' && prediction.status !== 'failed') { 115 | await sleep(1000); 116 | nCount++; 117 | if(nCount >= 60) 118 | { 119 | break; 120 | } 121 | response = await getPredictionStatus(prediction.id) 122 | if (response.err || response.output) { 123 | break 124 | } 125 | } 126 | if (response.output) { 127 | const imageUrl = response.output[0]; 128 | const photo = await Jimp.read(imageUrl); 129 | // Download the image 130 | const watermark = await Jimp.read('./logo.png'); 131 | const x = photo.bitmap.width - watermark.bitmap.width - 10; 132 | const y = photo.bitmap.height - watermark.bitmap.height - 10; 133 | // Add the watermark to the photo 134 | photo.composite(watermark, x, y); 135 | const photoBuffer = await photo.getBufferAsync(Jimp.MIME_JPEG); 136 | bot.sendPhoto(chatId, photoBuffer, { 137 | caption: 'Generated for @' + username + ': ' + match[1], 138 | reply_to_message_id: msg.message_id 139 | }) 140 | console.log('Generated for @' + username) 141 | } else { 142 | bot.sendMessage(chatId, 'Sorry. could you again please.'); 143 | } 144 | }) 145 | if(bot.isPolling()) { 146 | await bot.stopPolling(); 147 | } 148 | await bot.startPolling(); --------------------------------------------------------------------------------