Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Latest commit

 

History

History
146 lines (104 loc) · 3.83 KB

README.md

File metadata and controls

146 lines (104 loc) · 3.83 KB

This gem has been archived

Hi everyone, I haven't touched this code in a long time and I decided it may be best to archive the repo and put it to rest.
I hope this project helped many people get started in bot development, I had fun working on it.

I would recommend using https://github.com/atipugin/telegram-bot-ruby.

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.