README.md in telegram-bot-0.9.0.alpha2 vs README.md in telegram-bot-0.9.0

- old
+ new

@@ -15,11 +15,12 @@ [httpclient](https://github.com/nahi/httpclient) 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. -- Async requests for Telegram and/or Botan API. Let the queue adapter handle errors! +- __[Async mode](#async-mode)__ for Telegram and/or Botan API. + 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! @@ -130,10 +131,11 @@ # `on_` to avoid conflicts. def start(data = nil, *) # do_smth_with(data) # There are `chat` & `from` shortcut methods. + # For callback queries `chat` if taken from `message` when it's available. response = from ? "Hello #{from['username']}!" : 'Hi there!' # There is `respond_with` helper to set `chat_id` from received message: respond_with :message, text: response # `reply_with` also sets `reply_to_message_id`: reply_with :photo, photo: File.open('party.jpg') @@ -244,11 +246,11 @@ context_to_action! # It'll use context value as action name for all contexts which miss handlers. end ``` -You can use `CallbackQueyContext` in the similar way to split `#callback_query` into +You can use `CallbackQueryContext` in the similar way to split `#callback_query` into several specific methods. It doesn't require session support, and takes context from data. If data has a prefix with colon like this `my_ctx:smth...` it'll call `my_ctx_callback_query('smth...')` when there is such action method. Otherwise it'll call `callback_query('my_ctx:smth...')` as usual. @@ -332,10 +334,27 @@ There are also some helpers for controller tests. Check out `telegram/bot/updates_controller/rspec_helpers` and `telegram/bot/updates_controller/testing`. +Built-in RSpec matchers will help you to write tests fast: + +```ruby +include Telegram::Bot::RSpec::ClientMatchers # no need if you already use controller herlpers + +expect(&process_update).to send_telegram_message(bot, /msg regexp/, some: :option) +expect(&process_update). + to make_telegram_request(bot, :sendMessage, hash_including(text: 'msg text')) + +# controller specs are even simplier: +describe '#start' do + subject { -> { dispatch_message '/start' } } + it { should respond_with_message(/Hello/) } +end +# See sample app for more examples. +``` + ### Deploying Use `rake telegram:bot:set_webhook` to update webhook url for all configured bots. Certificate can be specified with `CERT=path/to/cert`. @@ -382,13 +401,30 @@ - Process updates very fast, without waiting for telegram and botan 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. 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 +with Async adapter by default, and there is +[Sucker Punch](https://github.com/brandonhilkert/sucker_punch) for Rails 4. + +Be aware of some limitations: + +- Client will not return API response. +- Sending files is not available in async mode [now], + because them can not be serialized. + +To disable async mode for the block of code use `bot.async(false) { bot.send_photo }`. +Yes, it's threadsafe too. ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.