An open API service indexing awesome lists of open source software.

https://github.com/cheivin/jvavwechatplugin


https://github.com/cheivin/jvavwechatplugin

Last synced: 3 days ago
JSON representation

Awesome Lists containing this project

README

          

# jvav群小助手java插件开发

## 安装

```xml

io.github.cheivin
jvavwechatplugin
2.0.0

```

### sse协议客户端示例

```java

import io.github.cheivin.assistant.message.CommandMessage;
import io.github.cheivin.assistant.message.Message;
import io.github.cheivin.assistant.plugin.AbstractPluginHandler;
import io.github.cheivin.assistant.plugin.PluginRequest;
import io.github.cheivin.assistant.plugin.PluginResponse;
import io.github.cheivin.assistant.protocol.sse.SSEMessageClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class WebSocketMessageListenerTest {
public static void main(String[] args) {
SSEMessageClient client = new SSEMessageClient("http://127.0.0.1:8080/sse", "plugin", "plugin");
ExampleHandler handler = new ExampleHandler().addPlugin(new AbstractPluginHandler(client.getSender()) {
@Override
public List getCommands() {
return List.of("ping");
}

@Override
public String getDescription() {
return "ping pong消息";
}

@Override
public PluginResponse handle(PluginRequest request) {
return PluginResponse.text("pong");
}
});

client.start(handler);

CountDownLatch latch = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread(latch::countDown));

System.out.println("开始监听消息,按 Ctrl+C 退出...");
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("程序即将退出...");
}

static class ExampleHandler implements IMessageHandler {
private final Map commands;

public ExampleHandler() {
this.commands = new HashMap<>();
}

public ExampleHandler addPlugin(AbstractPluginHandler handler) {
for (String command : handler.getCommands()) {
commands.put(command, handler);
}
return this;
}

@Override
public boolean onMessage(Message message) {
System.out.printf("%d %s: %s\n", message.getTime(), message.getUsername(), message.getContent());
CommandMessage cmd = message.toCommandMessage();
if (cmd != null && commands.containsKey(cmd.getCommand())) {
commands.get(cmd.getCommand()).onCommand(cmd.getCommand(), cmd.getParameter(), cmd);
}
return true;
}
}
}
```

### ws协议客户端示例

```java

import io.github.cheivin.assistant.message.CommandMessage;
import io.github.cheivin.assistant.message.Message;
import io.github.cheivin.assistant.plugin.AbstractPluginHandler;
import io.github.cheivin.assistant.plugin.PluginRequest;
import io.github.cheivin.assistant.plugin.PluginResponse;
import io.github.cheivin.assistant.protocol.ws.WebSocketMessageClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class WebSocketMessageListenerTest {
public static void main(String[] args) throws InterruptedException {
WebSocketMessageClient client = new WebSocketMessageClient("ws://127.0.0.1:8080/ws", "username", "password");
ExampleHandler handler = new ExampleHandler().addPlugin(new AbstractPluginHandler(client.getSender()) {
@Override
public List getCommands() {
return List.of("ping");
}

@Override
public String getDescription() {
return "ping pong消息";
}

@Override
public PluginResponse handle(PluginRequest request) {
return PluginResponse.text("pong");
}
});

client.start(handler);

CountDownLatch latch = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread(latch::countDown));

System.out.println("开始监听消息,按 Ctrl+C 退出...");
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("程序即将退出...");
}

static class ExampleHandler implements IMessageHandler {
private final Map commands;

public ExampleHandler() {
this.commands = new HashMap<>();
}

public ExampleHandler addPlugin(AbstractPluginHandler handler) {
for (String command : handler.getCommands()) {
commands.put(command, handler);
}
return this;
}

@Override
public boolean onMessage(Message message) {
System.out.printf("%d %s: %s\n", message.getTime(), message.getUsername(), message.getContent());
CommandMessage cmd = message.toCommandMessage();
if (cmd != null && commands.containsKey(cmd.getCommand())) {
commands.get(cmd.getCommand()).onCommand(cmd.getCommand(), cmd.getParameter(), cmd);
}
return true;
}
}
}
```