add currency command

This commit is contained in:
lyx0 2023-05-06 22:52:45 +02:00
parent 046bb9f770
commit b60fcd51a3
4 changed files with 48 additions and 4 deletions

View file

@ -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")

View file

@ -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")
ErrWeatherLocationNotFound = errors.New("location not found")
)

View file

@ -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
}

View file

@ -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.",