All Projects → atipugin → Telegram Bot Ruby

atipugin / Telegram Bot Ruby

Licence: wtfpl
Ruby wrapper for Telegram's Bot API

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Telegram Bot Ruby

Moviemagnetbot
🤖 telegram bot for movies
Stars: ✭ 39 (-96.25%)
Mutual labels:  telegram-bot, telegram
Torpedo
Pluggable, multi-network asynchronous chat bot written in Go
Stars: ✭ 19 (-98.17%)
Mutual labels:  telegram-bot, telegram
Java Telegram Bot Api
Telegram Bot API for Java
Stars: ✭ 819 (-21.17%)
Mutual labels:  telegram-bot, telegram
Flowerss Bot
A telegram bot for rss reader. 一个支持应用内阅读的 Telegram RSS Bot。
Stars: ✭ 660 (-36.48%)
Mutual labels:  telegram-bot, telegram
Planetpython telegrambot
Django App Planet Python Telegram Bot
Stars: ✭ 42 (-95.96%)
Mutual labels:  telegram-bot, telegram
Zabbix In Telegram
Zabbix Notifications with graphs in Telegram
Stars: ✭ 710 (-31.67%)
Mutual labels:  telegram-bot, telegram
Micha
Client lib for Telegram bot api
Stars: ✭ 18 (-98.27%)
Mutual labels:  telegram-bot, telegram
Mtproto
Full-native go implementation of Telegram API
Stars: ✭ 566 (-45.52%)
Mutual labels:  telegram-bot, telegram
Tikvidbot
🎵🤖 TikTok Any Video Downloader Telegram Bot
Stars: ✭ 20 (-98.08%)
Mutual labels:  telegram-bot, telegram
Pytelbot
A playful bot in telegram
Stars: ✭ 12 (-98.85%)
Mutual labels:  telegram-bot, telegram
Telegram Api
Complete async capable Telegram bot API implementation for PHP7
Stars: ✭ 650 (-37.44%)
Mutual labels:  telegram-bot, telegram
Telegram Php
Stars: ✭ 31 (-97.02%)
Mutual labels:  telegram-bot, telegram
Telegram Sms
An SMS-forwarding Robot Running on Your Android Device.
Stars: ✭ 641 (-38.31%)
Mutual labels:  telegram-bot, telegram
Informer
A Telegram Mass Surveillance Bot in Python
Stars: ✭ 745 (-28.3%)
Mutual labels:  telegram-bot, telegram
Telegram Bot
Rust Library for creating a Telegram Bot
Stars: ✭ 633 (-39.08%)
Mutual labels:  telegram-bot, telegram
Mypackbot
🤖 Your own unlimited pack of Telegram-stickers
Stars: ✭ 18 (-98.27%)
Mutual labels:  telegram-bot, telegram
Shell Bot
🤖 Telegram bot that executes commands and sends the live output
Stars: ✭ 470 (-54.76%)
Mutual labels:  telegram-bot, telegram
Telegraf
Modern Telegram Bot Framework for Node.js
Stars: ✭ 5,178 (+398.36%)
Mutual labels:  telegram-bot, telegram
Xye Bot
Бот Хуификатор
Stars: ✭ 26 (-97.5%)
Mutual labels:  telegram-bot, telegram
Rssbot
Lightweight Telegram RSS bot for notifications only. 用于消息通知的轻量级 Telegram RSS 机器人
Stars: ✭ 952 (-8.37%)
Mutual labels:  telegram-bot, telegram

telegram-bot-ruby

Ruby wrapper for Telegram's Bot API.

Gem Version Maintainability Build Status Say Thanks!

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 |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}")
    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. All methods are available in both snake_case and camelCase notations.

Same thing about message object - it implements Message spec, so you always know what to expect from it.

Webhooks

If you are going to use webhooks instead of long polling, you need to implement your own webhook callbacks server. Take a look at this repo as an example.

Proxy

As some countries block access to Telegram, you can set up your own proxy and use it to access Telegram API. In this case you need to configure API url:

Telegram::Bot::Client.run(token, url: 'https://proxy.example.com') do |bot|
  # ...
end

Custom keyboards

You can use your own custom keyboards. Here is an example:

bot.listen do |message|
  case message.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.send_message(chat_id: message.chat.id, text: question, reply_markup: answers)
  when '/stop'
    # See more: https://core.telegram.org/bots/api#replykeyboardremove
    kb = Telegram::Bot::Types::ReplyKeyboardRemove.new(remove_keyboard: true)
    bot.api.send_message(chat_id: message.chat.id, text: 'Sorry to see you go :(', reply_markup: kb)
  end
end

Furthermore, you can ask user to share location or phone number using KeyboardButton:

bot.listen do |message|
  kb = [
    Telegram::Bot::Types::KeyboardButton.new(text: 'Give me your phone number', request_contact: true),
    Telegram::Bot::Types::KeyboardButton.new(text: 'Show me your location', request_location: true)
  ]
  markup = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: kb)
  bot.api.send_message(chat_id: message.chat.id, text: 'Hey!', reply_markup: markup)
end

Inline keyboards

Bot API 2.0 brought us new inline keyboards. Example:

bot.listen do |message|
  case message
  when Telegram::Bot::Types::CallbackQuery
    # Here you can handle your callbacks from inline buttons
    if message.data == 'touch'
      bot.api.send_message(chat_id: message.from.id, text: "Don't touch me!")
    end
  when Telegram::Bot::Types::Message
    kb = [
      Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Go to Google', url: 'https://google.com'),
      Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Touch me', callback_data: 'touch'),
      Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Switch to inline', switch_inline_query: 'some text')
    ]
    markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
    bot.api.send_message(chat_id: message.chat.id, text: 'Make a choice', reply_markup: markup)
  end
end

Inline bots

If you are going to create inline bot, check the example below:

bot.listen do |message|
  case message
  when Telegram::Bot::Types::InlineQuery
    results = [
      [1, 'First article', 'Very interesting text goes here.'],
      [2, 'Second article', 'Another interesting text here.']
    ].map do |arr|
      Telegram::Bot::Types::InlineQueryResultArticle.new(
        id: arr[0],
        title: arr[1],
        input_message_content: Telegram::Bot::Types::InputTextMessageContent.new(message_text: arr[2])
      )
    end

    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

Now, with inline mode enabled, your message object can be an instance of Message, InlineQuery or ChosenInlineResult. That's why you need to check type of each message and decide how to handle it.

Using answer_inline_query you can send query results to user. results field must be an array of query result objects.

File upload

Your bot can even upload files (photos, audio, documents, stickers, video) to Telegram servers. Just like this:

bot.listen do |message|
  case message.text
  when '/photo'
    bot.api.send_photo(chat_id: message.chat.id, photo: Faraday::UploadIO.new('~/Desktop/jennifer.jpg', 'image/jpeg'))
  end
end

Logging

By default, bot doesn't log anything (uses NullLoger). You can change this behavior and provide your own logger class. See example below:

Telegram::Bot::Client.run(token, logger: Logger.new($stderr)) do |bot|
  bot.logger.info('Bot has been started')
  bot.listen do |message|
    # ...
  end
end

Connection adapters

Since version 0.5.0 we rely on faraday under the hood. You can use any of supported adapters (for example, net/http/persistent):

require 'net/http/persistent'

Telegram::Bot.configure do |config|
  config.adapter = :net_http_persistent
end

Boilerplates

If you don't know how to setup database for your bot or how to use it with different languages here are some boilerplates which can help you to start faster:

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].