Class: SignalWire::Prefabs::Receptionist

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

Overview

Prefab agent for greeting callers and transferring them to departments.

agent = Receptionist.new(
  departments: [
    { 'name' => 'sales',   'description' => 'Product inquiries', 'number' => '+15551235555' },
    { 'name' => 'support', 'description' => 'Technical help',    'number' => '+15551236666' }
  ]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(departments:, name: 'receptionist', route: '/receptionist', greeting: 'Thank you for calling. How can I help you today?', **_opts) ⇒ Receptionist

Returns a new instance of Receptionist.

Raises:

  • (ArgumentError)


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

def initialize(departments:, name: 'receptionist', route: '/receptionist',
               greeting: 'Thank you for calling. How can I help you today?', **_opts)
  raise ArgumentError, 'departments must be a non-empty Array' unless departments.is_a?(Array) && !departments.empty?
  departments.each_with_index do |d, i|
    d = d.transform_keys(&:to_s)
    raise ArgumentError, "Department #{i} missing 'name'" unless d['name']
    raise ArgumentError, "Department #{i} missing 'number'" unless d['number']
  end

  @departments = departments.map { |d| d.transform_keys(&:to_s) }
  @greeting    = greeting
  @name  = name
  @route = route
end

Instance Attribute Details

#departmentsObject (readonly)

Returns the value of attribute departments.



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

def departments
  @departments
end

#greetingObject (readonly)

Returns the value of attribute greeting.



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

def greeting
  @greeting
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#routeObject (readonly)

Returns the value of attribute route.



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

def route
  @route
end

Instance Method Details

#global_dataObject



54
55
56
57
58
59
# File 'lib/signalwire/prefabs/receptionist.rb', line 54

def global_data
  {
    'departments' => @departments,
    'caller_info' => {}
  }
end

#handle_transfer(args, _raw_data) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/signalwire/prefabs/receptionist.rb', line 61

def handle_transfer(args, _raw_data)
  dept_name = args['department']
  dept = @departments.find { |d| d['name'] == dept_name }
  if dept
    result = Swaig::FunctionResult.new("Transferring you to #{dept_name} now.")
    result.connect(dept['number'])
    result
  else
    Swaig::FunctionResult.new("I couldn't find that department. Available departments: #{@departments.map { |d| d['name'] }.join(', ')}")
  end
end

#prompt_sectionsObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/signalwire/prefabs/receptionist.rb', line 43

def prompt_sections
  bullets = @departments.map { |d| "#{d['name']}: #{d['description'] || d['name']} (#{d['number']})" }
  [
    {
      'title' => 'Receptionist',
      'body' => @greeting,
      'bullets' => bullets
    }
  ]
end

#toolsObject



39
40
41
# File 'lib/signalwire/prefabs/receptionist.rb', line 39

def tools
  %w[transfer_to_department collect_caller_info]
end