File

Tools for developing bot for Telegram. Best used with Rails, but can be be used in standalone app. Supposed to be used in webhook-mode in production, and poller mode in development, but you can use poller in production if you want.

Package contains:

  • Ligthweight client to bot API (with fast and thread-safe httpclient is under the hood.)
  • Controller with message parser. Allows to write separate methods for each command.
  • Middleware and routes helpers for production env.
  • Poller with automatic source-reloader for development env.
  • Rake tasks to update webhook urls.

Installation

Add this line to your application’s Gemfile:

And then execute:

Or install it yourself as:

Usage

Configuration

Add telegram section into secrets.yml:

Client

From now clients will be accessible with Telegram.bots[:chat] or Telegram.bots[:auction]. Single bot can be accessed with Telegram.bot or Telegram.bots[:default].

You can create clients manually with Telegram::Bot.new(token, username). Username is optional and used only to parse commands with mentions.

Client has all available methods in underscored style (answer_inline_query instead of answerInlineQuery). All this methods just post given params to specific URL.

Controller

Routes

Use telegram_webhooks helper to add routes. It will create routes for bots at “telegram/#botbot.token” path.

For Rack applications you can also use Telegram::Bot::Middleware or just call .dispatch(bot, update) on controller.

Development & Debugging

Use rake telegram:bot:poller BOT=chat to run poller. It’ll automatically load changes without restart in development env. This task will not if you don’t use telegram_webhooks.

You can run poller manually with Telegram::Bot::UpdatesPoller.start(bot, controller_class).

Deploying

Use rake telegram:bot:set_webhook to update webhook url for all configured bots. Certificate can be specified with CERT=path/to/cert.

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/printercu/telegram-bot.

Leave a Reply

Your email address will not be published. Required fields are marked *