A simple Telegram bot in Python that can send and receive messages.
- Python 3.x
- uv - Fast Python package manager
- Libraries:
requests,python-dotenv
- Install dependencies:
uv pip install requests python-dotenvor if you have a pyproject.toml or requirements.txt:
uv pip sync requirements.txt-
Configure environment variables:
- Copy the
.env.examplefile to.env:cp .env.example .env
- Edit the
.envfile and insert your values:TELEGRAM_BOT_TOKEN: Your bot token (get it from @BotFather)TELEGRAM_CHAT_ID: Your chat ID (get it from @getidsbot)
Example
.env:TELEGRAM_BOT_TOKEN=1234567890:ABCdefGHIjklMNOpqrsTUVwxyz TELEGRAM_CHAT_ID=123456789 - Copy the
You can run the script directly with uv:
uv run main.pySend the default message:
uv run main.pySend a custom message:
uv run main.py -m "Your custom message"or
uv run main.py --message "Your custom message"To put the bot in listening mode and receive messages:
uv run main.py -lor
uv run main.py --listenThe bot will remain running and respond to received messages. To stop it, press Ctrl+C.
When the bot is in listen mode, it responds to the following commands:
/start- Welcome message/help- Show the list of available commands- Any other message - The bot will echo the received message
# Send "Hello, I'm vengomattoBot!" (default message)
uv run main.py
# Send a custom message
uv run main.py -m "Hello from Python!"
# Start the bot in listen mode
uv run main.py -lYou can customize the bot's responses by modifying the listen_for_messages() function in main.py. Add new conditions to handle custom commands:
if text.lower() == '/yourcommand':
send_message("Your custom response")To see all available options:
uv run main.py -h- DO NOT share the
.envfile publicly - The
.envfile is already included in.gitignoreto avoid accidentally committing it - Use
.env.exampleas a template for other developers - NEVER commit tokens and credentials to the repository
send_message(message)- Send a message to the specified chatget_updates(offset)- Retrieve updates from Telegramlisten_for_messages()- Listen for new messages and respond automatically
- Telegram Bot API
- @BotFather - To create and manage bots
- @getidsbot - To get your chat ID