Skip to content

A charismatic Ruby client for Telegram's Bot API

License

Notifications You must be signed in to change notification settings

XoctocatX/telegram_bot

This branch is 1 commit behind eljojo/telegram_bot:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5df9bed · Aug 9, 2018

History

66 Commits
Jun 30, 2015
Aug 6, 2018
Aug 6, 2018
Aug 6, 2018
Nov 22, 2017
Mar 24, 2018
Aug 6, 2018
Jun 30, 2015
Jun 24, 2015
Jun 24, 2015
Jun 9, 2018
Apr 26, 2017
Aug 6, 2018

Repository files navigation

TelegramBot

A charismatic ruby client for Telegram's Bot API.

Write your own Telegram Bot using Ruby! Yay!

Currently under heavy development. Please collaborate with your questions, ideas or problems!

Installation

Add this line to your application's Gemfile (currently under development):

gem 'telegram_bot'

And then execute:

$ bundle

Usage

Here's an example:

require 'telegram_bot'

bot = TelegramBot.new(token: '[YOUR TELEGRAM BOT TOKEN GOES HERE]')
bot.get_updates(fail_silently: true) do |message|
  puts "@#{message.from.username}: #{message.text}"
  command = message.get_command_for(bot)

  message.reply do |reply|
    case command
    when /greet/i
      reply.text = "Hello, #{message.from.first_name}!"
    else
      reply.text = "#{message.from.first_name}, have no idea what #{command.inspect} means."
    end
    puts "sending #{reply.text.inspect} to @#{message.from.username}"
    reply.send_with(bot)
  end
end

Here's a sample output:

$ bundle exec ruby bot.rb
@eljojo: greet
sending "Hello, José!" to @eljojo
@eljojo: heeeeeeeeya!
sending "José, have no idea what \"heeeeeeeeya!\" means." to @eljojo

Example

How do I get a Bot Token

Talk to the @BotFather. You can find more info here.

How to get Token

What else can it do?

you can pass options to the bot initializer:

bot = TelegramBot.new(token: 'abc', logger: Logger.new(STDOUT), offset: 123, timeout: 20)

if you don't want to start the loop, don't pass a block to #get_updates and you'll get an array with the latest messages:

messages = bot.get_updates(timeout: 30, offset: 123)

Because things can go wrong sometimes with the API, there's a fail_silently option that you can pass to #get_updates like this:

bot.get_updates(fail_silently: true) do |message|
  puts message.text
end

A message has several attributes:

message = bot.get_updates.last

# message data
message.text # "hello moto"
message.date # Wed, 01 Jul 2015 09:52:54 +0200 (DateTime)

# reading user
message.from # TelegramBot::User
message.from.first_name # "Homer"
message.from.last_name  # "Simpson"
message.from.username   # "mr_x"

# channel
message.channel.id # 123123123 (telegram's id)

# reply
message.reply do |reply|
  reply.text = "homer please clean the garage"
  reply.send_with(bot)
end
# or
reply = message.reply
reply.text = "i'll do it after going to moe's"
bot.send_message(reply)

To send message to specific channel you could do following:

bot = TelegramBot.new(token: '[YOUR TELEGRAM BOT TOKEN GOES HERE]')
channel = TelegramBot::Channel.new(id: channel_id)
message = TelegramBot::OutMessage.new
message.chat = channel
message.text = 'Some message'

message.send_with(bot)

Also you may pass additional options described in API Docs

message.parse_mode = 'Markdown'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/eljojo/telegram_bot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

A charismatic Ruby client for Telegram's Bot API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%