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..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 @@ -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) { 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()