Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ Scan now, because other Wechaty Java developers want to talk with you too! (secr
```java
class Bot{
public static void main(String args[]){
bot = Wechaty.instance()
.on('scan', (qrcode, status, string) -> System.out.println('Scan QR Code to login: %s\nhttps://api.qrserver.com/v1/create-qr-code/?data=%s', status, encodeURIComponent(qrcode)))
.on('login', user -> System.out.println('User %s logined', user))
.on('message', message -> System.out.println('Message: %s', message))
.start();
Wechaty bot = Wechaty.instance()
.onScan((qrcode, statusScanStatus, data) -> System.out.println(QrcodeUtils.getQr(qrcode)))
.onLogin(user -> System.out.println("User logined :" + user))
.onMessage(message -> System.out.println("Message:" + message))
.start(true);
}
}
```
Expand Down Expand Up @@ -264,9 +264,11 @@ mvn install wechaty

### master

### v0.1.1 (June 6 2020)
1. update wechaty grpc to 0.16.1
2. move examples from wechaty to independent module. Make example easy to use.
### v0.1.2 (June 6 2020)
1. change method `on(Event:String,Listener:listener)` to `onEvent(Listener:listener)`. See examples
2. update version to 0.1.2
3. update wechaty grpc to 0.16.1
4. move examples from wechaty to independent module. Make example easy to use.

### v0.1.1 (May 31 2020)

Expand Down
4 changes: 2 additions & 2 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty-parent</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wechaty-example</artifactId>
Expand All @@ -24,7 +24,7 @@
<dependency>
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
36 changes: 15 additions & 21 deletions examples/src/main/java/io/github/wechaty/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.wechaty.example;


import io.github.wechaty.LoginListener;
import io.github.wechaty.MessageListener;
import io.github.wechaty.Wechaty;
import io.github.wechaty.filebox.FileBox;
import io.github.wechaty.io.github.wechaty.schemas.EventEnum;
import io.github.wechaty.user.Contact;
import io.github.wechaty.user.Room;
import io.github.wechaty.utils.QrcodeUtils;
Expand All @@ -16,29 +18,21 @@

public class Main {

public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {

Wechaty bot = Wechaty.instance("your_token");

bot.on("scan", (qrcode, statusScanStatus, data) -> {
System.out.println(QrcodeUtils.getQr(qrcode));
});

bot.on("message", (MessageListener) message -> {

Contact from = message.from();
Room room = message.room();

String text = message.text();

if (StringUtils.equals(text, "#ding")) {
if (room != null) {
room.say("dong");
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);

bot.start(true);
// }

// Room room = bot.room();
Expand Down
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty-parent</artifactId>
<packaging>pom</packaging>
<version>0.1.1-SNAPSHOT</version>
<version>0.1.2-SNAPSHOT</version>
<name>kotlin-wechaty</name>

<description>
Expand Down Expand Up @@ -147,12 +147,6 @@
<artifactId>okhttp</artifactId>
<version>4.6.0</version>
</dependency>
<!-- &lt;!&ndash; https://mvnrepository.com/artifact/io.grpc/grpc-kotlin-stub &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>io.grpc</groupId>-->
<!-- <artifactId>grpc-kotlin-stub</artifactId>-->
<!-- <version>0.1.1</version>-->
<!-- </dependency>-->

<dependency>
<groupId>org.hamcrest</groupId>
Expand Down
2 changes: 1 addition & 1 deletion wechaty-puppet-hostie/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty-parent</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wechaty-puppet-hostie</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.wechaty.Puppet
import io.github.wechaty.StateEnum
import io.github.wechaty.filebox.FileBox
import io.github.wechaty.grpc.puppet.*
import io.github.wechaty.io.github.wechaty.schemas.EventEnum
import io.github.wechaty.schemas.*
import io.github.wechaty.utils.JsonUtils
import io.grpc.ManagedChannel
Expand All @@ -19,6 +20,7 @@ import java.util.concurrent.CompletableFuture
import java.util.concurrent.Executors.newFixedThreadPool
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import kotlin.system.exitProcess

/**
* puppet
Expand Down Expand Up @@ -97,7 +99,7 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {

try {
if (logonoff()) {
emit("logout",EventLogoutPayload(getId()!!,"logout"))
emit(EventEnum.LOGOUT,EventLogoutPayload(getId()!!,"logout"))

this.setId(null)
}
Expand Down Expand Up @@ -148,7 +150,7 @@ 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")
throw Exception("cannot get ip by token, check token")
exitProcess(1)
}
val newFixedThreadPool = newFixedThreadPool(16)
channel = ManagedChannelBuilder.forAddress(discoverHostieIp.first,NumberUtils.toInt(discoverHostieIp.second)).usePlaintext().executor(newFixedThreadPool).build()
Expand All @@ -167,7 +169,7 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
override fun onError(t: Throwable?) {
log.error("error of grpc",t)
val payload = EventResetPayload(t?.message ?:"")
emit("reset",payload)
emit(EventEnum.RESET,payload)
}

override fun onCompleted() {
Expand Down Expand Up @@ -200,7 +202,7 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
} catch (e: Exception) {
log.error("logout() rejection: %s", e)
} finally {
emit("logout",EventLogoutPayload(getId()!!,"logout"))
emit(EventEnum.LOGOUT,EventLogoutPayload(getId()!!,"logout"))
}
return CompletableFuture.completedFuture(null)
}
Expand Down Expand Up @@ -942,67 +944,67 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
log.debug("PuppetHostie $type payload $payload")

if (type != Event.EventType.EVENT_TYPE_HEARTBEAT) {
emit("heartbeat", EventHeartbeatPayload("heartbeat"))
emit(EventEnum.HEART_BEAT, EventHeartbeatPayload("heartbeat"))
}

when (type) {
Event.EventType.EVENT_TYPE_DONG -> emit("dong", JsonUtils.readValue<EventDongPayload>(payload))
Event.EventType.EVENT_TYPE_DONG -> emit(EventEnum.DONG, JsonUtils.readValue<EventDongPayload>(payload))

Event.EventType.EVENT_TYPE_ERROR -> {
emit("error", JsonUtils.readValue<EventErrorPayload>(payload))
emit(EventEnum.ERROR, JsonUtils.readValue<EventErrorPayload>(payload))
}

Event.EventType.EVENT_TYPE_HEARTBEAT -> {
val heartbeatPayload = JsonUtils.readValue<EventHeartbeatPayload>(payload)
emit("heartbeat", heartbeatPayload)
emit(EventEnum.HEART_BEAT, heartbeatPayload)
}

Event.EventType.EVENT_TYPE_FRIENDSHIP -> {
val friendshipPayload = JsonUtils.readValue<EventFriendshipPayload>(payload)
emit("friendship", friendshipPayload)
emit(EventEnum.FRIENDSHIP, friendshipPayload)
}

Event.EventType.EVENT_TYPE_LOGIN -> {
val loginPayload = JsonUtils.readValue<EventLoginPayload>(payload)
setId(loginPayload.contactId)
emit("login", loginPayload)
emit(EventEnum.LOGIN, loginPayload)
}

Event.EventType.EVENT_TYPE_LOGOUT -> {
this.setId("")
emit("logout", JsonUtils.readValue<EventLogoutPayload>(payload))
emit(EventEnum.LOGOUT, JsonUtils.readValue<EventLogoutPayload>(payload))
}


Event.EventType.EVENT_TYPE_MESSAGE -> {
val eventMessagePayload = JsonUtils.readValue<EventMessagePayload>(payload)
emit("message", eventMessagePayload)
emit(EventEnum.MESSAGE, eventMessagePayload)
}

Event.EventType.EVENT_TYPE_READY -> {
emit("ready", JsonUtils.readValue<EventReadyPayload>(payload))
emit(EventEnum.READY, JsonUtils.readValue<EventReadyPayload>(payload))
}

Event.EventType.EVENT_TYPE_ROOM_INVITE -> {
emit("room-invite", JsonUtils.readValue<EventRoomInvitePayload>(payload))
emit(EventEnum.ROOM_INVITE, JsonUtils.readValue<EventRoomInvitePayload>(payload))
}

Event.EventType.EVENT_TYPE_ROOM_JOIN -> {
emit("room-join", JsonUtils.readValue<EventRoomJoinPayload>(payload))
emit(EventEnum.ROOM_JOIN, JsonUtils.readValue<EventRoomJoinPayload>(payload))
}

Event.EventType.EVENT_TYPE_ROOM_LEAVE -> {
emit("room-leave", JsonUtils.readValue<EventRoomLeavePayload>(payload))
emit(EventEnum.ROOM_LEAVE, JsonUtils.readValue<EventRoomLeavePayload>(payload))
}

Event.EventType.EVENT_TYPE_ROOM_TOPIC -> {
emit("room-topic", JsonUtils.readValue<EventRoomTopicPayload>(payload))
emit(EventEnum.ROOM_TOPIC, JsonUtils.readValue<EventRoomTopicPayload>(payload))
}

Event.EventType.EVENT_TYPE_SCAN -> {
val eventScanPayload = JsonUtils.readValue<EventScanPayload>(payload)
log.debug("scan pay load is {}", eventScanPayload)
emit("scan", eventScanPayload)
emit(EventEnum.SCAN, eventScanPayload)
}

Event.EventType.EVENT_TYPE_RESET -> {
Expand Down
20 changes: 1 addition & 19 deletions wechaty-puppet-mock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>wechaty-parent</artifactId>
<groupId>io.github.wechaty</groupId>
<version>0.1.1-SNAPSHOT</version>
<version>0.1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -21,24 +21,6 @@
<artifactId>wechaty-puppet</artifactId>
<version>${project.version}</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-api</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-core</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-slf4j-impl</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-api</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.wechaty

import io.github.wechaty.filebox.FileBox
import io.github.wechaty.io.github.wechaty.schemas.EventEnum
import io.github.wechaty.schemas.*
import org.slf4j.LoggerFactory
import java.util.*
Expand Down Expand Up @@ -30,15 +31,15 @@ class MockPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
*/
val eventScanPayload = EventScanPayload(ScanStatus.Cancel)
eventScanPayload.qrcode = "https://github.com/wechaty/java-wechaty/wechaty-puppet-mock"
emit("scan", eventScanPayload)
emit(EventEnum.SCAN, eventScanPayload)


val userPayload = MockData.getFakeContactPayload()
cacheContactPayload.put(userPayload.id, userPayload)

setId(userPayload.id)

emit("login", EventLoginPayload(userPayload.id))
emit(EventEnum.LOGIN, EventLoginPayload(userPayload.id))

timer.scheduleAtFixedRate(0, 5000) {
val fromContactPayload = MockData.getFakeContactPayload()
Expand All @@ -47,7 +48,7 @@ class MockPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {

cacheMessagePayload.put(messagePayload.id, messagePayload)
log.info("MockPuppet start() schedule pretending received a new message:${messagePayload.id}")
emit("message", EventMessagePayload(messagePayload.id))
emit(EventEnum.MESSAGE, EventMessagePayload(messagePayload.id))
}
return CompletableFuture.completedFuture(null)
}
Expand All @@ -74,15 +75,15 @@ class MockPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
log.info("MockPuppet logout()")
val id = getId() ?: throw Exception("logout before login?")

emit("logout", EventLogoutPayload(id, "test"))
emit(EventEnum.LOGOUT, EventLogoutPayload(id, "test"))
setId(null)

return CompletableFuture.completedFuture(null)
}

override fun ding(data: String?) {
log.info("MockPuppet ding(${data ?: ""})")
emit("dong", EventDongPayload(data ?: ""))
emit(EventEnum.DONG, EventDongPayload(data ?: ""))
}

override fun contactSelfName(name: String): Future<Void> {
Expand Down
2 changes: 1 addition & 1 deletion wechaty-puppet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty-parent</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wechaty-puppet</artifactId>
Expand Down
Loading