make a simple aiogram 3 bot
- User
make a simple aiogram 3 bot
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
make a simple aiogram 3 bot
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, here is a very simple aiogram 3 bot that responds to the /start command and any other text message.
Before You Start:
@BotFather on Telegram and create a new bot. It will give you an API token.pip install aiogram
The Code:
import asyncio
import logging
import sys
from os import getenv
from aiogram import Bot, Dispatcher, types, F
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
# Replace with your actual bot token
# It's better practice to use environment variables in production
TOKEN = getenv("BOT_TOKEN")
if not TOKEN:
# If running locally without environment variables, you can uncomment this
# and paste your token directly, but remember to remove it for production!
# TOKEN = "YOUR_ACTUAL_BOT_TOKEN_HERE"
# If you still haven't set the token, exit
if not TOKEN:
print("Error: No BOT_TOKEN found. Please set it as an environment variable or uncomment the placeholder line.")
sys.exit(1)
# Initialize Bot and Dispatcher
# ParseMode.HTML is used so you can easily send formatted text like <b>bold</b>
bot = Bot(token=TOKEN, parse_mode=ParseMode.HTML)
dp = Dispatcher()
# Configure logging (optional, but good for debugging)
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
# Handler for the /start command
@dp.message(CommandStart())
async def command_start_handler(message: types.Message) -> None:
"""
This handler receives messages with `/start` command
"""
await message.answer(f"Hello, {message.from_user.full_name}!\nI'm a simple aiogram 3 bot.")
# Handler for any other text message (that isn't a command)
@dp.message(F.text)
async def handle_other_messages(message: types.Message) -> None:
"""
This handler receives any text message and responds to it.
"""
await message.answer("I received your message!")
# Main function to start the bot
async def main() -> None:
"""
Starts the bot in polling mode.
"""
# Drop pending updates (optional) - useful if your bot was down and you don't want to process old messages
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())
How to Run:
simple_bot.py).BOT_TOKEN environment variable in your terminal:
export BOT_TOKEN="YOUR_ACTUAL_BOT_TOKEN_HERE" (replace the placeholder)set BOT_TOKEN="YOUR_ACTUAL_BOT_TOKEN_HERE"$env:BOT_TOKEN="YOUR_ACTUAL_BOT_TOKEN_HERE"
Then run the script in the same terminal session.TOKEN = "YOUR_ACTUAL_BOT_TOKEN_HERE" line in the code and paste your token there. Remember to remove this for production environments!python simple_bot.py
Your bot should now be running and you can interact with it on Telegram. Send /start or any text message.