Class: FlowChat::Intercom::Middleware::ChoiceMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_chat/intercom/middleware/choice_mapper.rb

Overview

Maps an Intercom reply back to the choice it belongs to.

Intercom has no interactive buttons, so the renderer writes the options out as a numbered list and asks the reader to reply with a number. That number has to come back to the choice key the flow branches on, the same job USSD's mapper does for a handset.

A number is the only thing that resolves, as on USSD, and that is what the message asks for in as many words. It is also what makes this mapper safe by construction rather than by care: positions are unique whatever the labels say, so there is no equivalence under which two choices could collapse and nothing to check them against.

Labels were matched too, case insensitively. That is what needed the care - two choices reading the same, or differing only in case, folded onto one entry and the second could not be picked by name at all. On a screen that prints a number beside every option and asks for one, the number already does that job unambiguously.

Flow:

  1. Flow returns choices with their own keys (e.g. => "Sales")
  2. This middleware numbers them and remembers what each number meant
  3. The renderer writes "1. Sales" and asks for a number
  4. The reader replies "1"
  5. This middleware turns it back into "sales"
  6. The flow sees its own key

Constant Summary collapse

SESSION_KEY =
"intercom.choice_mapping"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ChoiceMapper

Returns a new instance of ChoiceMapper.



33
34
35
36
# File 'lib/flow_chat/intercom/middleware/choice_mapper.rb', line 33

def initialize(app)
  @app = app
  FlowChat.logger.debug { "Intercom::ChoiceMapper: Initialized Intercom choice mapping middleware" }
end

Instance Method Details

#call(context) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/flow_chat/intercom/middleware/choice_mapper.rb', line 38

def call(context)
  @context = context
  @session = context.session

  resolve_input

  type, prompt, choices, media = @app.call(context)

  choices = remember(choices)

  [type, prompt, choices, media]
end