From d1623ba2d1577c24ae2fa81d900dd0e00568fb2a Mon Sep 17 00:00:00 2001 From: IanCao Date: Sun, 15 May 2022 22:16:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=20MOD:=20=E5=A2=9E=E5=8A=A0grpc=E7=9A=84au?= =?UTF-8?q?thority=E4=BF=A1=E6=81=AF=E4=BB=A5=E5=85=BC=E5=AE=B9=E6=96=B0?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=20=20ADD:=20=E6=9C=AC=E5=9C=B0puppet-server?= =?UTF-8?q?=E7=9A=84=E8=B0=83=E7=94=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/github/wechaty/example/Main.java | 70 +++++++++++++++---- .../io/github/wechaty/grpc/GrpcPuppet.kt | 9 +-- wechaty-puppet/src/main/kotlin/Puppet.kt | 1 - 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/examples/src/main/java/io/github/wechaty/example/Main.java b/examples/src/main/java/io/github/wechaty/example/Main.java index 4a0f34e..693bfbc 100644 --- a/examples/src/main/java/io/github/wechaty/example/Main.java +++ b/examples/src/main/java/io/github/wechaty/example/Main.java @@ -2,7 +2,11 @@ import io.github.wechaty.Wechaty; +import io.github.wechaty.WechatyOptions; +import io.github.wechaty.schemas.PuppetOptions; +import io.github.wechaty.schemas.ScanStatus; import io.github.wechaty.user.Room; +import io.github.wechaty.utils.JsonUtils; import io.github.wechaty.utils.QrcodeUtils; import okhttp3.OkHttpClient; import org.apache.commons.lang3.StringUtils; @@ -13,21 +17,10 @@ public class Main { - public static void main(String[] args){ - - Wechaty bot = Wechaty.instance("your_token") - .onScan((qrcode, statusScanStatus, data) -> System.out.println(QrcodeUtils.getQr(qrcode))) - .onLogin(user -> System.out.println(user)) - .onMessage(message -> { - Room room = message.room(); - String text = message.text(); - if (StringUtils.equals(text, "#ding")) { - if (room != null) { - room.say("dong"); - } - } - }).start(true); - + public static void main(String[] args) { + String yourDiyToken = ""; +// runWithTarget(yourDiyToken, "127.0.0.1:8788"); + runWithCloud(yourDiyToken); // } // Room room = bot.room(); @@ -45,7 +38,54 @@ public static void main(String[] args){ // FileBox fileBox = FileBox.fromFile("dong.jpg", "dong.jpg"); // // room1.say(fileBox).get(); + } + + + public static void runWithCloud(String token) { + Wechaty bot = Wechaty.instance(token) + .onScan((qrcode, statusScanStatus, data) -> { + if (statusScanStatus == ScanStatus.Waiting) { + System.out.println(QrcodeUtils.getQr(qrcode)); + } + }) + .onLogin(user -> System.out.println(user.name() + "/" + user.gender() + "/" + user.city())) + .onMessage(message -> { + System.out.print(message.text() + "/" + message.talker().name() + "/"); + String text = message.text(); + if (StringUtils.equals(text, "#ding")) { + if (message.room() != null) { + message.room().say("dong"); + } else if (message.talker() != null) { + message.talker().say("dong"); + } + } + }).start(true); + } + public static void runWithTarget(String token, String hostPort) { + WechatyOptions wechatyOptions = new WechatyOptions(); + PuppetOptions puppetOptions = new PuppetOptions(); + puppetOptions.setToken(token); + puppetOptions.setEndPoint(hostPort); + wechatyOptions.setPuppetOptions(puppetOptions); + Wechaty bot = Wechaty.instance(wechatyOptions) + .onScan((qrcode, statusScanStatus, data) -> { + if (statusScanStatus == ScanStatus.Waiting) { + System.out.println(QrcodeUtils.getQr(qrcode)); + } + }) + .onLogin(user -> System.out.println(user.name() + "/" + user.gender() + "/" + user.city())) + .onMessage(message -> { + System.out.print(message.text() + "/" + message.talker().name() + "/"); + String text = message.text(); + if (StringUtils.equals(text, "#ding")) { + if (message.room() != null) { + message.room().say("dong"); + } else if (message.talker() != null) { + message.talker().say("dong"); + } + } + }).start(true); } diff --git a/wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt b/wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt index 7e61a7a..7919756 100644 --- a/wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt +++ b/wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt @@ -151,11 +151,12 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) { } if (StringUtils.isEmpty(discoverHostieIp.first) || StringUtils.equals(discoverHostieIp.first, "0.0.0.0")) { - log.error("cannot get ip by token, check token") + log.error("cannot get ip by token, check token or endPoint") exitProcess(1) } val newFixedThreadPool = newFixedThreadPool(16) - channel = ManagedChannelBuilder.forAddress(discoverHostieIp.first, NumberUtils.toInt(discoverHostieIp.second)).usePlaintext().executor(newFixedThreadPool).build() + channel = ManagedChannelBuilder.forAddress(discoverHostieIp.first, NumberUtils.toInt(discoverHostieIp.second)) + .overrideAuthority(puppetOptions?.token).usePlaintext().executor(newFixedThreadPool).build() grpcClient = PuppetGrpc.newBlockingStub(channel) grpcAsyncClient = PuppetGrpc.newStub(channel) @@ -946,7 +947,7 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) { log.debug("PuppetHostie $type payload $payload") if (type != Event.EventType.EVENT_TYPE_HEARTBEAT) { - emit(EventEnum.HEART_BEAT, EventHeartbeatPayload("heartbeat",6000)) + emit(EventEnum.HEART_BEAT, EventHeartbeatPayload("heartbeat", 6000)) } when (type) { @@ -1005,7 +1006,7 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) { Event.EventType.EVENT_TYPE_SCAN -> { val eventScanPayload = JsonUtils.readValue(payload) - log.debug("scan pay load is {}", eventScanPayload) + log.debug("scan pay loadx is {}", eventScanPayload) emit(EventEnum.SCAN, eventScanPayload) } diff --git a/wechaty-puppet/src/main/kotlin/Puppet.kt b/wechaty-puppet/src/main/kotlin/Puppet.kt index a91cdc1..5ca4f76 100644 --- a/wechaty-puppet/src/main/kotlin/Puppet.kt +++ b/wechaty-puppet/src/main/kotlin/Puppet.kt @@ -483,7 +483,6 @@ abstract class Puppet : EventEmitter { val filterKv = list.get(0) val filterFunction = { payload: ContactPayload -> - Boolean val clazz = payload::class.java val field = clazz.getField(filterKv.first) val toString = field.get(payload).toString() From d372dcbc715b19cdc5b518bc83721eabf0a67cba Mon Sep 17 00:00:00 2001 From: IanCao Date: Sun, 15 May 2022 22:22:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?mod=EF=BC=9A=E4=BF=AE=E5=A4=8D=E6=8F=8F?= =?UTF-8?q?=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt b/wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt index 7919756..90f530e 100644 --- a/wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt +++ b/wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt @@ -1006,7 +1006,7 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) { Event.EventType.EVENT_TYPE_SCAN -> { val eventScanPayload = JsonUtils.readValue(payload) - log.debug("scan pay loadx is {}", eventScanPayload) + log.debug("scan pay load is {}", eventScanPayload) emit(EventEnum.SCAN, eventScanPayload) }