AI models like Llama are powerful tools for building conversational applications. In this tutorial, I'll show you how to set up and deploy an AI-powered Telegram bot using Ollama (a self-hosted Llama model) and Node.js, running entirely on a virtual machine (VM) without a GPU.
You’ll learn how to:
- Install and run Llama3.2 model.
- Set up a Node.js application with PM2 for process management.
- Build a Telegram bot that interacts with the self-hosted AI instance.
The full code is available in my
2️⃣ Step 2: Setting Up Node.js and PM2
We'll use Node.js to create a backend application that interacts with both Ollama and Telegram.
Install Node.js and PM2
CODEsudo apt update
sudo apt install -y nodejs npm
sudo npm install -g pm2
Initialize the Project
Create a folder for your project and initialize a Node.js application:
CODEmkdir ai-telegram-bot
cd ai-telegram-bot
npm init -y
Install Dependencies
CODEnpm install dotenv node-telegram-bot-api
Create the Folder Structure
Organize your project as follows:
CODE📁 src/
├── 📁 services/
│ ├── 📄 ollamaService.js # Handles interactions with the local Ollama API
│ └── 📄 telegramBotService.js # Telegram bot logic and API integration
3️⃣ Step 3: Writing the Code
CreateollamaService.js
This file handles interactions with the Ollama API. The function below sends a string to the API and returns a response, which can be forwarded to the Telegram user.
Refer to the
CODErequire("dotenv").config();
const TelegramBot = require("node-telegram-bot-api");
const { queryOllama } = require("./ollamaService");
const token = process.env.TELEGRAM_BOT_TOKEN;
const bot = new TelegramBot(token, { polling: true });
bot.on("message", async (msg) => {
const chatId = msg.chat.id;
switch (msg.text) {
case "/start":
bot.sendMessage(
chatId,
"Welcome to Ollama Bot! Send me a message and I will translate it to Ollama language."
);
break;
case "/help":
bot.sendMessage(
chatId,
"Send me a message and I will translate it to Ollama language using the llama3.2 model with 1 billion parameters. I can make mistakes, double check important infos."
);
break;
default:
bot.sendChatAction(chatId, "typing");
try {
const response = await queryOllama(msg.text);
bot.sendMessage(chatId, response);
} catch (error) {
bot.sendMessage(
chatId,
"Oops, something went wrong. Please try again."
);
}
}
});
Future improvement: Adding more commands or integrating additional services.
4️⃣ Step 4: Create a Telegram Bot
Create your bot:
- Open Telegram and search for the user BotFather.
- Start a chat with BotFather and send the command
/newbot. - Follow the instructions, providing a name and username for your bot.
- Once completed, BotFather will send you a token. This token is used to authenticate your bot.
Save this token in your.envfile:
CODETELEGRAM_BOT_TOKEN=your_telegram_bot_token
5️⃣ Step 5: Running the Bot
Start the Application
Start the bot using PM2 for process management:
CODEpm2 start src/services/telegramBotService.js --name ai-telegram-bot
pm2 save
Check Logs
Ensure the bot is running correctly by checking the logs:
CODEpm2 logs ai-telegram-bot
Now, your bot is live and ready to interact with users on Telegram!
If your bot doesn’t respond, wake it up with this endpoint:
CODEcurl --location --request POST 'https://api.telegram.org/bot{your-token-here}/getMe'
6️⃣ How It Works
- Users send a message to the Telegram bot.
- The message is forwarded to the Ollama API for processing.
- Ollama generates a response using the Llama model.
- The bot sends the response back to the user in Telegram.
📢 Final Thoughts
Congratulations! You've successfully built and deployed an AI-powered Telegram bot using a self-hosted Llama model.
Feel free to experiment with more advanced models or customize the bot further. If you encounter issues, leave a comment or fork the GitHub repository for more examples and enhancements.
Happy coding! 🚀
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR