Skip to content

Commit 869e9d9

Browse files
authored
Merge pull request #34 from Ran-Xing/master
添加 插件示例
2 parents b8f1ce7 + b52f540 commit 869e9d9

File tree

4 files changed

+170
-13
lines changed

4 files changed

+170
-13
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ bot:
1212
.PHONY: test
1313
test:
1414
go build -o examples/ding-dong-bot -v ./examples/ding-dong-bot.go
15+
go build -o examples/plugln/ding-ding-bot -v ./examples/plugln/ding-ding-bot.go
1516

1617
.PHONY: clean
1718
clean:
1819
rm -f examples/ding-dong-bot
20+
rm -f examples/plugln/ding-ding-bot

examples/plugln/ding-ding-bot.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* Wechaty - https://github.com/wechaty/wechaty
3+
*
4+
* @copyright 2020-now Wechaty
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*/
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+
// PlugTwo (The Second Plug-in)
124+
func PlugTwo(args string) *wechaty.Plugin {
125+
newPlug := wechaty.NewPlugin()
126+
127+
// The Third Onmessage Processing Logic
128+
newPlug.OnMessage(func(context *wechaty.Context, message *user.Message) {
129+
130+
// Get Value From Wechaty Context
131+
needReply, ok := context.GetData("needReply").(bool)
132+
133+
// Determine If Parsing Error (`!`)
134+
if !ok {
135+
log.Println("context GetData needReply Error")
136+
return
137+
}
138+
139+
log.Println(args)
140+
141+
// Determine If You Have Responded (`!`)
142+
if !needReply {
143+
return
144+
}
145+
146+
if _, err = message.Say("PlugTwo OnMessage Here!"); err != nil {
147+
log.Println(err)
148+
return
149+
}
150+
log.Println("PlugTwo OnMessage Here!")
151+
})
152+
return newPlug
153+
}

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ require (
1515
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086 // indirect
1616
github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2 // indirect
1717
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
18+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
19+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
2020
golang.org/x/text v0.3.7 // indirect
21-
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
21+
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
22+
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac // indirect
2223
google.golang.org/grpc v1.45.0 // indirect
23-
google.golang.org/protobuf v1.27.1 // indirect
24+
google.golang.org/protobuf v1.28.0 // indirect
2425
)

go.sum

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
9696
golang.org/x/net v0.0.0-20190926025831-c00fd9afed17/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
9797
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
9898
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
99-
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
100-
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
99+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 h1:bRb386wvrE+oBNdF1d/Xh9mQrfQ4ecYhW5qJ5GvTGT4=
100+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
101101
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
102102
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
103103
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -111,8 +111,8 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
111111
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
112112
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
113113
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
114-
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
115-
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
114+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
115+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
116116
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
117117
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
118118
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@@ -125,16 +125,17 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
125125
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
126126
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
127127
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
128-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
129128
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
129+
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U=
130+
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
130131
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
131132
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
132133
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
133134
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
134135
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
135136
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
136-
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e h1:fNKDNuUyC4WH+inqDMpfXDdfvwfYILbsX+oskGZ8hxg=
137-
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
137+
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac h1:qSNTkEN+L2mvWcLgJOR+8bdHX9rN/IdU3A1Ghpfb1Rg=
138+
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
138139
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
139140
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
140141
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
@@ -155,8 +156,8 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
155156
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
156157
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
157158
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
158-
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
159-
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
159+
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
160+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
160161
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
161162
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
162163
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)