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
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
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.
This currently supports the following features:
If you want a new feature feel free to submit an issue or open a pull request.
The rsync command in Linux synchronizes files and directories between two locations, locally or over SSH. Unlike scp,…
The patch command in Linux applies a set of changes from a diff file to one or…
The basename command in Linux extracts the last component of a file path, removing the leading directory…
The timeout command in Linux runs a command with a time limit and terminates it when the limit is reached. It is part of GNU coreutils, available on virtually every Linux distribution. It is most useful for commands with no built-in timeout option, such as ping, curl, tcpdump, or a custom script that might hang indefinitely. How the timeout Command Works in Linux The syntax is: bashtimeout [OPTIONS] DURATION COMMAND [ARG]...…
The rmmod command in Linux removes a loaded module from the running kernel. Because the kernel has…
The free command in Linux gives you a quick summary of RAM and swap usage. It reads…