The current repo belongs to Paused status, and some functions are restricted. For details, please refer to the description of repo status
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
Paused

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
animate.go 1.45 KB
Copy Edit Raw Blame History
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
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.3.1

Search