How to Create a Telegram Bot Using Termux on Android
How to Create a Telegram Bot Using Termux on Android
If you want to explore programming, automation, or content sharing while leveraging just your Android smartphone, Termux is a powerful platform. In this blog, you’ll learn how to build a Telegram bot from scratch using Termux, step by step. This approach works well for learning, testing, or even deploying lightweight bots that interact with APIs like OpenAI, for chat, or automate your online content.
Why Use Termux?
Termux turns your Android device into a full-featured Linux terminal, allowing you to run Python, Node.js, Git, and use tools like tmux for advanced code management—all on mobile. Whether you’re experimenting with chatbots or serious about mobile-first coding, it empowers you to create flexible bots without a laptop.
Prerequisites
- Termux installed (preferably from F-Droid)
- Telegram account (to create a bot via BotFather)
- Basic Python knowledge
- Internet connection
Step 1: Get Your Telegram Bot Token
1. Open Telegram and search for @Botfather.
2. Type `/newbot` and follow the prompts to create your bot.
3. Copy the API token provided. You’ll need this to connect Termux with the Telegram API.
Step 2: Set Up Termux
Open Termux and run these commands:
```bash
pkg update && pkg upgrade
pkg install python git
pip install python-telegram-bot requests
pkg install nano
nano bot.py
```
Step 3: Create Your Bot Script
Use your favorite text editor (or install `nano`/`vim`) to create a file, for example, `bot.py`.
Here’s a simple example using the latest Python-Telegram-Bot library syntax:
Paste this syntax by changing telegram bot token & open ai api from open router
```python
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
TELEGRAM_BOT_TOKEN = "PASTE_YOUR_API_TOKEN_HERE"
async def start(update, context):
await update.message.reply_text(
"👋 Hello! I am your Termux-powered Telegram bot. Send me any message and I'll echo it back!"
)
async def echo(update, context):
await update.message.reply_text(update.message.text)
if __name__ == '__main__':
app = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
app.run_polling()
```
Here Full syntax (full code) paste this code.
## Step 4: Run Your Bot
Save your script and launch your bot:
```bash
python bot.py
```
On Telegram, chat with your newly created bot account and test `/start`. Your bot should respond instantly!
Final Tips
- Protect your API keys—never share them or hardcode them in public scripts
- For regular deployment, consider session management tools or even running your bot on a VPS/server for reliability
- Explore Termux plugins for extended capabilities
With just your Android device and Termux, you’re ready to build real-world Telegram bots, learn modern development workflows, and automate what matters most to you—all from your pocket!
Comments
Post a Comment