add currency

This commit is contained in:
lyx0 2021-10-23 19:48:50 +02:00
parent 9bd0719877
commit b3543fcced
3 changed files with 61 additions and 0 deletions

42
pkg/api/currency.go Normal file
View file

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

16
pkg/commands/currency.go Normal file
View file

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

View file

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