go-sarah: github.com/oklahomer/go-sarah/examples/simple/plugins/worldweather Index | Files

package worldweather

import "github.com/oklahomer/go-sarah/examples/simple/plugins/worldweather"

Package worldweather is a reference implementation that provides relatively practical use of sarah.CommandProps.

This illustrates the use of a user's conversational context, sarah.UserContext. When weather API returns a 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 the detailed implementation, read the corresponding code where this command is calling slack.NewResponse.

Setup can be done by importing this package since sarah.RegisterCommandProps() is called in init(). However, to read the configuration on the fly, sarah.ConfigWatcher's implementation must be set.

package main

import (
  _ "github.com/oklahomer/go-sarah/v3/examples/simple/plugins/worldweather"
  "github.com/oklahomer/go-sarah/v3/watchers"
)

func main() {
  // setup watcher
  watcher, _ := watchers.NewFileWatcher(context.TODO(), "/path/to/config/dir/")
  sarah.RegisterConfigWatcher(watcher)

  // Do the rest

}

Index

Package Files

client.go response.go weather.go

Variables

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

MatchPattern defines regular expression pattern that is checked against user input

var SlackProps = sarah.NewCommandPropsBuilder().
    BotType(slack.SLACK).
    Identifier("weather").
    ConfigurableFunc(NewCommandConfig(), SlackCommandFunc).
    Instruction(`Input ".weather" followed by city name e.g. ".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.

func SlackCommandFunc Uses

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.

type Astronomy Uses

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 Uses

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

Client is a API client for World Weather.

func NewClient Uses

func NewClient(config *Config) *Client

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

func (*Client) Get Uses

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 Uses

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

LocalWeather fetches given location's weather.

type CommandConfig Uses

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

CommandConfig contains some configuration variables for weather command.

func NewCommandConfig Uses

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 Uses

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

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

func (*CommonData) HasError Uses

func (data *CommonData) HasError() bool

HasError tells if the response contains any error.

type Config Uses

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

Config contains some configuration variables for World Weather.

func NewConfig Uses

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 Uses

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 Uses

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 Uses

type HourlyForecastTime struct {
    OriginalValue string
    DisplayTime   string
    Hour          int
}

HourlyForecastTime is a time representation for hourly forecast.

func (*HourlyForecastTime) UnmarshalText Uses

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

UnmarshalText converts time value returned by API to convenient form.

type HourlyWeather Uses

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 Uses

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 Uses

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

Request represents clients request.

type Weather Uses

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 Uses

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 Uses

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

WeatherDescription represents weather description.

type WeatherIcon Uses

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

WeatherIcon is an icon url that represents corresponding weather.

Package worldweather imports 13 packages (graph). Updated 2020-09-27. Refresh now. Tools for package owners.