Tourmaline : Telegram Bot Framework For Crystal

Tourmaline is a Telegram Bot (and hopefully soon Client) API framework for Crystal. Based heavily off of Telegraf this Crystal implementation allows your Telegram bot to be written in a language that’s both beautiful and fast. Benchmarks coming soon.

Installation

Add this to your application’s shard.yml:

dependencies:
tourmaline:
github: watzon/tourmaline
version: ~> 0.7.0

Also Read – SwiftMonkey : A Framework For Doing Randomised UI Testing Of iOS Apps

Usage

Basic usage

require “tourmaline/bot”

alias TGBot = Tourmaline::Bot

bot = TGBot::Client.new(ENV[“API_KEY”])

bot.command([“start”, “help”]) do |message|
text = “Echo bot is a sample bot created with the Tourmaline bot framework.”
bot.send_message(message.chat.id, text)
end

bot.command(“echo”) do |message, params|
text = params.join(” “)
bot.send_message(message.chat.id, text)
end

bot.poll

Listening for events

Tourmaline has a number of events that you can listen for (the same events as Telegraf actually). The full list of events is as can be found in the documentation.

bot.on(:text) do |update|
text = update.message.not_nil!.text.not_nil!
puts “TEXT: #{text}”
end

Adding middleware

Middleware can be created by extending the Tourmaline::Bot::Middleware class. All middleware classes need to have a call(update : Update) method. The middleware will be called on every update.

class MyMiddleware < TGBot::Middleware

# All middlware include a reference to the parent bot.
# @bot : Tourmaline::Bot::Client

def call(update : Update)
if message = update.message
if user = message.from_user
if text = message.text
puts “#{user.first_name}: #{text}”
end
end
end
end
end

bot.use MyMiddleware

Webhooks

Using webhooks is easy, even locally if you use the ngrok.cr package.

bot.command(“buy”) do |message, params|
bot.send_invoice(
message.chat.id,
“Sample Invoice”,
“This is a test…”,
“123344232323”,
“YOUR_PROVIDER_TOKEN”,
“test1”,
“USD”,
bot.labeled_prices([{label: “Sample”, amount: 299}, {label: “Another”, amount: 369}]).to_json
)
end

Games

Ability to create and run games with your Tourmaline Bot is a recent feature that hasn’t been tested yet. Please use the issue tracker if you experience problems.

Kemal Middleware

Tourmaline provides middleware for Kemal, just in case you want to use Kemal as the server.

require “kemal”
require “tourmaline/kemal/tourmaline_handler”

require “./your_bot”

add_handler Kemal::TourmalineHandler.new(
bot: YourBot.new,
url: “https://something.com”,
path: “/bot-webhook/#{ENV[“TGBOT_API_KEY”]}”
)

Kemal.run

Note: Telegram won’t send webhook requests to non-ssl domains. This means that you need to be running your kemal server with ssl enabled. For local development this can be a pain, but it is made much easier with ngrok.cr.

Development

This currently supports the following features:

  • Bot API
    • Implementation examples
    • Easy command syntax
    • Robust middleware system
    • Standard API queries
    • Stickers
    • Inline mode
    • Long polling
    • Webhooks
    • Payments
    • Games
  • Client API (in development)

If you want a new feature feel free to submit an issue or open a pull request.

R K

Recent Posts

Install PHP on Ubuntu 26.04: Apache, Nginx, and Multiple Versions

PHP 8.5 is included in Ubuntu 26.04's default repositories and is the recommended version for…

5 hours ago

Upgrade to Ubuntu 26.04 from 25.10 and 24.04 LTS: Complete Guide

Ubuntu 26.04 LTS "Resolute Raccoon" arrived on April 23, 2026 with Linux kernel 7.0, GNOME 50,…

5 hours ago

Install Kubernetes on Ubuntu 26.04 with kubeadm and containerd

Kubernetes is the standard platform for running containerized workloads across multiple servers with self-healing, rolling…

5 hours ago

Install Ubuntu 26.04: Bootable USB, Partitioning, and First Steps

Ubuntu 26.04 LTS "Resolute Raccoon" was released on April 23, 2026 with Linux kernel 7.0, GNOME desktop, and standard security support until April 2031. A clean install gives you a known-good starting point on new hardware, when replacing another operating system, or when an upgrade path is not practical. This guide walks through how to install Ubuntu 26.04: downloading and verifying the ISO, writing a bootable USB drive, completing the installer, and doing the initial setup after the first boot. Before you start: You need a USB drive with at least 12 GB of free space. Back up any existing data on the target machine — the installer can erase the entire disk. Install Ubuntu 26.04: Download the ISO…

5 hours ago

Change Timezone on Ubuntu: timedatectl and Desktop GUI Guide

The correct timezone affects more than the clock on your screen. It drives cron job scheduling, systemd timer execution, log file timestamps, database record timing, and SSL certificate validity checks. A mismatched timezone can cause scheduled jobs to fire at the wrong hour and make log timestamps impossible to match with real-world events. This guide shows how to change timezone on Ubuntu using the timedatectl command (the recommended approach for servers and remote machines) and through the graphical Date & Time settings on desktop systems. The steps apply to all current Ubuntu releases including 24.04 and 26.04. <strong>Prerequisite:</strong>&nbsp;Only&nbsp;the&nbsp;root&nbsp;user&nbsp;or&nbsp;a&nbsp;user&nbsp;with&nbsp;sudo&nbsp;access&nbsp;can&nbsp;change&nbsp;the&nbsp;system&nbsp;timezone. Check the Current Timezone Before You Change It Run timedatectl with no arguments to see the active timezone and clock status: bashtimedatectl Sample output: Local…

5 hours ago

Install Atom on Ubuntu 18.04: GitHub’s Code Editor APT Setup

Atom is a free, open-source, cross-platform code editor developed by GitHub. Built on Electron, it uses…

17 hours ago