telegram-bot-ruby
Ruby wrapper for Telegram's Bot API.
Installation
Add following line to your Gemfile:
gem 'telegram-bot-ruby'
And then execute:
$ bundle
Or install it system-wide:
$ gem install telegram-bot-ruby
Usage
First things first, you need to obtain a token for your bot. Then create your Telegram bot like this:
require 'telegram/bot'
token = 'YOUR_TELEGRAM_BOT_API_TOKEN'
Telegram::Bot::Client.run(token) do |bot|
bot.listen do ||
case .text
when '/start'
bot.api.sendMessage(chat_id: .chat.id, text: "Hello, #{.from.username}")
when '/stop'
bot.api.sendMessage(chat_id: .chat.id, text: "Bye, #{.from.username}")
end
end
end
Note that bot.api
object implements Telegram Bot API methods as is. So you can invoke any method inside the block without any problems.
Same thing about message
object - it implements Message spec, so you always know what to expect from it.
Custom keyboards
You can use your own custom keyboards. Here is an example:
bot.listen do ||
case .text
when '/start'
question = 'London is a capital of which country?'
# See more: https://core.telegram.org/bots/api#replykeyboardmarkup
answers =
Telegram::Bot::Types::ReplyKeyboardMarkup
.new(keyboard: [%w(A B), %w(C D)], one_time_keyboard: true)
bot.api.sendMessage(chat_id: .chat.id, text: question, reply_markup: answers)
when '/stop'
# See more: https://core.telegram.org/bots/api#replykeyboardhide
kb = Telegram::Bot::Types::ReplyKeyboardHide.new(hide_keyboard: true)
bot.api.sendMessage(chat_id: .chat.id, text: 'Sorry to see you go :(', reply_markup: kb)
end
end
File upload
Your bot can even upload files (photos, audio, documents, stickers, video) to Telegram servers. Just like this:
bot.listen do ||
case .text
when '/photo'
bot.api.sendPhoto(chat_id: .chat.id, photo: File.new('~/Desktop/jennifer.jpg'))
end
end
Contributing
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request