From b60fcd51a39a53285e8e5b42496590dc1ec40e5d Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 6 May 2023 22:52:45 +0200 Subject: [PATCH] add currency command --- cmd/nourybot/commands.go | 15 +++++++++++++++ internal/commands/commands.go | 5 ++--- internal/commands/currency.go | 30 ++++++++++++++++++++++++++++++ internal/commands/weather.go | 2 +- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 internal/commands/currency.go diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 096fdbc..a02b9d3 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -29,6 +29,21 @@ func (app *Application) ParseCommand(evt *event.Event) { app.SendText(evt, "XD !") return + case "currency": + if msgLen < 4 { + app.SendText(evt, "Not enough arguments provided") + return + } else { + if resp, err := commands.Currency(cmdParams[1], cmdParams[2], cmdParams[4]); err != nil { + app.Log.Error().Err(err).Msg("Failed to handle Xkcd command") + app.SendText(evt, "Something went wrong.") + return + } else { + app.SendText(evt, resp) + return + } + } + case "ping": if resp, err := commands.Ping(); err != nil { app.Log.Error().Err(err).Msg("Failed to handle Ping command") diff --git a/internal/commands/commands.go b/internal/commands/commands.go index b518c94..0483496 100644 --- a/internal/commands/commands.go +++ b/internal/commands/commands.go @@ -3,7 +3,6 @@ package commands import "errors" var ( - ErrEnvFileNotFound = errors.New(".env file not found") - ErrInternalServerError = errors.New("internal server error") - ErrLocationNotFound = errors.New("location not found") + ErrInternalServerError = errors.New("internal server error") + ErrWeatherLocationNotFound = errors.New("location not found") ) diff --git a/internal/commands/currency.go b/internal/commands/currency.go new file mode 100644 index 0000000..f6ca31e --- /dev/null +++ b/internal/commands/currency.go @@ -0,0 +1,30 @@ +package commands + +import ( + "fmt" + "io" + "net/http" +) + +func Currency(currAmount, currFrom, currTo string) (string, error) { + basePath := "https://decapi.me/misc/currency/" + from := fmt.Sprintf("?from=%s", currFrom) + to := fmt.Sprintf("&to=%s", currTo) + value := fmt.Sprintf("&value=%s", currAmount) + + // https://decapi.me/misc/currency/?from=usd&to=usd&value=10 + resp, err := http.Get(fmt.Sprint(basePath + from + to + value)) + if err != nil { + return "", ErrInternalServerError + } + + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", ErrInternalServerError + } + + reply := string(body) + return reply, nil +} diff --git a/internal/commands/weather.go b/internal/commands/weather.go index c96db6a..6f20017 100644 --- a/internal/commands/weather.go +++ b/internal/commands/weather.go @@ -29,7 +29,7 @@ func Weather(location string) (string, error) { // Longitude and Latitude are returned as 0 when the supplied location couldn't be // assigned to a OpenWeatherMap location. if w.GeoPos.Longitude == 0 && w.GeoPos.Latitude == 0 { - return "", ErrLocationNotFound + return "", ErrWeatherLocationNotFound } else { // Weather for Vilnius, LT: Feels like: 29.67°C. Currently 29.49°C with a high of 29.84°C and a low of 29.49°C, humidity: 45%, wind: 6.17m/s. reply := fmt.Sprintf("Weather for %s, %s: Feels like: %v°C. Currently %v°C with a high of %v°C and a low of %v°C, humidity: %v%%, wind: %vm/s.",