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

chatbot_helper-telegram

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chatbot_helper-telegram

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

ChatbotHelper::Telegram Build Status Code Climate Test Coverage

ChatbotHelper::Telegram helps you build awesome Telegram chatbots without bothering much about the documentation, the message structure or other details. It is a wrapper around the Telegram Bot API.

Installation

Add this line to your application's Gemfile:

gem 'chatbot_helper-telegram'

And then execute:

$ bundle

Or install it yourself as:

$ gem install chatbot_helper-telegram

Then require it and you are good to go:

require 'chatbot_helper/telegram'

Usage

As of now, you are responsible for getting messages or other objects from the Telegram Bot API with your favorite HTTP library. We consider adding all Bot API methods to this library in one of the next versions. Please open an issue if you think that's a good idea.

An example with typhoeus:

require 'typhoeus'
require 'chatbot_helper/telegram'
require 'json'

# Your environment must have set the `TELEGRAM_TOKEN` key to your telegram api
# token. See https://core.telegram.org/bots#3-how-do-i-create-a-bot for more
# information.
telegram_token = ENV['TELEGRAM_TOKEN']
url = "https://api.telegram.org/bot#{telegram_token}/sendMessage"

# We create a message
message = {
  chat_id: 123456,
  text: "This is an example message sent by my telegram bot"
}

request = Typhoeus::Request.new(
  url,
  method: :post,
  body: JSON.generate(message),
  headers: { 'Content-Type' => 'application/json' }
)
request.run

body = request.response.body

# We create a Message object from the response body
message = ChatbotHelper::Telegram::Message.new(string: body)

# Now you can access all properties of the message object
puts message.date
puts message.from.first_name

# Yay!

The real power of this library is the update and command parsing.
Say, for example, you have set up webhooks and you want to parse the updates retrieved from these webhook requests. This is a sinatra example but you can easily convert it to Rails code.

require 'sinatra'
require 'chatbot_helper/telegram'
require 'json'

post "/#{ENV['TELEGRAM_TOKEN']}/?" do
  request.body.rewind
  payload_body = request.body.read

  # Create an instance of Update from the request body...
  update = ChatbotHelper::Telegram::Update.new(string: payload_body)

  # Return a message based on the given command
  message = {}
  if update.message
    # Access the chat id through the given message
    message[:chat_id] = update.message.chat.id

    case update.message.text
    when '/start'
      text = "Hello and welcome to my awesome bot #{update.chat.first_name}!"
      message[:text] = text
    when '/saysomething'
      text = "Why should I talk with you, #{update.chat.first_name}? Give me "\
             "a reason!"
      message[:text] = text
    end

    # Return the message which should be sent
    return halt 200, { 'Content-Type' => 'application/json' }, JSON.generate(message)
  end

  # Fallback, return nothing
  return halt 200, { 'Content-Type' => 'application/json' }, '{}'
end

You can work with nearly all Telegram Bot API available types.
One exception are the Inline mode types which has currently just partial support and will be fully available in the next version.

If you need help or have a feature request feel free and open an issue.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

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

License

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

FAQs

Package last updated on 20 Apr 2017

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