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
    1
    gem install telegram-bot-ruby
    Copied!
  • Basic usage
As many APIs, you have to get a token to deal with your bot. Here a basic usage
1
require 'telegram/bot'
2
3
token = 'YOUR_TELEGRAM_BOT_API_TOKEN'
4
5
Telegram::Bot::Client.run(token) do |bot|
6
bot.listen do |message|
7
case message.text
8
when '/start'
9
bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}")
10
when '/stop'
11
bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}")
12
when '/rubyfu'
13
bot.api.send_message(chat_id: message.chat.id, text: "Rubyfu, where Ruby goes eveil!")
14
end
15
end
16
end
Copied!
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).
1
require 'telegram/bot'
2
3
bot.listen do |message|
4
case message
5
when Telegram::Bot::Types::InlineQuery
6
results = [
7
Telegram::Bot::Types::InlineQueryResultArticle
8
.new(id: 1, title: 'First article', message_text: 'Very interesting text goes here.'),
9
Telegram::Bot::Types::InlineQueryResultArticle
10
.new(id: 2, title: 'Second article', message_text: 'Another interesting text here.')
11
]
12
bot.api.answer_inline_query(inline_query_id: message.id, results: results)
13
when Telegram::Bot::Types::Message
14
bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}!")
15
end
16
end
Copied!
Resources
Last modified 3yr ago
Copy link