Creating a Telegram Bot in Swift: From Idea to Deployment

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Telegram Bot in Swift: A Step-by-Step Guide for the Bold

\n\n

Hello, developer! If you think Telegram bots are only written in Python or JavaScript — I have news for you. Swift, which you love for its elegance and performance, is also great for server-side development. Yes, that same Swift you use to write iOS apps can also work on the backend. In this guide, we will create a Telegram bot in Swift from scratch using Vapor — a powerful web framework. The bot will respond to commands, process messages, and even send images. Let's go!

\n\n

Why Swift for a Telegram Bot? Breaking Stereotypes

\n\n

Before diving into the code, let's figure out why you would write a bot in Swift. First, if you are an iOS developer, you already know the language. You don't need to learn Python or JS from scratch. Second, Swift offers type safety and speed. The bot will run fast and without surprises. Third, the Vapor ecosystem allows easy integration with databases, sending notifications, and even parsing JSON. And yes, the Telegram Bot API works great with Swift. The main thing is to set up the environment correctly.

\n\n

Step 1: Setting Up the Environment

\n\n

We will need Xcode (at least version 14), Homebrew, and Vapor. If you already have Xcode — great. If not, download it from the Mac App Store. Now let's install Vapor:

\n\n
brew install vapor
\n\n

After installation, check the version:

\n\n
vapor --version
\n\n

See the version number? Great. Now let's create a new project:

\n\n
vapor new TelegramBotSwift --template web\ncd TelegramBotSwift
\n\n

The --template web flag will create a server application skeleton. If you want minimalism — use --template api. We'll go with the web application path for more flexibility.

\n\n

Step 2: Connecting the Telegram Bot API

\n\n

To work with Telegram, we need a library. There are several options: TelegramBotSDK from Vapor, SwiftTelegramBot, or raw HTTP requests. I recommend TelegramBotSDK — it's stable and well-documented. Let's add it to Package.swift:

\n\n
// swift-tools-version:5.9\nimport PackageDescription\n\nlet package = Package(\n    name: "TelegramBotSwift",\n    platforms: [.macOS(.v12)],\n    dependencies: [\n        .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),\n        .package(url: "https://github.com/vapor/telegram-bot-sdk.git", from: "2.0.0")\n    ],\n    targets: [\n        .executableTarget(\n            name: "App",\n            dependencies: [\n                .product(name: "Vapor", package: "vapor"),\n                .product(name: "TelegramBotSDK", package: "telegram-bot-sdk")\n            ]\n        )\n    ]\n)
\n\n

Now let's get a token for the bot. Open Telegram, find BotFather, type /newbot, and follow the instructions. Save the token — you'll need it in a minute.

\n\n

Step 3: Writing Your First Bot in Swift

\n\n

Open the file Sources/App/configure.swift. This is the entry point. Let's write the basic structure:

\n\n
import Vapor\nimport TelegramBotSDK\n\npublic func configure(_ app: Application) throws {\n    // Get the token from environment variables\n    guard let token = Environment.get("TELEGRAM_BOT_TOKEN") else {\n        fatalError("TELEGRAM_BOT_TOKEN not found")\n    }\n    \n    // Initialize the bot\n    let bot = TelegramBot(token: token, app: app)\n    \n    // Handler for the /start command\n    bot.onCommand("start") { context in\n        try await context.sendMessage("Hello! I am a bot in Swift. How can I help you?")\n    }\n    \n    // Handler for the /help command\n    bot.onCommand("help") { context in\n        try await context.sendMessage("Available commands:\\n/start - start\\n/help - help\\n/echo [text] - repeat message")\n    }\n    \n    // Handler for the /echo command\n                

Blogs

Book Recommendations