Home

Awesome

Telegram API for Ruby!

DUB Gem Version Code Climate Inline docs

Gitter

A Ruby wrapper that communicates with the Telegram-CLI.

Installation

Requirements

RubyGems

In order to use the telegram-rb you will need to install the gem (either manually or using Bundler), and require the library in your Ruby application:

$ gem install telegram-rb

or in your Gemfile:

# latest stable
gem 'telegram-rb', require: 'telegram'

# or track master repo
gem 'telegram-rb', github: 'ssut/telegram-rb', require: 'telegram'

Usage

The library uses EventMachine, so the logic is wrapped in EM.run.

# When using Bundler, let it load all libraries
require 'bundler' 
Bundler.require(:default) 

# Otherwise, require 'telegram', which will load its dependencies
# require 'telegram'

EM.run do
  telegram = Telegram::Client.new do |cfg, auth|
    cfg.daemon = '/path/to/tg/bin/telegram-cli'
    cfg.key = '/path/to/tg/tg-server.pub'
    cfg.config_file = '/path/to/config' # optional, default file will be used if not set
    cfg.profile = 'user2' # optional, the profiles must be configured in ~/.telegram-cli/config
    cfg.logger = Logger.new(STDOUT) # optional, default logger will be created if not set

    # optional properties, could be used for authorization/registration telegram accounts
    auth.phone_number = '<valid phone number>'
    auth.confirmation_code = -> { '<received confirmation code via sms or call>' }
    auth.register = { # required in case with registration new telegram account
      first_name: '<user first name>',
      last_name: '<user last name>'
    }
  end

  telegram.connect do
    # This block will be executed when initialized.
    
    # See your telegram profile
    puts telegram.profile

    telegram.contacts.each do |contact|
      puts contact
    end
    
    telegram.chats.each do |chat|
      puts chat
    end
    
    # Event listeners
    # When you've received a message:
    telegram.on[Telegram::EventType::RECEIVE_MESSAGE] = Proc.new do |event|
      # `tgmessage` is TelegramMessage instance
      puts event.tgmessage
    end 

    # When you've sent a message:
    telegram.on[Telegram::EventType::SEND_MESSAGE] = Proc.new do |event|
      puts event
    end

    # When connection is closed:
    telegram.on_disconnect = Proc.new do
      puts 'Connection with telegram-cli is closed'
    end
  end
end

Documentation

You can check documentation from here!

My goal is to have the code fully documentated for the project, so developers can use this library easily!

Coverage (TODO)

Contributing

If there are bugs or things you would like to improve, fork the project, and implement your awesome feature or patch in its own branch, then send me a pull request here!

License

telegram-rb is licensed under the MIT License.

The MIT License (MIT)

Copyright (c) 2015 SuHun Han

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.