Ezy Bots
🚀 Project #2 (Cloudflare Edition) 🤖 Tiny Link Bot – Send any long URL, and it instantly replies with a short, shareable link. 🧠 Lesson: • Cloudflare Workers (JavaScript) • Fetching external APIs (is.gd) • Serverless & 100% Free with Cloudflare 👀 Try…
▎🚀 Let's Build: Project 2 (Tiny Link Bot)
Here is the complete code and guide for the URL Shortener Bot. This is purely for Cloudflare Workers (no Python, no Vercel).
▎1. The Code (
You only need this one file.
▎2. Deployment Guide
You can copy this into your channel. It explains how to deploy using the browser (easiest for beginners).
▎☁️ Project: Cloudflare Link Shortener Bot
Run a bot 100% free on Cloudflare Workers. No servers, just one file!
▎📂 Step 1: Create the Worker
1. Go to dash.cloudflare.com and sign up/login.
2. Click Compute & AI -> Workers Pages -> Create Application -> Start with hello world.
3. Click Create Worker -> Deploy.
4. Now click Edit Code.
▎📝 Step 2: The Code
1. Delete everything in the left side editor.
2. Paste the code above.
3. Click Save and Deploy (top right).
▎🔑 Step 3: Add Bot Token
1. Go back to your Worker's dashboard (click the back arrow).
2. Go to Settings -> Variables.
3. Click Add Variable.
– Name:
– Value: (Paste your Token from @BotFather)
– Click Deploy (or Save).
▎🔗 Step 4: Connect Webhook
Copy your Worker URL (e.g.,
Replace the details in this link and open it in your browser:
✅ Done! Send a link to your bot to test it.
#EzyBot #Cloudflare #JavaScript
Here is the complete code and guide for the URL Shortener Bot. This is purely for Cloudflare Workers (no Python, no Vercel).
▎1. The Code (
worker.js)You only need this one file.
export default {
async fetch(request, env, ctx) {
// 1. Handle the Webhook (POST request from Telegram)
if (request.method === "POST") {
const payload = await request.json();
// Check if it's a message
if (payload.message && payload.message.text) {
const chatId = payload.message.chat.id;
const text = payload.message.text;
// If command is /start
if (text === "/start") {
await sendTelegramMessage(chatId, "🔗 Send me any long URL, and I will shorten it!", env.BOT_TOKEN);
}
// If it looks like a URL (basic check)
else if (text.startsWith("http")) {
// Call the Shortener API
const shortUrl = await shortenUrl(text);
await sendTelegramMessage(chatId, ✅ Here is your short link:\n${shortUrl}, env.BOT_TOKEN);
}
else {
await sendTelegramMessage(chatId, "⚠️ That doesn't look like a link. Send a URL starting with http...", env.BOT_TOKEN);
}
}
return new Response("OK");
}
// 2. Simple landing page for the browser
return new Response("Bot is running on Cloudflare Workers! 🚀");
},
};
// --- Helper Functions ---
// Function to send message to Telegram
async function sendTelegramMessage(chatId, text, token) {
const url = https://api.telegram.org/bot${token}/sendMessage;
await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ chat_id: chatId, text: text }),
});
}
// Function to shorten URL using is.gd (Free, no API key needed)
async function shortenUrl(longUrl) {
const apiUrl = https://is.gd/create.php?format=json&url=${encodeURIComponent(longUrl)};
const response = await fetch(apiUrl);
const data = await response.json();
if (data.shorturl) {
return data.shorturl;
} else {
return "Error shortening link.";
}
}▎2. Deployment Guide
You can copy this into your channel. It explains how to deploy using the browser (easiest for beginners).
▎☁️ Project: Cloudflare Link Shortener Bot
Run a bot 100% free on Cloudflare Workers. No servers, just one file!
▎📂 Step 1: Create the Worker
1. Go to dash.cloudflare.com and sign up/login.
2. Click Compute & AI -> Workers Pages -> Create Application -> Start with hello world.
3. Click Create Worker -> Deploy.
4. Now click Edit Code.
▎📝 Step 2: The Code
1. Delete everything in the left side editor.
2. Paste the code above.
3. Click Save and Deploy (top right).
▎🔑 Step 3: Add Bot Token
1. Go back to your Worker's dashboard (click the back arrow).
2. Go to Settings -> Variables.
3. Click Add Variable.
– Name:
BOT_TOKEN– Value: (Paste your Token from @BotFather)
– Click Deploy (or Save).
▎🔗 Step 4: Connect Webhook
Copy your Worker URL (e.g.,
https://crimson-rain.user.workers.dev). Replace the details in this link and open it in your browser:
https://api.telegram.org/bot<YOUR_TOKEN>/setWebhook?url=<YOUR_WORKER_URL>
✅ Done! Send a link to your bot to test it.
#EzyBot #Cloudflare #JavaScript