1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-07 19:35:52 +03:00

Sessions for UpdatesController

Этот коммит содержится в:
Max Melentiev
2016-02-28 21:51:59 +06:00
родитель 298947f970
Коммит bff412360e
5 изменённых файлов: 153 добавлений и 5 удалений

Просмотреть файл

@@ -94,7 +94,7 @@ bot.get_me.class # => Telegram::Bot::Types::User
```ruby
class Telegram::WebhookController < Telegram::Bot::UpdatesController
# use callbacks like in any other controllers
around_action :set_locale
around_action :with_locale
# Every update can have one of: message, inline_query & chosen_inline_result.
# Define method with same name to respond to this updates.
@@ -123,7 +123,7 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
private
def set_locale(&block)
def with_locale(&block)
I18n.with_locale(locale_for_update, &block)
end
@@ -150,6 +150,36 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
end
```
There is support for sessions using `ActiveSupport::Cache` stores.
```ruby
# configure store in env files:
config.telegram_updates_controller.session_store = :redis_store, {expires_in: 1.month}
class Telegram::WebhookController < Telegram::Bot::UpdatesController
include Telegram::Bot::UpdatesController::Session
# You can override global config
self.session_store = :file_store
def write(text = nil, *)
session[:text] = text
end
def read
session[:text]
end
private
# By default it uses bot's username and user's id as a session key.
# Chat's id is used only when `from` field is empty.
# Override `session_key` method to change this behavior.
def session_key
# In this case session will persist for user only in specific chat:
"#{bot.username}:#{chat['id']}:#{from['id']}"
end
end
```
### Routes
Use `telegram_webhooks` helper to add routes. It will create routes for bots