No commit activity in last 3 years
No release in over 3 years
An extention to the telegram-bot-ruby gem that enables responding to Telegram webhook requests directly in the response body.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 0.11.3
~> 10.0
~> 3.0
~> 0.51.0
~> 0.9.12

Runtime

 Project Readme

Telegram::Bot::ResponseBuilder

This gem extends telegram-bot-ruby by enabling responding to Telegram webhook requests directly in the response body. It introduces a Telegram::Bot::Api subclass which builds response hash from parameters and the method name instead of performing an HTTP request.

Installation

Add this line to your application's Gemfile:

gem 'telegram-bot-response_builder'

And then execute:

$ bundle

Or install it yourself as:

$ gem install telegram-bot-response_builder

Usage

Create new instance of Telegram::Bot::ResponseBuilder and invoke any API method on it:

# ...
response_builder = Telegram::Bot::ResponseBuilder.new
response_builder.deleteMessage(chat_id: chat_id, message_id: message_id)
# {:method=>"deleteMessage", :chat_id=>chat_id, :message_id=>message_id}

Example — webhook-based "echo" bot, built on top of Sinatra:

require 'sinatra'
require 'sinatra/json'

class TelegramWebhookBot < Sinatra::Base
  use Rack::PostBodyContentTypeParser
  register Sinatra::JSON

  post '/webhooks/tg' do
    message = Telegram::Bot::Types::Update.new(params).message
    response_builder = Telegram::Bot::ResponseBuilder.new
    json response_builder.send_message(chat_id: message.chat.id, text: message.text)
    # response: {"chat_id":9001,"text":"Hello, World!","method":"sendMessage"}
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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/smaximov/telegram-bot-response_builder.

License

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