Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)
Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

How to Create an AI Companion Telegram Bot

↗ Quelle (dev.to)
🗣️ Stimme:

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:




  1. Install and run Llama3.2 model.

  2. Set up a Node.js application with PM2 for process management.

  3. 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





    CODE
    sudo 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:




    CODE
    mkdir ai-telegram-bot
    cd ai-telegram-bot
    npm init -y











    Install Dependencies





    CODE
    npm 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






    Create ollamaService.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




    CODE
    require("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:



    1. Open Telegram and search for the user BotFather.

    2. Start a chat with BotFather and send the command /newbot.

    3. Follow the instructions, providing a name and username for your bot.

    4. Once completed, BotFather will send you a token. This token is used to authenticate your bot.
      Save this token in your .env file:




    CODE
    TELEGRAM_BOT_TOKEN=your_telegram_bot_token












    5️⃣ Step 5: Running the Bot






    Start the Application


    Start the bot using PM2 for process management:




    CODE
    pm2 start src/services/telegramBotService.js --name ai-telegram-bot
    pm2 save











    Check Logs


    Ensure the bot is running correctly by checking the logs:




    CODE
    pm2 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:




    CODE
    curl --location --request POST 'https://api.telegram.org/bot{your-token-here}/getMe'












    6️⃣ How It Works




    1. Users send a message to the Telegram bot.

    2. The message is forwarded to the Ollama API for processing.

    3. Ollama generates a response using the Llama model.

    4. 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! 🚀

    Vollständiger Original-Bericht
    Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
    ↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
Use custom web fonts in Google Sheets charts
2 Quellen
Introducing the new 1Password App for Google Chat
1 Quelle
Context-aware access controls are available for Gemini Enterprise in the Admin console
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to Create an AI Companion Telegram Bot

Thematisch verwandte Begriffe: Create, Companion, Telegram · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...