Skip to content

Commit c60c4b0

Browse files
authored
qr scan example (#132)
1 parent be0a4a9 commit c60c4b0

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

examples/ding-dong-bot.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package main
33
import (
44
"fmt"
55
"log"
6+
"net/url"
7+
"os"
68
"time"
79

10+
"github.com/mdp/qrterminal/v3"
811
"github.com/wechaty/go-wechaty/wechaty"
912
wp "github.com/wechaty/go-wechaty/wechaty-puppet"
1013
"github.com/wechaty/go-wechaty/wechaty-puppet/filebox"
@@ -17,9 +20,7 @@ func main() {
1720
Token: "",
1821
}))
1922

20-
bot.OnScan(func(ctx *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
21-
fmt.Printf("Scan QR Code to login: %v\nhttps://wechaty.github.io/qrcode/%s\n", status, qrCode)
22-
}).OnLogin(func(ctx *wechaty.Context, user *user.ContactSelf) {
23+
bot.OnScan(onScan).OnLogin(func(ctx *wechaty.Context, user *user.ContactSelf) {
2324
fmt.Printf("User %s logined\n", user.Name())
2425
}).OnMessage(onMessage).OnLogout(func(ctx *wechaty.Context, user *user.ContactSelf, reason string) {
2526
fmt.Printf("User %s logouted: %s\n", user, reason)
@@ -78,3 +79,14 @@ func onMessage(ctx *wechaty.Context, message *user.Message) {
7879
}
7980
log.Printf("REPLY with urlLink: %s\n", urlLink)
8081
}
82+
83+
func onScan(ctx *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
84+
if status == schemas.ScanStatusWaiting || status == schemas.ScanStatusTimeout {
85+
qrterminal.GenerateHalfBlock(qrCode, qrterminal.L, os.Stdout)
86+
87+
qrcodeImageUrl := fmt.Sprintf("https://wechaty.js.org/qrcode/%s",url.QueryEscape(qrCode))
88+
fmt.Printf("onScan: %s - %s\n", status, qrcodeImageUrl)
89+
return
90+
}
91+
fmt.Printf("onScan: %s\n", status)
92+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.18
55
require (
66
github.com/hashicorp/golang-lru v0.5.4
77
github.com/lucsky/cuid v1.0.2
8+
github.com/mdp/qrterminal/v3 v3.0.0
89
github.com/otiai10/opengraph v1.1.1
910
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
1011
github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2
@@ -23,4 +24,5 @@ require (
2324
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
2425
golang.org/x/text v0.3.7 // indirect
2526
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
27+
rsc.io/qr v0.2.0 // indirect
2628
)

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=
@@ -105,6 +110,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
105110
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
106111
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
107112
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
113+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
108114
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
109115
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
110116
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -162,3 +168,5 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
162168
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
163169
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
164170
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
171+
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
172+
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=

0 commit comments

Comments
 (0)