mirror-nourybot/pkg/api/xkcd.go

27 lines
511 B
Go
Raw Normal View History

2021-10-18 14:57:44 +02:00
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func Xkcd() string {
response, err := http.Get("https://xkcd.com/info.0.json")
if err != nil {
log.Fatalln(err)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
var responseObject XkcdResponse
json.Unmarshal(responseData, &responseObject)
reply := fmt.Sprint("Current Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img)
return reply
}