How to Build a Telegram AI Chatbot with Flask, Ngrok, and Ollama

How to Build a Telegram AI Chatbot with Flask, Ngrok, and Ollama

Setting Up a Telegram AI Chatbot with Flask, Ngrok, and Ollama

In this guide, we will set up a Telegram chatbot that uses Flask for handling requests, Ngrok to expose our local server, and Ollama for AI-based responses.


1. Create a Telegram Bot using BotFather

BotFather is an official Telegram bot that helps you create and manage bots.

Steps to Get Your Bot Token:

  1. Open Telegram and search for @BotFather.
  2. Start a chat with BotFather and send the command /newbot.
  3. Follow the prompts and choose a name and username for your bot.
  4. After completion, you will receive a Bot Token. Save this securely.

2. Install Ngrok on Linux

Ngrok helps expose your local development server to the internet.

Installation Steps:

curl -sSL https://ngrok-agent.s3.amazonaws.com/ngrok.asc \
    | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null \
    && echo "deb https://ngrok-agent.s3.amazonaws.com buster main" \
    | sudo tee /etc/apt/sources.list.d/ngrok.list \
    && sudo apt update \
    && sudo apt install ngrok

Start Ngrok:

ngrok config add-authtoken 2sGVljxxxxxxxxxYourAuthTokenxxxxxxxxGjkJD6

ngrok http --url=your-ngrok-url.ngrok-free.app 8000

Ngrok will generate a forwarding URL like https://your-ngrok-url.ngrok-free.app 

Copy this URL.


3. Install Ollama for AI-based Responses

Ollama provides AI models for generating chatbot responses.

Installation Steps:

curl -fsSL https://ollama.ai/install.sh | sh

Verify installation with:

ollama --version

Download Llama3 Model:

ollama pull llama3.2:1b

Serve if needed:

ollama serve


4. Install Python and Dependencies

We will use Flask to handle webhooks and requests.

Install Python and Pip:

sudo apt update && sudo apt install python3 python3-pip -y

Install Flask and Requests:

pip3 install flask requests


5. Set Up the Flask Application

Create a new Python script (OllamaBot.py) with the following code:

👉 Source Code on GitHubTelegram Webhook


6. Set Up Environment Variables

To store your Bot Token securely, set it as an environment variable:

export TELEGRAM_BOT_TOKEN="your-bot-token-here"


7. Run the Flask Application

Start your chatbot server with:

python3 OllamaBot.py

Now, your server is running locally.


8. Set Up the Webhook

To link Telegram with your bot, visit in a browser:

https://your-ngrok-url.ngrok-free.app

If successful, you will see

{"description":"Webhook is set","ok":true,"result":true}


9. Test Your Telegram Bot

👉 Try ChatBot here@imvickykumar999_bot

Open Telegram and send a message to your bot. It should respond using the AI model!




Conclusion

You have successfully set up a Telegram chatbot using Flask, Ngrok, and Ollama. You can now modify the bot’s logic or deploy it to a cloud service for continuous availability.

Happy coding! 🚀

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading...