worldweather

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2019 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package worldweather is an reference implementation that provides relatively practical sarah.CommandProps.

This illustrates the use of user's conversational context, sarah.UserContext. When weather API returns response that indicates input error, this command returns text message along with a sarah.UserContext so the user's next input will be directly fed to the designated function, which actually is equivalent to second command call in this time. To see detailed implementation, read corresponding code where this command is calling slack.NewStringResponseWithNext.

When this sarah.CommandProps is passed to sarah.Runner, sarah.Runner tries to read configuration file and map content to weather.CommandConfig. Setup should be somewhat like below:

options := sarah.NewRunnerOptions

options.Append(sarah.WithCommandProps(hello.SlackProps))
options.Append(sarah.WithCommandProps(echo.SlackProps))
options.Append(sarah.WithCommandProps(worldweather.SlackProps))

// Config.PluginConfigRoot must be set to read configuration file for this command.
// Runner searches for configuration file located at config.PluginConfigRoot + "/slack/weather.(yaml|yml|json)".
config := sarah.NewConfig()
config.PluginConfigRoot = "/path/to/config/" // Or do yaml.Unmarshal(fileBuf, config), json.Unmarshal(fileBuf, config)
runner, err := sarah.NewRunner(config, options.Arg())

Index

Constants

This section is empty.

Variables

View Source
var MatchPattern = regexp.MustCompile(`^\.weather`)

MatchPattern defines regular expression pattern that is checked against user input

View Source
var SlackProps = sarah.NewCommandPropsBuilder().
	BotType(slack.SLACK).
	Identifier("weather").
	ConfigurableFunc(NewCommandConfig(), SlackCommandFunc).
	InputExample(".weather tokyo").
	MatchPattern(MatchPattern).
	MustBuild()

SlackProps provide a set of command configuration variables for weather command. Since this sets *CommandConfig in ConfigurableFunc, configuration file is observed by Runner and CommandConfig is updated on file change. Weather command is re-built on configuration update.

Functions

func SlackCommandFunc

func SlackCommandFunc(ctx context.Context, input sarah.Input, config sarah.CommandConfig) (*sarah.CommandResponse, error)

SlackCommandFunc is a function that satisfies sarah.CommandConfig type. This can be fed to CommandPropsBuilder.ConfigurableFunc.

Types

type Astronomy

type Astronomy struct {
	Sunrise  string `json:"sunrise"`
	Sunset   string `json:"sunset"`
	MoonRise string `json:"moonrise"`
	MoonSet  string `json:"moonset"`
}

Astronomy represents astronomical information.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a API client for World Weather.

func NewClient

func NewClient(config *Config) *Client

NewClient creates and returns new API client with given Config struct.

func (*Client) Get

func (client *Client) Get(ctx context.Context, apiType string, queryParams *url.Values, data interface{}) error

Get makes HTTP GET request to World Weather API endpoint.

func (*Client) LocalWeather

func (client *Client) LocalWeather(ctx context.Context, location string) (*LocalWeatherResponse, error)

LocalWeather fetches given location's weather.

type CommandConfig

type CommandConfig struct {
	APIKey string `yaml:"api_key"`
}

CommandConfig contains some configuration variables for weather command.

func NewCommandConfig

func NewCommandConfig() *CommandConfig

NewCommandConfig creates and returns CommandConfig with default settings. To override default settings, pass the returned value to (json|yaml).Unmarshal or do this manually.

type CommonData

type CommonData struct {
	Error []*ErrorDescription `json:"error"`
}

CommonData represents common response fields returned as part of API response.

func (*CommonData) HasError

func (data *CommonData) HasError() bool

HasError tells if the response contains any error.

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config contains some configuration variables for World Weather.

func NewConfig

func NewConfig(apiKey string) *Config

NewConfig returns initialized Config struct with default settings. APIKey is empty at this point. This can be set by feeding this instance to json.Unmarshal/yaml.Unmarshal, or by direct assignment.

type CurrentCondition

type CurrentCondition struct {
	ObservationTime       string                `json:"observation_time"`
	Temperature           int                   `json:"temp_C,string"`
	FeelingTemperature    int                   `json:"FeelsLikeC,string"`
	WindSpeed             int                   `json:"windspeedKmph,string"`
	WindDirection         int                   `json:"winddirDegree,string"`
	WindDirectionCardinal string                `json:"winddir16Point"`
	WeatherCode           string                `json:"weatherCode"`
	WeatherIcon           []*WeatherIcon        `json:"weatherIconUrl"`
	Description           []*WeatherDescription `json:"weatherDesc"`
	Precipitation         float32               `json:"precpMM,string"`     // Precipitation in mm
	Humidity              int                   `json:"humidity,string"`    // Humidity in percentage
	Visibility            int                   `json:"visibility,string"`  // Visibility in kilometres
	Pressure              int                   `json:"pressure,string"`    // Atmospheric pressure in millibars
	CloudCover            int                   `json:"cloudcocver,string"` // Cloud cover amount in percentage (%)
}

CurrentCondition represents current weather condition returned by API.

type ErrorDescription

type ErrorDescription struct {
	Message string `json:"msg"`
}

ErrorDescription represents error that returned by World Weather API. `{ "data": { "error": [ {"msg": "Unable to find any matching weather location to the query submitted!" } ] }}`

type HourlyForecastTime

type HourlyForecastTime struct {
	OriginalValue string
	DisplayTime   string
	Hour          int
}

HourlyForecastTime is a time representation for hourly forecast.

func (*HourlyForecastTime) UnmarshalText

func (t *HourlyForecastTime) UnmarshalText(b []byte) error

UnmarshalText converts time value returned by API to convenient form.

type HourlyWeather

type HourlyWeather struct {
	Time                  HourlyForecastTime    `json:"time"`         // hhmm format
	Temperature           int                   `json:"tempC,string"` // not temp_C
	FeelingTemperature    int                   `json:"FeelsLikeC,string"`
	WindSpeed             int                   `json:"windspeedKmph,string"`
	WindDirection         int                   `json:"winddirDegree,string"`
	WindDirectionCardinal string                `json:"winddir16Point"`
	WeatherCode           string                `json:"weatherCode"`
	WeatherIcon           []*WeatherIcon        `json:"weatherIconUrl"`
	Description           []*WeatherDescription `json:"weatherDesc"`
	Precipitation         float32               `json:"precpMM,string"`     // Precipitation in mm
	Humidity              int                   `json:"humidity,string"`    // Humidity in percentage
	Visibility            int                   `json:"visibility,string"`  // Visibility in kilometres
	Pressure              int                   `json:"pressure,string"`    // Atmospheric pressure in millibars
	CloudCover            int                   `json:"cloudcocver,string"` // Cloud cover amount in percentage (%)
}

HourlyWeather represents hourly weather information.

type LocalWeatherResponse

type LocalWeatherResponse struct {
	Data *WeatherData `json:"data"`
}

LocalWeatherResponse represents local weather information. https://developer.worldweatheronline.com/api/docs/local-city-town-weather-api.aspx#data_element

type Request

type Request struct {
	Type  string `json:"type"`
	Query string `json:"query"`
}

Request represents clients request.

type Weather

type Weather struct {
	Astronomy []*Astronomy     `json:"astronomy"`
	Date      string           `json:"date"` // 2016-09-04
	MaxTempC  int              `json:"maxTempC,string"`
	MaxTempF  int              `json:"maxTempF,string"`
	MinTempC  int              `json:"minTempC,string"`
	MinTempF  int              `json:"minTempF,string"`
	UV        int              `json:"uvindex,string"`
	Hourly    []*HourlyWeather `json:"hourly"`
}

Weather represents set of weather information.

type WeatherData

type WeatherData struct {
	CommonData
	Request          []*Request          `json:"request"`
	CurrentCondition []*CurrentCondition `json:"current_condition"`
	Weather          []*Weather          `json:"weather"`
}

WeatherData represents set of weather information.

type WeatherDescription

type WeatherDescription struct {
	Content string `json:"value"`
}

WeatherDescription represents weather description.

type WeatherIcon

type WeatherIcon struct {
	URL string `json:"value"`
}

WeatherIcon is an icon url that represents corresponding weather.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL