зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-03 17:55:52 +03:00
Allow use different sessions for MessageContext
Этот коммит содержится в:
@@ -7,14 +7,11 @@ module Telegram
|
|||||||
|
|
||||||
include Session
|
include Session
|
||||||
|
|
||||||
included do
|
|
||||||
# As we use before_action context is cleared anyway,
|
|
||||||
# no matter we used it or not.
|
|
||||||
singleton_class.send :attr_reader, :context_handlers, :context_to_action
|
|
||||||
@context_handlers = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
def context_handlers
|
||||||
|
@_context_handlers ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
# Registers handler for context.
|
# Registers handler for context.
|
||||||
#
|
#
|
||||||
# context_handler :rename do |*|
|
# context_handler :rename do |*|
|
||||||
@@ -39,6 +36,8 @@ module Telegram
|
|||||||
context_handlers[context] = action || context
|
context_handlers[context] = action || context
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr_reader :context_to_action
|
||||||
|
|
||||||
# Use it to use context value as action name for all contexts
|
# Use it to use context value as action name for all contexts
|
||||||
# which miss handlers.
|
# which miss handlers.
|
||||||
# For security reasons it supports only action methods and will
|
# For security reasons it supports only action methods and will
|
||||||
@@ -59,10 +58,16 @@ module Telegram
|
|||||||
# according to previous request.
|
# according to previous request.
|
||||||
attr_reader :context
|
attr_reader :context
|
||||||
|
|
||||||
|
# Controller may have multiple sessions, let it be possible
|
||||||
|
# to select session for message context.
|
||||||
|
def message_context_session
|
||||||
|
session
|
||||||
|
end
|
||||||
|
|
||||||
# Fetches context and finds handler for it. If message has new command,
|
# Fetches context and finds handler for it. If message has new command,
|
||||||
# it has higher priority than contextual action.
|
# it has higher priority than contextual action.
|
||||||
def action_for_message
|
def action_for_message
|
||||||
val = session.delete(:context)
|
val = message_context_session.delete(:context)
|
||||||
@context = val && val.to_sym
|
@context = val && val.to_sym
|
||||||
super || context && begin
|
super || context && begin
|
||||||
handler = handler_for_context
|
handler = handler_for_context
|
||||||
@@ -72,7 +77,7 @@ module Telegram
|
|||||||
|
|
||||||
# Save context for the next request.
|
# Save context for the next request.
|
||||||
def save_context(context)
|
def save_context(context)
|
||||||
session[:context] = context
|
message_context_session[:context] = context
|
||||||
end
|
end
|
||||||
|
|
||||||
def handler_for_context
|
def handler_for_context
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user