mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
36 lines
771 B
Go
36 lines
771 B
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/lyx0/nourybot/pkg/utils"
|
|
)
|
|
|
|
type XkcdResponse struct {
|
|
Num int `json:"num"`
|
|
SafeTitle string `json:"safe_title"`
|
|
Img string `json:"img"`
|
|
}
|
|
|
|
func RandomXkcd() string {
|
|
comicNum := fmt.Sprint(utils.GenerateRandomNumber(2468))
|
|
response, err := http.Get(fmt.Sprint("http://xkcd.com/" + comicNum + "/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("Random Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img)
|
|
|
|
return reply
|
|
}
|