Learn Bot Development (Telegram & Discord)

What Are Bots?

Bots are software applications that run automated tasks over the internet. On messaging platforms like Telegram and Discord, bots can interact with users, respond to commands, send notifications, fetch data, and even integrate with external APIs. They are widely used for customer support, automation, games, and community management.

Common Use Cases

  • Customer Service – Answer FAQs automatically.
  • Notifications – Send alerts, reminders, or news updates.
  • Moderation – Filter spam, manage chat rules.
  • Games & Quizzes – Interactive games, trivia.
  • Workflow Automation – Trigger actions from other services (e.g., GitHub, weather, APIs).

Telegram Bots with Python

1. Getting Started

To create a Telegram bot, you need a bot token from the BotFather. Open Telegram, search for @BotFather, and follow the instructions to create a new bot. You'll receive a token like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.

Install the Python library:

pip install python-telegram-bot

2. Basic Echo Bot

This simple bot replies with whatever message you send it. It also responds to the /start command.

from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes

TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")

async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)

if __name__ == '__main__':
    application = Application.builder().token(TOKEN).build()
    application.add_handler(CommandHandler('start', start))
    application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), echo))
    application.run_polling()

Run the script and start a conversation with your bot on Telegram.

3. Adding More Commands

You can easily extend your bot with additional commands, such as /help or /info.

async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    help_text = "Available commands:\n/start - Start the bot\n/help - Show this help"
    await update.message.reply_text(help_text)

# Register the handler
application.add_handler(CommandHandler('help', help_command))

With python-telegram-bot, you can also handle inline queries, custom keyboards, and media messages.

Discord Bots with JavaScript (Discord.js)

1. Getting Started

Create a new application at the Discord Developer Portal, then add a bot user and copy the token. Invite the bot to your server using the OAuth2 URL generator with the bot scope.

Initialize a Node.js project and install discord.js:

npm init -y
npm install discord.js

2. Ping‑Pong Bot

This bot replies with "pong!" whenever a user sends !ping.

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

const TOKEN = "YOUR_DISCORD_BOT_TOKEN";

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', msg => {
    if (msg.author.bot) return; // ignore bot messages
    if (msg.content === '!ping') {
        msg.reply('pong!');
    }
});

client.login(TOKEN);

After running the script, type !ping in your Discord server and the bot will reply.

3. Handling Events & More Commands

Discord.js provides many events like messageCreate, guildMemberAdd, and interactionCreate for slash commands. You can create more advanced bots with embeds, reactions, and moderation tools.

// Welcome new members
client.on('guildMemberAdd', member => {
    const channel = member.guild.channels.cache.find(ch => ch.name === 'welcome');
    if (!channel) return;
    channel.send(`Welcome to the server, ${member.user.username}! 🎉`);
});

// A simple slash command (requires InteractionCreate event)
client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;
    if (interaction.commandName === 'ping') {
        await interaction.reply('Pong!');
    }
});