Skip to content

Commit 6d3b5b4

Browse files
author
星冉
committed
添加插件示例
1 parent b8f1ce7 commit 6d3b5b4

File tree

6 files changed

+690
-14
lines changed

6 files changed

+690
-14
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package DingTools
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
"github.com/blinkbean/dingtalk"
9+
"github.com/spf13/viper"
10+
. "github.com/wechaty/go-wechaty-getting-started/examples/plug-demo/StructTools" // 导入结构体
11+
"github.com/wechaty/go-wechaty/wechaty"
12+
"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
13+
"github.com/wechaty/go-wechaty/wechaty/user"
14+
)
15+
16+
/*
17+
初始化插件
18+
*/
19+
func DingTools() *wechaty.Plugin {
20+
// 初始化插件
21+
plug := wechaty.NewPlugin()
22+
plug.
23+
OnScan(onScan).
24+
OnLogout(onLogout).
25+
OnMessage(onMessage)
26+
27+
return plug
28+
}
29+
30+
/*
31+
处理登录事件,无需传参
32+
*/
33+
func onScan(context *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
34+
if status.String() == "ScanStatusWaiting" {
35+
for i := 0; i < 6; i++ {
36+
DingSend(fmt.Sprintf("账号未登录请扫码!\n\n---\n\n[qrCode](https://wechaty.js.org/qrcode/%v)", qrCode))
37+
time.Sleep(120 * time.Second)
38+
if i == 5 {
39+
os.Exit(1)
40+
}
41+
}
42+
} else if status.String() == "ScanStatusScanned" {
43+
fmt.Printf("%v[Scan] Status: %v %v\n", viper.GetString("info"), status.String(), data)
44+
} else {
45+
fmt.Printf("%v[Scan] Status: %v %v\n", viper.GetString("info"), status.String(), data)
46+
}
47+
}
48+
49+
/*
50+
处理账号登出事件,无需传参
51+
*/
52+
func onLogout(context *wechaty.Context, user *user.ContactSelf, reason string) {
53+
DingSend(fmt.Sprintf("%s已登出!\n\n---\n\n**reason**\n\n%v", user.Name(), reason))
54+
}
55+
56+
/*
57+
处理消息事件,由 DingTools 传入消息对象
58+
*/
59+
func onMessage(context *wechaty.Context, message *user.Message) {
60+
// 导入我们之前设置的 结构体数据
61+
m, ok := (context.GetData("MessageInfo")).(MessageInfo)
62+
if !ok {
63+
fmt.Printf("Conversion Failed")
64+
return
65+
}
66+
67+
// 判断是否是机器人自己发送的消息
68+
if message.Self() {
69+
fmt.Printf("Self")
70+
}
71+
72+
// 判断是否超过 2 分钟
73+
if message.Age() > 2*60*time.Second {
74+
fmt.Printf("TimeOut")
75+
}
76+
77+
// 判断是否回复消息, 尴尬下面还要再写一次, 这里不写的话会有一堆的消息
78+
if !m.Reply {
79+
return
80+
}
81+
82+
// 设置 DingDing 回复消息的内容
83+
msg := fmt.Sprintf("%v@我了\n\n---\n\n### 用户属性\n\n用户名: [%v]\n\n用户ID: [%v]", message.Talker().Name(), message.Talker().Name(), message.Talker().ID())
84+
85+
// 判读是否是群聊
86+
if m.Status {
87+
msg += fmt.Sprintf("\n\n---\n\n### 群聊属性\n\n群聊名称: [%v]\n\n群聊ID: [%v]", message.Room().Topic(), message.Room().ID())
88+
}
89+
90+
// 继续追加内容
91+
msg += fmt.Sprintf("\n\n---\n\n**内容**: [%v]", message.Text())
92+
93+
// 判断是否有别的原因跳过了操作
94+
if m.Pass {
95+
msg += fmt.Sprintf("\n\n**Pass**: [%v]", m.PassResult)
96+
}
97+
98+
// 判断是否回复内容
99+
if m.Reply {
100+
msg += fmt.Sprintf("\n\n**回复**: [%v]", m.ReplyResult)
101+
}
102+
103+
// 到这里的时候基本设置好了一些默认的值了
104+
DingSend(msg)
105+
}
106+
107+
/*
108+
发送消息到 DingDing
109+
外部调用:
110+
DingSend(msg string)
111+
*/
112+
func DingSend(msg string) {
113+
cli := dingtalk.InitDingTalkWithSecret(viper.GetString("Ding.TOKEN"), viper.GetString("Ding.SECRET"))
114+
if err := cli.SendMarkDownMessage(msg, msg); err != nil {
115+
fmt.Printf("DingMessage Error: [%v]", err)
116+
return
117+
}
118+
fmt.Printf("DingTalk Success!")
119+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package StructTools
2+
3+
/*
4+
定义一个共享的结构体
5+
*/
6+
type MessageInfo struct {
7+
Status bool `json:"status"` // 群聊属性
8+
AtMe bool `json:"atme"` // 是否@我
9+
ReplyResult string `json:"reply_result"` // 自动回复
10+
Reply bool `json:"reply"` // 自动回复状态
11+
PassResult string `json:"pass_result"` // pass 原因
12+
Pass bool `json:"pass"` // pass 状态
13+
}

examples/plug-demo/config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Ding:
2+
SECRET: xxxxxxxxxxxxxxxxxxxxx
3+
TOKEN: xxxxxxxxxxxxxxxxxxxxx
4+
## KeyWord: 用户名, ID ( 请在钉钉内部设置!)
5+
WECHATY:
6+
ENDPOINT: xxxxxxxxxxxxxxxxxxxxx
7+
TOKEN: xxxxxxxxxxxxxxxxxxxxx

examples/plug-demo/ding-ding-bot.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
"runtime"
8+
"strings"
9+
"time"
10+
11+
"github.com/mdp/qrterminal/v3"
12+
"github.com/spf13/viper"
13+
. "github.com/wechaty/go-wechaty-getting-started/examples/plug-demo/DingTools"
14+
. "github.com/wechaty/go-wechaty-getting-started/examples/plug-demo/StructTools" // 导入结构体
15+
"github.com/wechaty/go-wechaty/wechaty"
16+
puppet "github.com/wechaty/go-wechaty/wechaty-puppet"
17+
"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
18+
"github.com/wechaty/go-wechaty/wechaty/user"
19+
)
20+
21+
var (
22+
err error
23+
)
24+
25+
/*
26+
初始化插件
27+
*/
28+
func init() {
29+
// 初始化配置文件
30+
var abPath string
31+
_, filename, _, ok := runtime.Caller(0)
32+
if ok {
33+
abPath = path.Dir(filename)
34+
}
35+
viper.SetConfigName("config")
36+
viper.AddConfigPath(".")
37+
viper.AddConfigPath(abPath)
38+
viper.SetConfigType("yaml")
39+
err = viper.ReadInConfig()
40+
if err != nil {
41+
fmt.Printf("Read config failed: %v", err)
42+
}
43+
if viper.GetString("WECHATY.TOKEN") == "" {
44+
fmt.Printf("Please Set WECHATY.TOKEN in config.yaml")
45+
os.Exit(1)
46+
}
47+
}
48+
49+
/*
50+
主程序
51+
*/
52+
func main() {
53+
var bot = wechaty.NewWechaty(wechaty.WithPuppetOption(puppet.Option{
54+
Token: viper.GetString("WECHATY.TOKEN"),
55+
Endpoint: viper.GetString("WECHATY.ENDPOINT"),
56+
}))
57+
bot.
58+
OnScan(func(context *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
59+
fmt.Printf("\n\n")
60+
if status.String() == "ScanStatusWaiting" {
61+
qrterminal.GenerateWithConfig(qrCode, qrterminal.Config{
62+
Level: qrterminal.L,
63+
Writer: os.Stdout,
64+
BlackChar: " \u2005",
65+
WhiteChar: "💵",
66+
QuietZone: 1,
67+
})
68+
fmt.Printf("\n\n")
69+
fmt.Printf("%v[Scan] https://wechaty.js.org/qrcode/%v %v", viper.GetString("info"), qrCode, data)
70+
} else if status.String() == "ScanStatusScanned" {
71+
fmt.Printf("%v[Scan] Status: %v %v\n", viper.GetString("info"), status.String(), data)
72+
} else {
73+
fmt.Printf("%v[Scan] Status: %v %v\n", viper.GetString("info"), status.String(), data)
74+
}
75+
}).
76+
OnLogin(func(context *wechaty.Context, user *user.ContactSelf) {
77+
fmt.Printf("\n微信机器人: [%v] 已经登录成功了。\n", user.Name())
78+
// 可 别小看 context
79+
context.SetData("botName", user.Name())
80+
}).
81+
OnMessage(func(context *wechaty.Context, message *user.Message) {
82+
var m MessageInfo
83+
84+
// 群聊消息 || 好友消息
85+
// 这样最简单,最快捷吧
86+
if message.Room() != nil {
87+
m.Status = true
88+
// 如果 @ 我了
89+
if message.MentionSelf() {
90+
m.AtMe = true
91+
// 如果是@所有人
92+
if strings.Contains(message.Text(), "所有人") {
93+
m.Pass = true
94+
m.PassResult = "全员消息"
95+
}
96+
} else {
97+
m.AtMe = false
98+
}
99+
} else {
100+
m.Status = false
101+
m.AtMe = true
102+
}
103+
104+
// 测试是否运作
105+
if message.Type() == schemas.MessageTypeText {
106+
if message.Age() > 2*60*time.Second {
107+
if message.Text() == "ding" {
108+
if _, err := message.Say("dong"); err != nil {
109+
fmt.Printf("Send Message Error, Error: [%v]", err)
110+
// 这里不能 return , 因为 下面还有一个 数据没导出,我自己是单独写了个模块处理这些数据
111+
}
112+
// name回复过了,就设置下回复状态吧,不然别人问你一句,你会别人十句
113+
m.Reply = true
114+
m.ReplyResult = "ding"
115+
}
116+
}
117+
}
118+
119+
// 保存 结构体,当前消息所在的任何函数都可调用
120+
context.SetData("MessageInfo", m)
121+
// 执行完毕 ,按照 顺序,下一个执行的就是 DingTools
122+
}).
123+
124+
// wechaty 执行是按照先后顺序的
125+
// OnScan 和 Use 插件 内部的 Onscan 也是按顺序执行
126+
127+
Use(DingTools()).
128+
129+
// 启动守护程序
130+
DaemonStart()
131+
}

go.mod

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@ module github.com/wechaty/go-wechaty-getting-started
22

33
go 1.18
44

5-
require github.com/wechaty/go-wechaty v0.4.1
5+
require (
6+
github.com/blinkbean/dingtalk v0.0.0-20210905093040-7d935c0f7e19
7+
github.com/mdp/qrterminal/v3 v3.0.0
8+
github.com/spf13/viper v1.11.0
9+
github.com/wechaty/go-wechaty v0.4.1
10+
)
611

712
require (
13+
github.com/fsnotify/fsnotify v1.5.1 // indirect
814
github.com/golang/protobuf v1.5.2 // indirect
915
github.com/google/uuid v1.1.2 // indirect
1016
github.com/grpc-ecosystem/grpc-gateway/v2 v2.10.0 // indirect
1117
github.com/hashicorp/golang-lru v0.5.4 // indirect
18+
github.com/hashicorp/hcl v1.0.0 // indirect
1219
github.com/lucsky/cuid v1.0.2 // indirect
20+
github.com/magiconair/properties v1.8.6 // indirect
1321
github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4 // indirect
22+
github.com/mitchellh/mapstructure v1.4.3 // indirect
1423
github.com/otiai10/opengraph v1.1.1 // indirect
24+
github.com/pelletier/go-toml v1.9.4 // indirect
25+
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
1526
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086 // indirect
27+
github.com/spf13/afero v1.8.2 // indirect
28+
github.com/spf13/cast v1.4.1 // indirect
29+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
30+
github.com/spf13/pflag v1.0.5 // indirect
31+
github.com/subosito/gotenv v1.2.0 // indirect
1632
github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2 // indirect
1733
github.com/wechaty/go-grpc v1.5.2 // indirect
18-
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
19-
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
34+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
35+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
2036
golang.org/x/text v0.3.7 // indirect
21-
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
37+
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac // indirect
2238
google.golang.org/grpc v1.45.0 // indirect
23-
google.golang.org/protobuf v1.27.1 // indirect
39+
google.golang.org/protobuf v1.28.0 // indirect
40+
gopkg.in/ini.v1 v1.66.4 // indirect
41+
gopkg.in/yaml.v2 v2.4.0 // indirect
42+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
43+
rsc.io/qr v0.2.0 // indirect
2444
)

0 commit comments

Comments
 (0)