mirror of
https://github.com/lyx0/nourybot-matrix.git
synced 2024-11-13 19:49:54 +01:00
add currency command
This commit is contained in:
parent
046bb9f770
commit
b60fcd51a3
4 changed files with 48 additions and 4 deletions
|
@ -29,6 +29,21 @@ func (app *Application) ParseCommand(evt *event.Event) {
|
||||||
app.SendText(evt, "XD !")
|
app.SendText(evt, "XD !")
|
||||||
return
|
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":
|
case "ping":
|
||||||
if resp, err := commands.Ping(); err != nil {
|
if resp, err := commands.Ping(); err != nil {
|
||||||
app.Log.Error().Err(err).Msg("Failed to handle Ping command")
|
app.Log.Error().Err(err).Msg("Failed to handle Ping command")
|
||||||
|
|
|
@ -3,7 +3,6 @@ package commands
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrEnvFileNotFound = errors.New(".env file not found")
|
|
||||||
ErrInternalServerError = errors.New("internal server error")
|
ErrInternalServerError = errors.New("internal server error")
|
||||||
ErrLocationNotFound = errors.New("location not found")
|
ErrWeatherLocationNotFound = errors.New("location not found")
|
||||||
)
|
)
|
||||||
|
|
30
internal/commands/currency.go
Normal file
30
internal/commands/currency.go
Normal 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
|
||||||
|
}
|
|
@ -29,7 +29,7 @@ func Weather(location string) (string, error) {
|
||||||
// Longitude and Latitude are returned as 0 when the supplied location couldn't be
|
// Longitude and Latitude are returned as 0 when the supplied location couldn't be
|
||||||
// assigned to a OpenWeatherMap location.
|
// assigned to a OpenWeatherMap location.
|
||||||
if w.GeoPos.Longitude == 0 && w.GeoPos.Latitude == 0 {
|
if w.GeoPos.Longitude == 0 && w.GeoPos.Latitude == 0 {
|
||||||
return "", ErrLocationNotFound
|
return "", ErrWeatherLocationNotFound
|
||||||
} else {
|
} 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.
|
// 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.",
|
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.",
|
||||||
|
|
Loading…
Reference in a new issue