A Super Simple Guide for Complete Beginners π
Hey there! π Let's build something cool togetherβa Telegram bot that shortens long URLs. Don't worry if you're new to coding; we'll go step by step!
π― What Will Our Bot Do?
β
Turn long URLs into short ones
β
Create custom short URLs (choose your own nickname for a link!)
β
Work directly in Telegram, just like chatting with a friend
π οΈ What Youβll Need
- Python installed on your computer (Download here)
- A Telegram account
- A free API key from Shotcut
π Step-by-Step Guide
Step 1: Setting Up Your Tools
Install the required Python packages by running:
pip install python-telegram-bot shotcutap
Step 2: Getting Your Telegram Bot Token
- Open Telegram and search for βBotFatherβ.
- Send
/newbot
and follow the prompts:- Choose a name (e.g., My URL Shortener)
- Choose a username (must end in
bot
, e.g., myurlshortener_bot)
- BotFather will give you a bot tokenβsave it somewhere safe!
Step 3: Getting Your Shotcut API Key
- Go to Shotcut Developer Portal
- Sign up for a free account
- Get your API key from the dashboard
π§βπ» Step 4: Writing the Code
Create a new file bot.py
and paste this code:
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from shotcut import ShotcutAPI
BOT_TOKEN = "paste_your_telegram_bot_token_here" # Get this from BotFather
SHOTCUT_API_KEY = "paste_your_shotcut_api_key_here" # Get this from Shotcut website
shortener = ShotcutAPI(api_key=SHOTCUT_API_KEY)
async def welcome_user(update: Update, context: ContextTypes.DEFAULT_TYPE):
welcome_message = """
π Hi there! I'm your URL Shortener Bot!
Just send me any link, and I'll make it shorter for you!
Type /help for more info.
"""
await update.message.reply_text(welcome_message)
async def show_help(update: Update, context: ContextTypes.DEFAULT_TYPE):
help_message = """
π How to use me:
1οΈβ£ Send a link to shorten it.
2οΈβ£ Use /custom [URL] [name] for a custom short link.
"""
await update.message.reply_text(help_message)
async def make_custom_link(update: Update, context: ContextTypes.DEFAULT_TYPE):
if len(context.args) < 2:
await update.message.reply_text("β Please provide a URL and custom name.")
return
original_link, custom_name = context.args[0], context.args[1]
try:
result = shortener.shorten_link(url=original_link, custom=custom_name)
await update.message.reply_text(f"β¨ Custom short link: {result['shorturl']}")
except Exception as e:
await update.message.reply_text(f"π Error: {str(e)}")
async def make_short_link(update: Update, context: ContextTypes.DEFAULT_TYPE):
original_link = update.message.text.strip()
if not original_link.startswith(('http://', 'https://')):
await update.message.reply_text("π€ That doesn't look like a valid link.")
return
try:
result = shortener.shorten_link(url=original_link)
await update.message.reply_text(f"β¨ Short link: {result['shorturl']}")
except Exception as e:
await update.message.reply_text(f"π Error: {str(e)}")
def start_bot():
print("π Starting Telegram Bot...")
app = Application.builder().token(BOT_TOKEN).build()
app.add_handler(CommandHandler("start", welcome_user))
app.add_handler(CommandHandler("help", show_help))
app.add_handler(CommandHandler("custom", make_custom_link))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, make_short_link))
app.run_polling()
if __name__ == '__main__':
start_bot()
π Running Your Bot
-
Save Your Secret Tokens
- Replace
paste_your_telegram_bot_token_here
with your Telegram bot token - Replace
paste_your_shotcut_api_key_here
with your Shotcut API key
- Replace
Start Your Bot
python bot.py
You should see βYour bot is ready!β π
-
Test Your Bot
- Open Telegram and find your bot
- Try
/start
,/help
, sending a link, and/custom
commands
π Troubleshooting
Issue | Solution |
---|---|
Invalid Token | Ensure your bot token is correct & has no spaces |
Links not working | Make sure they start with http:// or https://
|
Bot not responding | Restart the bot (Ctrl+C then python bot.py ) |
π Keep Your Bot Secure
- Never share your bot token or API key
- Store your credentials in a safe place
- Be careful about the links you shorten
π You Did It!
Congratulations! Youβve built your own Telegram bot! This is just the beginningβfeel free to add more features.
Need help? The Python and Telegram Bot communities are super friendly and always happy to help new coders! π
Try Shotcut.in Official Telegram Bot πβ¨
Happy coding! π
Top comments (0)