Skip to content

MOD: add authority of grpc & add a new demo case #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions examples/src/main/java/io/github/wechaty/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion wechaty-puppet/src/main/kotlin/Puppet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down