From b3543fcced7397a4c615b90f31e79aa84622c432 Mon Sep 17 00:00:00 2001 From: lyx0 Date: Sat, 23 Oct 2021 19:48:50 +0200 Subject: [PATCH] add currency --- pkg/api/currency.go | 42 ++++++++++++++++++++++++++++++++++++++++ pkg/commands/currency.go | 16 +++++++++++++++ pkg/handlers/command.go | 3 +++ 3 files changed, 61 insertions(+) create mode 100644 pkg/api/currency.go create mode 100644 pkg/commands/currency.go diff --git a/pkg/api/currency.go b/pkg/api/currency.go new file mode 100644 index 0000000..243ac26 --- /dev/null +++ b/pkg/api/currency.go @@ -0,0 +1,42 @@ +package api + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + log "github.com/sirupsen/logrus" +) + +// reply, err := api.Currency(currAmount, currFrom, currTo) + +type currencyResponse struct { + Amount int `json:"amount"` + Base string `json:"base"` + Rates rates +} + +type rates map[string]interface{} + +func Currency(currAmount string, currFrom string, currTo string) (string, error) { + // + baseUrl := "https://api.frankfurter.app" + response, err := http.Get(fmt.Sprintf("%s/latest?amount=%s&from=%s&to=%s", baseUrl, currAmount, currFrom, currTo)) + if err != nil { + log.Error(err) + } + + responseData, err := ioutil.ReadAll(response.Body) + if err != nil { + log.Error(err) + } + + var responseObject currencyResponse + json.Unmarshal(responseData, &responseObject) + + log.Info(responseObject.Amount) + log.Info(responseObject.Base) + log.Info(responseObject.Rates) + return "", nil +} diff --git a/pkg/commands/currency.go b/pkg/commands/currency.go new file mode 100644 index 0000000..8160de5 --- /dev/null +++ b/pkg/commands/currency.go @@ -0,0 +1,16 @@ +package commands + +import ( + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/api" + "github.com/sirupsen/logrus" +) + +// commands.Currency(target, cmdParams[1], cmdParams[2], cmdParams[4], nb) +func Currency(target string, currAmount string, currFrom string, currTo string, nb *bot.Bot) { + reply, err := api.Currency(currAmount, currFrom, currTo) + if err != nil { + logrus.Info(err) + } + nb.Send(target, reply) +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 462605b..f516713 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -70,6 +70,9 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) { commands.Color(message, nb) return + // ()currency 10 gbp to usd + case "currency": + commands.Currency(target, cmdParams[1], cmdParams[2], cmdParams[4], nb) case "echo": commands.Echo(target, message.Message[7:len(message.Message)], nb) return