DEV Community

Cover image for How to create an AI ChatBot and flex in front of your dumb friends
Souvik Paul
Souvik Paul

Posted on

How to create an AI ChatBot and flex in front of your dumb friends

Today, I'll show you how you can create your very own personal assistant that can answer all types of questions, and how you can host it for completely free of cost.

If you're in college and your friends are dumb, you can flex in front of them.

Just kidding, not just flex, you can build any type of personal robot that follows your instructions.

To build this, we need 3 things: a place where we can chat, an LLM API to generate answers and a server to run the chatbot.

So we use these platforms to build our app:

  1. Telegram (Telegram Bot API)
  2. OpenRouter/Krutrim Cloud (LLM API)
  3. Python

Let's start with Telegram.

Open Telegram and go to @BotFather to create your bot.

Then, send /newbot to BotFather and write your preferred name and username (the username must include the word 'bot' in it).

Telegram BotFather

Now, copy the Telegram API Key.

Now open any code editor where you write Python code, and let's start building the bot.

Before building the bot, let's grab the main brain. LLM API to generate replies to the messages.

For this project, I'm using Qwen3 32B model. It works well for me in many cases before; you can try playing around with other models.

You can get the free API key from OpenRouter.

Qwen at OpenRouter

With a free OpenRouter account, you can call the API 50 times a day. If you're just playing around, you can use it.

But if you scale, you can add credits, or if you're from India, you can use Krutrim Cloud and use the services at scale at a very reasonable price.

Krutrim Cloud Models

You can find a lot of models there also.

Ok, now just create an API key on OpenRouter or Krutrim Cloud and copy the key.

Now open the terminal/cmd and install the PyTelegramBotAPI package by running !pip install pyTelegramBotAPI command.

Open your code editor and paste this code.

import telebot
import requests
import json

API_KEY = "<--TELEGRAM BOT API KEY-->"
bot = telebot.TeleBot(API_KEY)

def start_chat(message):
  return True

@bot.message_handler(func=start_chat)
def chat(message):

  print('Typing...')
  bot.send_chat_action(chat_id=message.chat.id, action='typing')

  response = requests.post(
    url="https://openrouter.ai/api/v1/chat/completions",
    headers={
      "Authorization": "Bearer <--LLM API KEY-->",
      "Content-Type": "application/json"
    },
    data=json.dumps({
      "model": "qwen/qwen3-32b:free",
      "messages": [
        {
          "role": "user",
          "content": message.text
        },
        {
            "role": "system",
            "content": "You are <--BOT NAME-->, created by <--COMPANY NAME--> at <--COMPANY LOCATION-->, by <--DEVELOPER NAME-->, a smart and friendly AI assistant. Always respond in a short, clear, and to-the-point manner. Avoid unnecessary explanations unless asked. Use simple language. Prioritise helpfulness, speed, and clarity. If unsure, say so briefly."
        }
      ],

    })
  )
  data = response.json()
  reply = data['choices'][0]['message']['content']
  reply = reply.replace('**', "")
  bot.send_message(message.chat.id, reply)
  print('Reply sent to '+message.from_user.first_name)

print('AI is running...')

bot.infinity_polling()
Enter fullscreen mode Exit fullscreen mode

This is the code you need for the bot.

Change the API keys and system prompt details accordingly. You can also tweak & use different system prompts to do a completely different job as well as I said earlier.

Make use of updating the API URL and model name according to the service you use.

By now, if you run the code, you'll find your bot working perfectly fine like this.

AI ChatBot Working

Awesome, now just keep running the server, and when it's running, your bot is also running.

Now, to run it 24x7, you can deploy the Python code to any cloud VPS server from any of your preferred hosting companies.

You can also rent Krutrim Cloud CPU and GPU to run your applications and AI models as well.

Or if you've an active internet connection in your home, you can use your old Android mobile as a server and it's pretty much do the work pretty well.

Just download Termux and run the Python script there.

Termux Working on Termux

If you want to SSH your Termux terminal to your computer for development purposes, you can follow this tutorial from Talkin' Tech Stuff channel.

Now connect the phone to the charger, connect to WiFi and just run the Python script.

Congratulations on your new AI ChatBot.

By now, if your friends think you're cool, give me a treat, bro!

Top comments (0)