Class: SignalWire::Prefabs::InfoGatherer

Inherits:
Object
  • Object
show all
Defined in:
lib/signalwire/prefabs/info_gatherer.rb

Overview

Prefab agent for collecting answers to a series of questions.

agent = InfoGatherer.new(
  questions: [
    { 'key_name' => 'full_name', 'question_text' => 'What is your full name?' },
    { 'key_name' => 'email',     'question_text' => 'Email?', 'confirm' => true }
  ]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questions:, name: 'info_gatherer', route: '/info_gatherer', **_opts) ⇒ InfoGatherer

Returns a new instance of InfoGatherer.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 24

def initialize(questions:, name: 'info_gatherer', route: '/info_gatherer', **_opts)
  raise ArgumentError, 'questions must be a non-empty Array' unless questions.is_a?(Array) && !questions.empty?
  questions.each_with_index do |q, i|
    raise ArgumentError, "Question #{i} missing key_name" unless q['key_name'] || q[:key_name]
    raise ArgumentError, "Question #{i} missing question_text" unless q['question_text'] || q[:question_text]
  end

  @questions = questions.map { |q| q.transform_keys(&:to_s) }
  @name  = name
  @route = route
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 22

def name
  @name
end

#questionsObject (readonly)

Returns the value of attribute questions.



22
23
24
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 22

def questions
  @questions
end

#routeObject (readonly)

Returns the value of attribute route.



22
23
24
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 22

def route
  @route
end

Instance Method Details

#global_dataObject

Global data for initial state.



53
54
55
56
57
58
59
60
61
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 53

def global_data
  {
    'info_gatherer' => {
      'questions' => @questions,
      'question_index' => 0,
      'answers' => []
    }
  }
end

#handle_start(_args, _raw_data) ⇒ Object

Tool handler: start_questions



64
65
66
67
68
69
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 64

def handle_start(_args, _raw_data)
  q = @questions.first
  Swaig::FunctionResult.new(
    "[Question 1 of #{@questions.size}]: \"#{q['question_text']}\""
  )
end

#handle_submit(args, _raw_data) ⇒ Object

Tool handler: submit_answer



72
73
74
75
76
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 72

def handle_submit(args, _raw_data)
  answer = args['answer'] || ''
  # In a real implementation, state would be tracked via global_data.
  Swaig::FunctionResult.new("Answer recorded: #{answer}")
end

#prompt_sectionsObject

Build the prompt sections.



42
43
44
45
46
47
48
49
50
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 42

def prompt_sections
  [
    {
      'title' => 'Info Gatherer',
      'body' => 'You need to gather answers to a series of questions. ' \
                'Call start_questions to get the first question, then submit_answer after each response.'
    }
  ]
end

#toolsObject

Tool definitions this prefab provides.



37
38
39
# File 'lib/signalwire/prefabs/info_gatherer.rb', line 37

def tools
  %w[start_questions submit_answer]
end