Class: SignalWire::Skills::Builtin::WeatherApiSkill

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

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, #instance_key, #required_env_vars, #supports_multiple_instances?, #version

Constructor Details

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

Instance Method Details

#descriptionObject



12
# File 'lib/signalwire/skills/builtin/weather_api.rb', line 12

def description; 'Get current weather information from WeatherAPI.com'; end

#get_parameter_schemaObject



78
79
80
81
82
83
84
# File 'lib/signalwire/skills/builtin/weather_api.rb', line 78

def get_parameter_schema
  {
    'api_key'          => { 'type' => 'string', 'required' => true, 'hidden' => true, 'env_var' => 'WEATHER_API_KEY' },
    'tool_name'        => { 'type' => 'string', 'default' => 'get_weather' },
    'temperature_unit' => { 'type' => 'string', 'default' => 'fahrenheit', 'enum' => %w[fahrenheit celsius] }
  }
end

#nameObject



11
# File 'lib/signalwire/skills/builtin/weather_api.rb', line 11

def name;        'weather_api'; end

#register_toolsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/signalwire/skills/builtin/weather_api.rb', line 22

def register_tools
  if @temp_unit == 'celsius'
    temp_field      = 'temp_c'
    feels_field     = 'feelslike_c'
    unit_name       = 'Celsius'
  else
    temp_field      = 'temp_f'
    feels_field     = 'feelslike_f'
    unit_name       = 'Fahrenheit'
  end

  response_template =
    "Tell the user the current weather conditions. " \
    "Express all temperatures in #{unit_name} using natural language numbers " \
    "without abbreviations or symbols for clear text-to-speech pronunciation. " \
    "Current conditions: ${current.condition.text}. " \
    "Temperature: ${current.#{temp_field}} degrees #{unit_name}. " \
    "Wind: ${current.wind_dir} at ${current.wind_mph} miles per hour. " \
    "Cloud coverage: ${current.cloud} percent. " \
    "Feels like: ${current.#{feels_field}} degrees #{unit_name}."

  # Default to the WeatherAPI.com host; WEATHER_API_BASE_URL
  # overrides for tests and the audit fixture. The `/v1/current.json`
  # path is preserved so the audit can match on `current.json`.
  base = ENV['WEATHER_API_BASE_URL']
  base = 'https://api.weatherapi.com' if base.nil? || base.empty?
  base = base.sub(/\/$/, '')

  tool = {
    'function'    => @tool_name,
    'description' => 'Get current weather information for any location',
    'parameters'  => {
      'type' => 'object',
      'properties' => {
        'location' => { 'type' => 'string', 'description' => 'The city, state, country, or location to get weather for' }
      },
      'required' => ['location']
    },
    'data_map' => {
      'webhooks' => [
        {
          'url'    => "#{base}/v1/current.json?key=#{@api_key}&q=${lc:enc:args.location}&aqi=no",
          'method' => 'GET',
          'output' => Swaig::FunctionResult.new(response_template).to_h
        }
      ],
      'error_keys' => ['error'],
      'output' => Swaig::FunctionResult.new(
        'Sorry, I cannot get weather information right now. Please try again later or check if the location name is correct.'
      ).to_h
    }
  }

  [{ datamap: tool }]
end

#setupObject



14
15
16
17
18
19
20
# File 'lib/signalwire/skills/builtin/weather_api.rb', line 14

def setup
  @api_key   = get_param('api_key', env_var: 'WEATHER_API_KEY')
  @tool_name = get_param('tool_name', default: 'get_weather')
  @temp_unit = get_param('temperature_unit', default: 'fahrenheit')
  return false unless @api_key && !@api_key.empty?
  true
end