Fetch the repository succeeded.
package animate
/*
Animate command for the Micro Bot
usage: animate [text]
*/
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/micro/go-bot/command"
)
var (
// public giphy key
key = "dc6zaTOxFJmzC"
)
func init() {
command.Commands["^animate "] = Animate()
}
func Animate() command.Command {
usage := "animate [text]"
desc := "Returns an animation"
type Meta struct {
Status int `json:"status"`
Msg string `json:"msg"`
}
type Image struct {
Url string `json:"url"`
}
type Images struct {
FixedHeight Image `json:"fixed_height"`
}
type Result struct {
Images Images `json:"images"`
}
type Results struct {
Data []Result `json:"data"`
Meta Meta `json:"meta"`
}
return command.NewCommand("animate", usage, desc, func(args ...string) ([]byte, error) {
if len(args) < 2 {
return []byte("animate what?"), nil
}
u := url.Values{}
u.Set("q", strings.Join(args[1:], " "))
u.Set("limit", "1")
u.Set("api_key", key)
rsp, err := http.Get("http://api.giphy.com/v1/gifs/search?" + u.Encode())
if err != nil {
return nil, err
}
defer rsp.Body.Close()
var res Results
if err := json.NewDecoder(rsp.Body).Decode(&res); err != nil {
return nil, err
}
if res.Meta.Status != 200 {
return nil, fmt.Errorf("returned status: %d %s", res.Meta.Status, res.Meta.Msg)
}
if len(res.Data) == 0 {
return nil, nil
}
return []byte(res.Data[0].Images.FixedHeight.Url), nil
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。