Add this line to your application's Gemfile:
gem 'telegram_bot_ruby'
And then execute:
$ bundle
Or install it yourself as:
$ gem install telegram_bot_ruby
require 'telegram_bot'
bot = TelegramBot.new(token: <token>)
bot.listen(method: :webhook, url: '/meow/meow') # not implemented yet
# or
bot.listen(method: :poll, interval: 1)
bot.on :command, 'ping' do # /ping
reply 'pong'
end
bot.on :command, 'plus' do |num1, num2| # /plus 1 2
reply (num1.to_i + num2.to_i).to_s
end
bot.on :text 'ping' do # plain 'ping'
send_message 'pong'
end
# with block: false, message will keep passing through other listeners
bot.on :text, /(\d+)\+(\d+)\=\?/, block: false do |n1, n2|
send_chat_action :typing
send_message (n1.to_i + n2.to_i).to_s
end
# a simple history logger
bot.on :anything do |msg|
Database.save [msg.from.username, msg.text] if !msg.text.empty?
end
# register service at telegram or start long polling
bot.start!
Bug reports and pull requests are welcome on GitHub at https://github.com/shouya/telegram-bot.
The gem is available as open source under the terms of the MIT License.