Skip to content

Commit 7c15ad2

Browse files
authored
feat: 添加有道翻译插件 (yqchilde#26)
1 parent 99e2da5 commit 7c15ad2

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

engine/plugins/plugins.go

+16-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/youdaofanyi/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## 介绍
2+
3+
**🔔 注意:以下`[]`符号内代表此处是需要填写内容,并且不需要`[]`符号**
4+
5+
* 插件名:有道翻译
6+
* 权限:所有好友和群聊
7+
* 指令:
8+
* [x] `有道翻译 [你要查的内容]`
9+
* [x] `翻译 [你要查的内容]`
10+

plugins/youdaofanyi/main.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package youdaofanyi
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
7+
"github.com/imroc/req/v3"
8+
"github.com/yqchilde/wxbot/engine/control"
9+
"github.com/yqchilde/wxbot/engine/robot"
10+
)
11+
12+
func init() {
13+
engine := control.Register("youdaofanyi", &control.Options{
14+
Alias: "有道中英文互译",
15+
Help: "用法:有道翻译|翻译 XX\n" +
16+
"示例:翻译 detail",
17+
})
18+
19+
engine.OnRegex(`(^有道翻译|^翻译) ?(.*?)$`).SetBlock(true).Handle(func(ctx *robot.Ctx) {
20+
word := ctx.State["regex_matched"].([]string)[2]
21+
if data, err := getFanYi(word); err == nil {
22+
if data == nil {
23+
ctx.ReplyText("我还不会,稍后尝试")
24+
} else {
25+
ctx.ReplyText(fmt.Sprintf("🔎 译文:\n %s", data.Result))
26+
}
27+
} else {
28+
ctx.ReplyText("查询失败,这一定不是bug🤔")
29+
}
30+
})
31+
}
32+
33+
type apiResponse struct {
34+
Code int `json:"code"`
35+
Msg string `json:"msg"`
36+
Name string `json:"name"`
37+
Result string `json:"result"`
38+
}
39+
40+
func getFanYi(keyword string) (*apiResponse, error) {
41+
var data apiResponse
42+
api := "https://api.qqsuu.cn/api/dm-ydfy?name=" + url.QueryEscape(keyword)
43+
if err := req.C().Get(api).Do().Into(&data); err != nil {
44+
return nil, err
45+
}
46+
if len(data.Result) == 0 {
47+
return nil, nil
48+
}
49+
return &data, nil
50+
}

0 commit comments

Comments
 (0)