Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

telegram_bot_ruby

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

telegram_bot_ruby

  • 0.1.7
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

TelegramBotRuby

telegram api doc

Installation

Add this line to your application's Gemfile:

gem 'telegram_bot_ruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install telegram_bot_ruby

Quick start

Set up client

require 'telegram_bot'

bot = TelegramBot.new(token: <token>)

bot.listen(method: :webhook, url: '/meow/meow')  # not implemented yet
# or
bot.listen(method: :poll, interval: 1)

Set up update listeners

bot.on :command, 'ping' do  # /ping
  reply 'pong'
end

bot.on :command, 'plus' do |num1, num2|  # /plus 1 2
  reply (num1.to_i + num2.to_i).to_s
end

bot.on :text 'ping' do     # plain 'ping'
  send_message 'pong'
end

# with block: false, message will keep passing through other listeners
bot.on :text, /(\d+)\+(\d+)\=\?/, block: false do |n1, n2|
  send_chat_action :typing
  send_message (n1.to_i + n2.to_i).to_s
end

# a simple history logger
bot.on :anything do |msg|
  Database.save [msg.from.username, msg.text] if !msg.text.empty?
end

Start && Stop

# register service at telegram or start long polling
trap(:INT) {
    bot.stop!
}
bot.start!

Documentation

Matchers

anything/fallback matcher

on :anything
on :fallback

text matcher

# match when message.type == :text
on :text do |txt|
end

# arguments are given by `MatchData#to_a`
on :text, /reply me with (.*)/ do |matched_text, reply_txt|
    send_message reply_txt
end

command matcher

on :command, :ping do
    reply 'pong'
end

# handling requests like `/plus 1 2`
on :command :plus do |*args|
    reply args.map(&:to_i).sum.to_s
end

# or handle all commands
on :command do |cmd, *args|
    case cmd
    when 'echo'
        reply args.join(' ')
    when 'rand'
        reply rand.to_s
    else
        reply "unknown command #{cmd}"
    end
end

Telegram Types

Check the classes that include this module:

http://www.rubydoc.info/gems/telegram_bot_ruby/TelegramBot/AutoFromMethods

Bot methods

# msg: message object, or message id
# to: chat/user object or chat/user id
TelegramBot#forward_message(msg, to, from = nil)

TelegramBot#get_me => User

CHAT_ACTIONS = [
  :typing,
  :upload_photo,
  :record_video,
  :update_video,
  :record_audio,
  :upload_audio,
  :upload_document,
  :find_location
]
# chat: {chat,user} {object,id}
# action: CHAT_ACTIONS.sample
TelegramBot#send_chat_action(chat, action)


# chat: {chat,user} {object,id}
# text: text content of the message
# disable_web_page_preview: see Telegram doc
# reply_to: message object or id
# reply_markup: a keyboard markup, see example below
send_message(chat,
             text,
             disable_web_page_preview: nil,
             reply_to: nil,
             reply_markup: nil)


# example keyboard markup
markup = {
    keyboard: [
        ["1", "2"],
        ["3", "4", "5"],
        ["6"]
    ],
    selective: false
}

Shorthand methods

shorthand methods can be used within the handler block, with the received message as context. rubydoc.info

reply(text, *args)
send_message(text, *args)
forward_message(to, *args)
send_chat_action(action, *args)

Handler context variables/methods

Other than the shorthand above, within the handler block, these contextual variable are accessible:

bot            # the current TelegramBot instance
bot.history    # request histories :: [Message]
bot.start!     # start polling
bot.stop!      # quit the bot

message        # the message just received
message.<attr> # message attributes
matcher        # current matcher object

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/shouya/telegram-bot.

License

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

FAQs

Package last updated on 11 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc