Skip to content

Commit 93e7a75

Browse files
authored
qr scan example (#40)
1 parent ca747ed commit 93e7a75

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

examples/ding-dong-bot.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ package main
2020

2121
import (
2222
"fmt"
23+
"github.com/mdp/qrterminal/v3"
2324
"log"
25+
"net/url"
26+
"os"
2427
"time"
2528

2629
"github.com/wechaty/go-wechaty/wechaty"
@@ -31,9 +34,7 @@ import (
3134
func main() {
3235
var bot = wechaty.NewWechaty()
3336

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+
bot.OnScan(onScan).OnLogin(func(ctx *wechaty.Context, user *user.ContactSelf) {
3738
fmt.Printf("User %s logined\n", user.Name())
3839
}).OnMessage(onMessage).OnLogout(func(ctx *wechaty.Context, user *user.ContactSelf, reason string) {
3940
fmt.Printf("User %s logouted: %s\n", user, reason)
@@ -75,3 +76,14 @@ func onMessage(ctx *wechaty.Context, message *user.Message) {
7576
//}
7677
//log.Printf("REPLY: %s\n", fileBox)
7778
}
79+
80+
func onScan(ctx *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
81+
if status == schemas.ScanStatusWaiting || status == schemas.ScanStatusTimeout {
82+
qrterminal.GenerateHalfBlock(qrCode, qrterminal.L, os.Stdout)
83+
84+
qrcodeImageUrl := fmt.Sprintf("https://wechaty.js.org/qrcode/%s", url.QueryEscape(qrCode))
85+
fmt.Printf("onScan: %s - %s\n", status, qrcodeImageUrl)
86+
return
87+
}
88+
fmt.Printf("onScan: %s\n", status)
89+
}

examples/plugln/ding-dong-bot.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ package main
2020

2121
import (
2222
"fmt"
23+
"github.com/mdp/qrterminal/v3"
2324
"log"
25+
"net/url"
26+
"os"
2427
"time"
2528

2629
"github.com/wechaty/go-wechaty/wechaty"
@@ -33,9 +36,7 @@ var err error
3336
func main() {
3437
var bot = wechaty.NewWechaty()
3538

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+
bot.OnScan(onScan).OnLogin(func(context *wechaty.Context, user *user.ContactSelf) {
3940
fmt.Printf("User %s logined\n", user.Name())
4041
}).OnLogout(func(ctx *wechaty.Context, user *user.ContactSelf, reason string) {
4142
fmt.Printf("User %s logouted: %s\n", user, reason)
@@ -151,3 +152,14 @@ func PlugTwo(args string) *wechaty.Plugin {
151152
})
152153
return newPlug
153154
}
155+
156+
func onScan(ctx *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
157+
if status == schemas.ScanStatusWaiting || status == schemas.ScanStatusTimeout {
158+
qrterminal.GenerateHalfBlock(qrCode, qrterminal.L, os.Stdout)
159+
160+
qrcodeImageUrl := fmt.Sprintf("https://wechaty.js.org/qrcode/%s", url.QueryEscape(qrCode))
161+
fmt.Printf("onScan: %s - %s\n", status, qrcodeImageUrl)
162+
return
163+
}
164+
fmt.Printf("onScan: %s\n", status)
165+
}

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/wechaty/go-wechaty-getting-started
22

33
go 1.18
44

5-
require github.com/wechaty/go-wechaty v0.4.4
5+
require (
6+
github.com/mdp/qrterminal/v3 v3.0.0
7+
github.com/wechaty/go-wechaty v0.4.4
8+
)
69

710
require (
811
github.com/golang/protobuf v1.5.2 // indirect
@@ -22,4 +25,5 @@ require (
2225
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac // indirect
2326
google.golang.org/grpc v1.45.0 // indirect
2427
google.golang.org/protobuf v1.28.0 // indirect
28+
rsc.io/qr v0.2.0 // indirect
2529
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ github.com/lucsky/cuid v1.0.2 h1:z4XlExeoderxoPj2/dxKOyPxe9RCOu7yNq9/XWxIUMQ=
5555
github.com/lucsky/cuid v1.0.2/go.mod h1:QaaJqckboimOmhRSJXSx/+IT+VTfxfPGSo/6mfgUfmE=
5656
github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4 h1:u9jwvcKbQpghIXgNl/EOL8hzhAFXh4ePrEP493W3tNA=
5757
github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4/go.mod h1:kcRFpEzolcEklV6rD7W95mG49/sbdX/PlFmd7ni3RvA=
58+
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
59+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
60+
github.com/mdp/qrterminal v1.0.1/go.mod h1:Z33WhxQe9B6CdW37HaVqcRKzP+kByF3q/qLxOGe12xQ=
61+
github.com/mdp/qrterminal/v3 v3.0.0 h1:ywQqLRBXWTktytQNDKFjhAvoGkLVN3J2tAFZ0kMd9xQ=
62+
github.com/mdp/qrterminal/v3 v3.0.0/go.mod h1:NJpfAs7OAm77Dy8EkWrtE4aq+cE6McoLXlBqXQEwvE0=
5863
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
5964
github.com/otiai10/marmoset v0.4.0 h1:Hg59lQI7qQowBEdsAJ/+VDTEospTBzXX/A1Gsw4mlvA=
6065
github.com/otiai10/marmoset v0.4.0/go.mod h1:t2q6dXWZ9YcFdRREDApX4bCmfQnL3isJ2dgl8ychlXg=
@@ -106,6 +111,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
106111
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
107112
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
108113
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
114+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
109115
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
110116
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
111117
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -164,3 +170,5 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
164170
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
165171
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
166172
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
173+
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
174+
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=

0 commit comments

Comments
 (0)