https://github.com/cheivin/jvavwechatplugin
https://github.com/cheivin/jvavwechatplugin
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/cheivin/jvavwechatplugin
- Owner: Cheivin
- Created: 2024-12-31T08:37:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-08T13:49:18.000Z (over 1 year ago)
- Last Synced: 2025-01-08T14:57:16.407Z (over 1 year ago)
- Language: Java
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
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;
}
}
}
```