README.md in telegram-bot-0.14.0 vs README.md in telegram-bot-0.14.1

- old
+ new

@@ -2,11 +2,11 @@ [![Gem Version](https://badge.fury.io/rb/telegram-bot.svg)](http://badge.fury.io/rb/telegram-bot) [![Code Climate](https://codeclimate.com/github/telegram-bot-rb/telegram-bot/badges/gpa.svg)](https://codeclimate.com/github/telegram-bot-rb/telegram-bot) [![Build Status](https://travis-ci.org/telegram-bot-rb/telegram-bot.svg)](https://travis-ci.org/telegram-bot-rb/telegram-bot) -__Breaking changes in v0.14!__ See [upgrading guide](https://github.com/telegram-bot-rb/telegram-bot/wiki/Upgrading-to-0.14). +__Breaking changes in v0.14!__ See [upgrade guide](https://github.com/telegram-bot-rb/telegram-bot/wiki/Upgrading-to-0.14). Tools for developing Telegram bots. Best used with Rails, but can be used in [standalone app](https://github.com/telegram-bot-rb/telegram-bot/wiki/Not-rails-application). Supposed to be used in webhook-mode in production, and poller-mode in development, but you can use poller in production if you want. @@ -17,11 +17,11 @@ [httpclient](https://github.com/nahi/httpclient) under the hood). - Controller with message parser: define methods for commands, not `case` branches. - Middleware and routes helpers for production env. - Poller with automatic source-reloader for development env. - Rake tasks to update webhook urls. -- __[Async mode](#async-mode)__ for Telegram and/or Botan API. +- __[Async mode](#async-mode)__. Let the queue adapter handle network errors! Here is sample [telegram_bot_app](https://github.com/telegram-bot-rb/telegram_bot_app) with session, keyboards and inline queries. Run it on your local machine in 1 minute! @@ -53,13 +53,32 @@ require 'telegram/bot' ``` ## Usage +### Configuration + +While clients can be instantiated explicitly, there is `Telegram.bots_config=` method +to configure app-wide clients, which are accessible via `Telegram.bots`. +It accepts hash of `{bot_id: bot_config}`, and there is special id `:default` +which is used for `Telegram.bot`. + +```ruby +Telegram.bots_config = { + default: DEFAULT_BOT_TOKEN, + chat: {token: CHAT_BOT_TOKEN, username: 'chatbot'}, +} + +Telegram.bot.get_updates +Telegram.bot == Telegram.bots[:default] # true +Telegram.bots[:chat].send_message(...) +``` + ### Configuration in Rails app -Add `telegram` section into `secrets.yml`: +In Rails app `Telegram.bots_config` is read from `secrets.yml` automatically +from `telegram` section: ```yml development: telegram: # Single bot can be specified like this @@ -77,10 +96,19 @@ auction: token: TOKEN_2 username: ChatBot ``` +For Rails >= 5.2 `Telegram::Bot` searches for config first in credentials and then in secrets. +To use credentials as config store, add telegram section to credentials instead of secrets using +`rails credentials:edit`. In this case be aware of that [Rails may not load +credentials in dev environment by default](https://github.com/telegram-bot-rb/telegram-bot/issues/74#issuecomment-384205609). + +I suggest not using Rails 5.2 credentials because it can lead to leakage of sesitive data +and it's more difficult to use in multiple environments. See +[secure_credentials](https://github.com/printercu/secure_credentials) gem for better option. + 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]`. ### Client @@ -204,11 +232,11 @@ ``` #### Optional typecasting You can enable typecasting of `update` with `telegram-bot-types` by including -`Telegram::Bot::UpdatesPoller::TypedUpdate`: +`Telegram::Bot::UpdatesController::TypedUpdate`: ```ruby class Telegram::WebhookController < Telegram::Bot::UpdatesController include Telegram::Bot::UpdatesController::TypedUpdate @@ -492,59 +520,23 @@ While webhooks-mode is prefered, poller still can be used in production. See [comparison and examples](https://github.com/telegram-bot-rb/telegram-bot/wiki/Deployment) for details. -### Botan.io metrics - -Initialize with `bot = Telegram::Bot::Client.new(token, botan: 'botan token')` -or just add `botan` key in `secrets.yml`: - -```yml - telegram: - bot: - token: bot_token - botan: botan_token -``` - -Access to Botan client with `bot.botan`. -Use `bot.botan.track(event, uid, payload)` to track events. - -There are some helpers for controllers in `Telegram::Bot::Botan::ControllerHelpers`: - -```ruby -class Telegram::WebhookController < Telegram::Bot::UpdatesController - include Telegram::Bot::Botan::ControllerHelpers - - # This will track with event: action_name & data: payload - before_action :botan_track_action - - def smth(*) - # This will track event for current user only when botan is configured. - botan_track :my_event, custom_data - - # or get access directly to botan client: - botan.track(...) - end -end -``` - -There is no stubbing for botan clients, so don't set botan token in tests. - ### Async mode There is built in support for async requests using ActiveJob. Without Rails you can implement your own worker class to handle such requests. This allows: -- Process updates very fast, without waiting for telegram and botan responses. +- Process updates very fast, without waiting for telegram responses. - Handle and retry network and other errors with queue adapter. - ??? Instead of performing request instantly client serializes it, pushes to queue, and immediately return control back. The job is then fetched with a worker and real API request is performed. And this all is absolutely transparent for the app. -To enable this mode add `async: true` to bot's and botan's config. +To enable this mode add `async: true` to bot's config. For more information and custom configuration check out [docs](http://www.rubydoc.info/github/telegram-bot-rb/telegram-bot/master/Telegram/Bot/Async) or [source](https://github.com/telegram-bot-rb/telegram-bot/blob/master/lib/telegram/bot/async.rb). If you want async mode, but don't want to setup queue, know that Rails 5 are shipped