Telegram API
As we know that Telegram is a messaging app identifies users by their mobile number. Fortunately, Telegram has its own API -Ruby has a wrapper gem for Telegram's Bot API called telegram-bot-ruby - which allows you to Integrate with other services, create custom tools, build single- and multiplayer games, build social services, do virtually anything else; Do you smell anything evil here?
  • Install telegram-bot gem
    gem install telegram-bot-ruby
  • Basic usage
As many APIs, you have to get a token to deal with your bot. Here a basic usage
require 'telegram/bot'
token = 'YOUR_TELEGRAM_BOT_API_TOKEN'
Telegram::Bot::Client.run(token) do |bot|
bot.listen do |message|
case message.text
when '/start'
bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}")
when '/stop'
bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}")
when '/rubyfu'
bot.api.send_message(chat_id: message.chat.id, text: "Rubyfu, where Ruby goes eveil!")
end
end
end
Once your run it, go to your telegram and find the bot and start chat with /start, try to send /rubyfu.
  • Inline bots
If you got that evil smile from above example, you may thinking about interacting with your bots inline to call/@mention your bots and request more action from the bot(s).
require 'telegram/bot'
bot.listen do |message|
case message
when Telegram::Bot::Types::InlineQuery
results = [
Telegram::Bot::Types::InlineQueryResultArticle
.new(id: 1, title: 'First article', message_text: 'Very interesting text goes here.'),
Telegram::Bot::Types::InlineQueryResultArticle
.new(id: 2, title: 'Second article', message_text: 'Another interesting text here.')
]
bot.api.answer_inline_query(inline_query_id: message.id, results: results)
when Telegram::Bot::Types::Message
bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}!")
end
end
Resources
Copy link