mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add currency
This commit is contained in:
parent
9bd0719877
commit
b3543fcced
3 changed files with 61 additions and 0 deletions
42
pkg/api/currency.go
Normal file
42
pkg/api/currency.go
Normal 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
16
pkg/commands/currency.go
Normal 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)
|
||||||
|
}
|
|
@ -70,6 +70,9 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
|
||||||
commands.Color(message, nb)
|
commands.Color(message, nb)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
// ()currency 10 gbp to usd
|
||||||
|
case "currency":
|
||||||
|
commands.Currency(target, cmdParams[1], cmdParams[2], cmdParams[4], nb)
|
||||||
case "echo":
|
case "echo":
|
||||||
commands.Echo(target, message.Message[7:len(message.Message)], nb)
|
commands.Echo(target, message.Message[7:len(message.Message)], nb)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue