├── .gitignore ├── package.json ├── README.md └── Bot_AICrime.js /.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_AICrime.js" 5 | }, 6 | "dependencies": { 7 | "axios": "^1.3.3", 8 | "cross-fetch": "^3.1.5", 9 | "dotenv": "^16.0.3", 10 | "fs": "^0.0.1-security", 11 | "node-fetch": "^3.3.0", 12 | "node-telegram-bot-api": "^0.59.0", 13 | "openai": "^3.1.0", 14 | "start": "^5.1.0" 15 | } 16 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Getting keys 2 | In order to use this bot yourself, you'll need to get a token for the Telegram bot. 3 | You can create a bot and get its token using [@BotFather](https://t.me/botfather) inside the app. 4 | 5 | ### Setting up the environment 6 | Once you've got Telegram Bot Token create a .env file in the root of the project and then add the tokens there, the following way: 7 | ``` 8 | TELEGRAM_BOT_TOKEN="your token as string" 9 | ``` 10 | Save the file and remember adding it to your .gitignore if you're pushing the code, otherwise you'd be sharing your keys. 11 | 12 | ### Running the bot 13 | First, we need to install the dependencies in order for the bot to run correctly. From the root, just execute the following command. 14 | ``` 15 | npm install 16 | ``` 17 | That's it, we're ready to run the bot, just use this script: 18 | ``` 19 | npm start 20 | ``` 21 | And that's all, you can now interact with your bot using Telegram. -------------------------------------------------------------------------------- /Bot_AICrime.js: -------------------------------------------------------------------------------- 1 | import { createRequire } from 'module' 2 | const require = createRequire(import.meta.url) 3 | const TelegramBot = require('node-telegram-bot-api') 4 | const axios = require('axios'); 5 | const dotenv = require('dotenv') 6 | 7 | dotenv.config() 8 | const token = process.env.TELEGRAM_BOT_TOKEN 9 | const bot = new TelegramBot(token, { polling: true }) 10 | 11 | const prompt = "Hello, I'm a chatbot about crime. Could you tell me about your age?" + 12 | "\nUser : I'm 13." + 13 | "\nYou are not alllowed to use this bot. This bot is allowed for users more than 14." + 14 | "\nUser : I'm 15." + 15 | "\nNice to meet you. I'd like to collect data of the crime from you. Could you tell me in details?" + 16 | "\nUser : Yes. I will."; 17 | 18 | bot.sendMessage("5973551893", "Hello, I'm a chatbot about crime. Could you tell me about your age?"); 19 | const chatbotParams = { 20 | temperature: 0.7, 21 | maxTokens: 3000, 22 | topP: 1, 23 | frequencyPenalty: 0.7, 24 | presencePenalty: 0 25 | }; 26 | 27 | // Define a function to generate a response from the ChatGPT model 28 | async function generateResponse(prompt, chatbotParams) { 29 | const response = await axios({ 30 | method: 'post', 31 | //url: 'https://api.openai.com/v1/engines/text-davinci-003/completions', 32 | url: 'https://api.openai.com/v1/engines/text-davinci-003/completions', 33 | headers: { 34 | 'Content-Type': 'application/json', 35 | 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` 36 | }, 37 | data: { 38 | prompt: prompt, 39 | temperature: chatbotParams.temperature, 40 | max_tokens: chatbotParams.maxTokens, 41 | top_p: chatbotParams.topP, 42 | frequency_penalty: chatbotParams.frequencyPenalty, 43 | presence_penalty: chatbotParams.presencePenalty, 44 | n : 1 45 | } 46 | }); 47 | 48 | let reply = response.data.choices[0].text.trim(); 49 | return reply; 50 | } 51 | 52 | // Handle incoming messages 53 | bot.on('message', async (msg) => { 54 | const chatId = msg.chat.id; 55 | const userText = msg.text; 56 | let promptText = prompt + "\nUser: " + userText; 57 | const chatbotResponse = await generateResponse(promptText, chatbotParams); 58 | const responseText = chatbotResponse; 59 | bot.sendMessage(chatId, responseText); 60 | }); 61 | 62 | if(bot.isPolling()) { 63 | await bot.stopPolling(); 64 | } 65 | await bot.startPolling(); --------------------------------------------------------------------------------