mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
messy but works
This commit is contained in:
parent
b3543fcced
commit
5c0545e732
2 changed files with 35 additions and 10 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -14,15 +15,35 @@ import (
|
||||||
type currencyResponse struct {
|
type currencyResponse struct {
|
||||||
Amount int `json:"amount"`
|
Amount int `json:"amount"`
|
||||||
Base string `json:"base"`
|
Base string `json:"base"`
|
||||||
Rates rates
|
Rates map[string]float32 `json:"rates"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type rates map[string]interface{}
|
// type rates map[string]interface{}
|
||||||
|
// type rates struct {
|
||||||
|
// AUD float32 `json:"AUD"`
|
||||||
|
// BGN float32 `json:"BGN"`
|
||||||
|
// BRL float32 `json:"BRL"`
|
||||||
|
// CAD float32 `json:"CAD"`
|
||||||
|
// CHF float32 `json:"CHF"`
|
||||||
|
// CNY float32 `json:"CNY"`
|
||||||
|
// CZK float32 `json:"CZK"`
|
||||||
|
// DKK float32 `json:"DKK"`
|
||||||
|
// GBP float32 `json:"GBP"`
|
||||||
|
// HKD float32 `json:"HKD"`
|
||||||
|
// HRK float32 `json:"HRK"`
|
||||||
|
// HUF float32 `json:"HUF"`
|
||||||
|
// IDR float32 `json:"IDR"`
|
||||||
|
// ILS float32 `json:"ILS"`
|
||||||
|
// // TODO: ADD MORE
|
||||||
|
// USD float32 `json:"USD"`
|
||||||
|
// }
|
||||||
|
|
||||||
|
// https://api.frankfurter.app/latest?amount=10&from=gbp&to=usd
|
||||||
|
|
||||||
|
// Really messy code but works right now
|
||||||
func Currency(currAmount string, currFrom string, currTo string) (string, error) {
|
func Currency(currAmount string, currFrom string, currTo string) (string, error) {
|
||||||
//
|
|
||||||
baseUrl := "https://api.frankfurter.app"
|
baseUrl := "https://api.frankfurter.app"
|
||||||
response, err := http.Get(fmt.Sprintf("%s/latest?amount=%s&from=%s&to=%s", baseUrl, currAmount, currFrom, currTo))
|
response, err := http.Get(fmt.Sprintf("%s/latest?amount=%s&from=%s&to=%s", baseUrl, currAmount, strings.ToUpper(currFrom), strings.ToUpper(currTo)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -35,8 +56,11 @@ func Currency(currAmount string, currFrom string, currTo string) (string, error)
|
||||||
var responseObject currencyResponse
|
var responseObject currencyResponse
|
||||||
json.Unmarshal(responseData, &responseObject)
|
json.Unmarshal(responseData, &responseObject)
|
||||||
|
|
||||||
log.Info(responseObject.Amount)
|
// log.Info(responseObject.Amount)
|
||||||
log.Info(responseObject.Base)
|
// log.Info(responseObject.Base)
|
||||||
log.Info(responseObject.Rates)
|
// log.Info(responseObject.Rates[strings.ToUpper(currTo)])
|
||||||
return "", nil
|
if responseObject.Rates[strings.ToUpper(currTo)] != 0 {
|
||||||
|
return fmt.Sprintf("%s%s are %v%s", currAmount, currFrom, responseObject.Rates[strings.ToUpper(currTo)], currTo), nil
|
||||||
|
}
|
||||||
|
return "Something went wrong FeelsBadMan", nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,4 +13,5 @@ func Currency(target string, currAmount string, currFrom string, currTo string,
|
||||||
logrus.Info(err)
|
logrus.Info(err)
|
||||||
}
|
}
|
||||||
nb.Send(target, reply)
|
nb.Send(target, reply)
|
||||||
|
// logrus.Info(target, reply)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue