-
Notifications
You must be signed in to change notification settings - Fork 1
/
hitokoto.go
64 lines (60 loc) · 1.71 KB
/
hitokoto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package command
import (
"encoding/json"
"fmt"
"github.com/cockroachdb/errors"
"github.com/hitokoto-osc/telegram_bot/request"
"github.com/samber/lo"
"gopkg.in/telebot.v3"
"strings"
)
var supportedTypeList = []string{
"a", // 动画
"b", // 漫画
"c", // 游戏
"d", // 文学
"e", // 原创
"f", // 来自网络
"g", // 其他
"h", // 影视
"i", // 诗词(主要是文言诗集)
"j", // 网易云
"k", // 哲学
"l", // 抖机灵
}
// Hitokoto 获取一言
func Hitokoto(b *telebot.Bot) {
b.Handle("/hitokoto", func(ctx telebot.Context) error {
payload := strings.TrimSpace(ctx.Message().Payload) // 指令:`/hitokoto <payload>` 这里提取 payload 用于提取参数
url := "https://v1.hitokoto.cn/"
if lo.Contains(supportedTypeList, payload) {
url += fmt.Sprintf("?c=%s", payload)
}
// 请求接口
client := request.NewDefault()
resp, err := client.Get(url)
if err != nil {
return errors.Wrap(err, "无法请求一言接口")
}
defer resp.Body.Close()
// 解析 JSON
data := &HitokotoSentenceAPIV1Response{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return errors.Wrap(err, "无法解析一言接口返回的 JSON 数据")
}
return ctx.Reply(fmt.Sprintf(`%s —— %s「%s」`, data.Hitokoto, data.FromWho, data.From))
})
}
// HitokotoSentenceAPIV1Response 定义了一言接口的结构
type HitokotoSentenceAPIV1Response struct {
ID uint32 `json:"id"`
Hitokoto string `json:"hitokoto"`
From string `json:"from"`
FromWho string `json:"from_who"`
Creator string `json:"creator"`
CreatorUID int32 `json:"creator_uid"`
Reviewer int32 `json:"reviewer"`
UUID string `json:"uuid"`
CreatedAt string `json:"created_at"`
}