RG
Size: a a a
AD
S
package plugins
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)
const catAPIEndpoint = "https://api.thecatapi.com/v1/images/search?"
//CatJSONModel модель которую возвращает CatAPI
type CatJSONModel []struct {
Breeds []interface{} `json:"breeds"`
ID string `json:"id"`
URL string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
}
//GetCatPicture (m *telebot.Message, b *telebot.Bot, token string)
//принимает сущности telebot + token для запроса в API и отправляет в чат картикну котика
func GetCatPicture(m *tb.Message, b *tb.Bot, token string) {
resp, err := http.Get(fmt.Sprintf(catAPIEndpoint+"api_key==%v", token))
if err != nil {
log.Error(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
}
var catAPI CatJSONModel
err = json.Unmarshal(body, &catAPI)
if err != nil {
log.Errorf("[Plugin: CatAPI] Error while unmarshaling responce from OpenCatAPI: %v", err)
b.Send(m.Chat, "Возможно сейчас котики спят и их не стоит тревожить. А может это у меня что-то сломалось. Попробуйте еще раз...")
}
b.SendAlbum(m.Chat, tb.Album{&tb.Photo{File: tb.FromURL(catAPI[0].URL)}})
}AD