DEV Community

Lucas Gragg
Lucas Gragg

Posted on

One bot, two platforms: Discord + Telegram

I've been working on a project that involves creating a bot that can run on both Discord and Telegram. At first, I thought it would be a simple task, but I quickly realized that each platform has its own set of APIs and requirements. I had to create separate codebases for each platform, which led to a lot of duplicated code and maintenance issues. That's when I decided to create a unified bot framework that could handle both Discord and Telegram.

The Problem with Duplicate Code

When I started working on the bot, I created separate codebases for Discord and Telegram. This led to a lot of duplicated code, especially when it came to handling commands and user interactions. For example, I had to create separate functions for handling the /start command on Telegram and the !start command on Discord. This not only increased the maintenance time but also made it harder to add new features to the bot.

Creating a Unified Framework

To solve this problem, I decided to create a unified bot framework that could handle both Discord and Telegram. I started by identifying the common functionality between the two platforms, such as handling commands and user interactions. I then created a modular system that could handle these interactions in a platform-agnostic way. Here's an example of how I handled commands in the unified framework:

import discord
from discord.ext import commands
import telegram
from telegram.ext import CommandHandler

class Bot:
    def __init__(self):
        self.discord_bot = commands.Bot(command_prefix='!')
        self.telegram_bot = telegram.Bot(token='YOUR_TELEGRAM_TOKEN')

    def handle_command(self, command, args):
        # Handle the command in a platform-agnostic way
        if command == 'start':
            # Handle the start command
            pass
        elif command == 'help':
            # Handle the help command
            pass

    def discord_command_handler(self, ctx):
        # Handle Discord commands
        command = ctx.command.name
        args = ctx.args
        self.handle_command(command, args)

    def telegram_command_handler(self, update, context):
        # Handle Telegram commands
        command = update.message.text.split(' ')[0][1:]
        args = update.message.text.split(' ')[1:]
        self.handle_command(command, args)
Enter fullscreen mode Exit fullscreen mode

Adding Webhook Support

Another important feature I needed to add to the bot was webhook support. This would allow me to integrate the bot with external services and receive notifications when certain events occurred. I added a webhook receiver to the bot that could handle incoming requests and trigger certain actions. Here's an example of how I implemented the webhook receiver:

import flask
from flask import request

class WebhookReceiver:
    def __init__(self, bot):
        self.bot = bot
        self.app = flask.Flask(__name__)

    def handle_webhook(self):
        # Handle incoming webhook requests
        if request.method == 'POST':
            # Trigger certain actions based on the incoming request
            pass
        else:
            # Handle other types of requests
            pass

    def run(self):
        # Run the webhook receiver
        self.app.run()
Enter fullscreen mode Exit fullscreen mode

Putting it all Together

I actually packaged this into a tool called Discord & Telegram Bot if you want the full working version. It's a dual-platform bot framework that includes modular command handlers, admin controls, and webhook integrations. You can use it to deploy community bots, notification systems, or custom tools in minutes. I've made it available for $49 at https://lukegraggster.gumroad.com/l/discord-telegram-bot?utm_source=devto&utm_medium=blog&utm_campaign=traffic_bot. I'm still working on adding more features to the bot, but it's already saved me a lot of time and effort in maintaining my own bots.

Top comments (0)