Class: SignalWire::Skills::Builtin::ApiNinjasTriviaSkill

Inherits:
SkillBase
  • Object
show all
Defined in:
lib/signalwire/skills/builtin/api_ninjas_trivia.rb

Constant Summary collapse

VALID_CATEGORIES =
{
  'artliterature'     => 'Art and Literature',
  'language'          => 'Language',
  'sciencenature'     => 'Science and Nature',
  'general'           => 'General Knowledge',
  'fooddrink'         => 'Food and Drink',
  'peopleplaces'      => 'People and Places',
  'geography'         => 'Geography',
  'historyholidays'   => 'History and Holidays',
  'entertainment'     => 'Entertainment',
  'toysgames'         => 'Toys and Games',
  'music'             => 'Music',
  'mathematics'       => 'Mathematics',
  'religionmythology' => 'Religion and Mythology',
  'sportsleisure'     => 'Sports and Leisure'
}.freeze

Instance Attribute Summary

Attributes inherited from SkillBase

#agent, #logger, #params, #swaig_fields

Instance Method Summary collapse

Methods inherited from SkillBase

#cleanup, #get_global_data, #get_hints, #get_param, #get_prompt_sections, #initialize, #required_env_vars, #version

Constructor Details

This class inherits a constructor from SignalWire::Skills::SkillBase

Instance Method Details

#descriptionObject



28
# File 'lib/signalwire/skills/builtin/api_ninjas_trivia.rb', line 28

def description; 'Get trivia questions from API Ninjas'; end

#get_parameter_schemaObject



86
87
88
89
90
91
# File 'lib/signalwire/skills/builtin/api_ninjas_trivia.rb', line 86

def get_parameter_schema
  {
    'api_key'    => { 'type' => 'string', 'required' => true, 'hidden' => true, 'env_var' => 'API_NINJAS_KEY' },
    'categories' => { 'type' => 'array', 'default' => VALID_CATEGORIES.keys }
  }
end

#instance_keyObject



41
# File 'lib/signalwire/skills/builtin/api_ninjas_trivia.rb', line 41

def instance_key; "api_ninjas_trivia_#{@tool_name}"; end

#nameObject



27
# File 'lib/signalwire/skills/builtin/api_ninjas_trivia.rb', line 27

def name;        'api_ninjas_trivia'; end

#register_toolsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/signalwire/skills/builtin/api_ninjas_trivia.rb', line 43

def register_tools
  descs = @categories.map { |c| "#{c}: #{VALID_CATEGORIES[c] || c}" }
  param_desc = 'Category for trivia question. Options: ' + descs.join('; ')

  # Default to the production endpoint; API_NINJAS_BASE_URL
  # overrides the host (the audit fixture sets it to a loopback
  # address). The `/v1/trivia` path is preserved so the audit
  # can match on `trivia` in the fixture's req.path.
  base = ENV['API_NINJAS_BASE_URL']
  base = 'https://api.api-ninjas.com' if base.nil? || base.empty?
  base = base.sub(/\/$/, '')

  tool = {
    'function'    => @tool_name,
    'description' => "Get trivia questions for #{@tool_name.tr('_', ' ')}",
    'parameters'  => {
      'type' => 'object',
      'properties' => {
        'category' => { 'type' => 'string', 'description' => param_desc, 'enum' => @categories }
      },
      'required' => ['category']
    },
    'data_map' => {
      'webhooks' => [
        {
          'url'     => "#{base}/v1/trivia?category=%{args.category}",
          'method'  => 'GET',
          'headers' => { 'X-Api-Key' => @api_key },
          'output'  => Swaig::FunctionResult.new(
            'Category %{array[0].category} question: %{array[0].question} Answer: %{array[0].answer}, be sure to give the user time to answer before saying the answer.'
          ).to_h
        }
      ],
      'error_keys' => ['error'],
      'output' => Swaig::FunctionResult.new(
        'Sorry, I cannot get trivia questions right now. Please try again later.'
      ).to_h
    }
  }

  [{ datamap: tool }]
end

#setupObject



31
32
33
34
35
36
37
38
39
# File 'lib/signalwire/skills/builtin/api_ninjas_trivia.rb', line 31

def setup
  @api_key    = get_param('api_key', env_var: 'API_NINJAS_KEY')
  @tool_name  = get_param('tool_name', default: 'get_trivia')
  @categories = get_param('categories') || VALID_CATEGORIES.keys

  return false unless @api_key && !@api_key.empty?
  return false unless @categories.is_a?(Array) && !@categories.empty?
  true
end

#supports_multiple_instances?Boolean

Returns:

  • (Boolean)


29
# File 'lib/signalwire/skills/builtin/api_ninjas_trivia.rb', line 29

def supports_multiple_instances?; true; end