Skip to content

Commit 171e32e

Browse files
author
星冉
committed
完善 plug-in 仓库示例 📝
1 parent 65c4ccd commit 171e32e

File tree

1 file changed

+138
-122
lines changed

1 file changed

+138
-122
lines changed

examples/plugln/ding-ding-bot.go

Lines changed: 138 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -16,125 +16,141 @@
1616
* limitations under the License.
1717
*
1818
*/
19-
package main
20-
21-
import (
22-
"fmt"
23-
"log"
24-
"time"
25-
26-
"github.com/wechaty/go-wechaty/wechaty"
27-
"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
28-
"github.com/wechaty/go-wechaty/wechaty/user"
29-
)
30-
31-
func main() {
32-
var bot = wechaty.NewWechaty()
33-
34-
bot.OnScan(func(ctx *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
35-
fmt.Printf("Scan QR Code to login: %v\nhttps://wechaty.js.org/qrcode/%s\n", status, qrCode)
36-
}).OnLogin(func(ctx *wechaty.Context, user *user.ContactSelf) {
37-
fmt.Printf("User %s logined\n", user.Name())
38-
}).OnMessage(onMessage).OnLogout(func(ctx *wechaty.Context, user *user.ContactSelf, reason string) {
39-
fmt.Printf("User %s logouted: %s\n", user, reason)
40-
})
41-
42-
// The first plug-in
43-
bot.Use(func() *wechaty.Plugin {
44-
plugOne := wechaty.NewPlugin()
45-
46-
// Second message processing logic
47-
plugOne.OnMessage(func(context *wechaty.Context, message *user.Message) {
48-
log.Println(message)
49-
50-
if message.Self() {
51-
log.Println("Message discarded because its outgoing")
52-
}
53-
54-
if message.Age() > 2*60*time.Second {
55-
log.Println("Message discarded because its TOO OLD(than 2 minutes)")
56-
}
57-
58-
if message.Type() != schemas.MessageTypeText || message.Text() != "plugOne" {
59-
log.Println("Message discarded because it does not match 'plugOne'")
60-
return
61-
}
62-
63-
// 3. reply 'plugOne here!'
64-
_, err := message.Say("plugOne here!")
65-
if err != nil {
66-
log.Println(err)
67-
return
68-
}
69-
})
70-
return plugOne
71-
}())
72-
73-
// Second plug-in
74-
bot.Use(func() *wechaty.Plugin {
75-
plugTwo := wechaty.NewPlugin()
76-
77-
// The third message processing logic
78-
plugTwo.OnMessage(func(context *wechaty.Context, message *user.Message) {
79-
log.Println(message)
80-
81-
if message.Self() {
82-
log.Println("Message discarded because its outgoing")
83-
}
84-
85-
if message.Age() > 2*60*time.Second {
86-
log.Println("Message discarded because its TOO OLD(than 2 minutes)")
87-
}
88-
89-
if message.Type() != schemas.MessageTypeText || message.Text() != "plugTwo" {
90-
log.Println("Message discarded because it does not match 'plugTwo'")
91-
return
92-
}
93-
94-
// 4. reply 'plugTwo here!'
95-
_, err := message.Say("plugTwo here!")
96-
if err != nil {
97-
log.Println(err)
98-
return
99-
}
100-
})
101-
return plugTwo
102-
}())
103-
bot.DaemonStart()
104-
}
105-
106-
// The first message processing logic
107-
func onMessage(ctx *wechaty.Context, message *user.Message) {
108-
log.Println(message)
109-
110-
if message.Self() {
111-
log.Println("Message discarded because its outgoing")
112-
}
113-
114-
if message.Age() > 2*60*time.Second {
115-
log.Println("Message discarded because its TOO OLD(than 2 minutes)")
116-
}
117-
118-
if message.Type() != schemas.MessageTypeText || message.Text() != "ding" {
119-
log.Println("Message discarded because it does not match 'ding'")
120-
return
121-
}
122-
123-
// 1. reply 'dong'
124-
_, err := message.Say("dong")
125-
if err != nil {
126-
log.Println(err)
127-
return
128-
}
129-
log.Println("REPLY: dong")
130-
131-
// 2. reply image(qrcode image)
132-
//fileBox, _ := file_box.FromUrl("https://wechaty.github.io/wechaty/images/bot-qr-code.png", "", nil)
133-
//_, err = message.Say(fileBox)
134-
//if err != nil {
135-
// log.Println(err)
136-
// return
137-
//}
138-
//log.Printf("REPLY: %s\n", fileBox)
139-
}
140-
19+
package main
20+
21+
import (
22+
"fmt"
23+
"log"
24+
"time"
25+
26+
"github.com/wechaty/go-wechaty/wechaty"
27+
"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
28+
"github.com/wechaty/go-wechaty/wechaty/user"
29+
)
30+
31+
var err error
32+
33+
func main() {
34+
var bot = wechaty.NewWechaty()
35+
36+
bot.OnScan(func(context *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
37+
fmt.Printf("Scan QR Code to login: %v\nhttps://wechaty.js.org/qrcode/%s\n", status, qrCode)
38+
}).OnLogin(func(context *wechaty.Context, user *user.ContactSelf) {
39+
fmt.Printf("User %s logined\n", user.Name())
40+
}).OnLogout(func(ctx *wechaty.Context, user *user.ContactSelf, reason string) {
41+
fmt.Printf("User %s logouted: %s\n", user, reason)
42+
})
43+
44+
bot.OnMessage(onMessage)
45+
// The First Plug-in
46+
bot.Use(PlugOne())
47+
// The Second Plug-in
48+
bot.Use(PlugTwo("Hello, PlugTwo"))
49+
50+
bot.DaemonStart()
51+
}
52+
53+
// The first onMessage processing logic
54+
func onMessage(context *wechaty.Context, message *user.Message) {
55+
log.Println(message)
56+
57+
if message.Self() {
58+
log.Println("Message discarded because its outgoing")
59+
}
60+
61+
if message.Age() > 2*60*time.Second {
62+
log.Println("Message discarded because its TOO OLD(than 2 minutes)")
63+
}
64+
65+
if message.Type() != schemas.MessageTypeText || message.Text() != "ding" {
66+
log.Println("Message discarded because it does not match 'ding'")
67+
68+
// Set needReply Status (`return`)
69+
context.SetData("needReply", false)
70+
return
71+
}
72+
73+
// 1. reply 'dong'
74+
if _, err = message.Say("dong"); err != nil {
75+
log.Println(err)
76+
return
77+
}
78+
log.Println("REPLY: dong")
79+
80+
// Set needReply Status
81+
context.SetData("needReply", true)
82+
83+
// 2. reply image(qrcode image)
84+
//fileBox, _ := file_box.FromUrl("https://wechaty.github.io/wechaty/images/bot-qr-code.png", "", nil)
85+
//_, err = message.Say(fileBox)
86+
//if err != nil {
87+
// log.Println(err)
88+
// return
89+
//}
90+
//log.Printf("REPLY: %s\n", fileBox)
91+
}
92+
93+
// PlugOne (The First Plug-in)
94+
func PlugOne() *wechaty.Plugin {
95+
newPlug := wechaty.NewPlugin()
96+
97+
// The Second Onmessage Processing Logic
98+
newPlug.OnMessage(func(context *wechaty.Context, message *user.Message) {
99+
100+
// Get Value From Wechaty Context
101+
needReply, ok := context.GetData("needReply").(bool)
102+
103+
// Determine If Parsing Error (`!`)
104+
if !ok {
105+
log.Println("context GetData needReply Error")
106+
return
107+
}
108+
109+
// Determine If You Have Responded (`!`)
110+
if !needReply {
111+
return
112+
}
113+
114+
if _, err = message.Say("PlugOne OnMessage Here!"); err != nil {
115+
log.Println(err)
116+
return
117+
}
118+
log.Println("PlugOne OnMessage Here!")
119+
})
120+
return newPlug
121+
}
122+
123+
/*
124+
PlugTwo (The Second Plug-in)
125+
func PlugTwo(msg string) *wechaty.Plugin {}
126+
*/
127+
func PlugTwo(args string) *wechaty.Plugin {
128+
newPlug := wechaty.NewPlugin()
129+
130+
// The Third Onmessage Processing Logic
131+
newPlug.OnMessage(func(context *wechaty.Context, message *user.Message) {
132+
133+
// Get Value From Wechaty Context
134+
needReply, ok := context.GetData("needReply").(bool)
135+
136+
// Determine If Parsing Error (`!`)
137+
if !ok {
138+
log.Println("context GetData needReply Error")
139+
return
140+
}
141+
142+
log.Println(args)
143+
144+
// Determine If You Have Responded (`!`)
145+
if !needReply {
146+
return
147+
}
148+
149+
if _, err = message.Say("PlugTwo OnMessage Here!"); err != nil {
150+
log.Println(err)
151+
return
152+
}
153+
log.Println("PlugTwo OnMessage Here!")
154+
})
155+
return newPlug
156+
}

0 commit comments

Comments
 (0)