-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
449 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/zhanganzhi/chathub/platforms/qq/QQAdaptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.zhanganzhi.chathub.platforms.qq; | ||
|
||
import com.alibaba.fastjson2.JSONArray; | ||
import com.alibaba.fastjson2.JSONObject; | ||
import com.zhanganzhi.chathub.ChatHub; | ||
import com.zhanganzhi.chathub.core.adaptor.AbstractAdaptor; | ||
import com.zhanganzhi.chathub.core.events.MessageEvent; | ||
import com.zhanganzhi.chathub.platforms.Platform; | ||
import com.zhanganzhi.chathub.platforms.qq.dto.QQEvent; | ||
import com.zhanganzhi.chathub.platforms.qq.protocol.QQAPI; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class QQAdaptor extends AbstractAdaptor<QQFormatter> { | ||
private final QQAPI qqAPI; | ||
private final Thread eventListener; | ||
private boolean listenerStopFlag = false; | ||
|
||
public QQAdaptor(ChatHub chatHub) { | ||
super(chatHub, Platform.QQ, new QQFormatter()); | ||
qqAPI = new QQAPI(chatHub); | ||
eventListener = new Thread(this::eventListener, "qq-event-listener"); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
chatHub.getLogger().info("QQ enabled"); | ||
qqAPI.start(); | ||
eventListener.start(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
// stop listener | ||
listenerStopFlag = true; | ||
|
||
// interrupt listener, clear event queue | ||
if (eventListener != null) { | ||
eventListener.interrupt(); | ||
} | ||
|
||
// close ws server | ||
qqAPI.stop(); | ||
} | ||
|
||
@Override | ||
public void sendPublicMessage(String message) { | ||
new Thread(() -> qqAPI.sendMessage(message, config.getQQGroupId())).start(); | ||
} | ||
|
||
public void eventListener() { | ||
while (!listenerStopFlag) { | ||
consumeEvent(); | ||
try { | ||
Thread.sleep(2000); | ||
} catch (InterruptedException e) { | ||
if (listenerStopFlag) { | ||
// clear other event | ||
consumeEvent(); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void consumeEvent() { | ||
QQEvent curEvent; | ||
while ((curEvent = qqAPI.getQqEventQueue().poll()) != null) { | ||
if ( | ||
"message".equals(curEvent.getPostType()) | ||
&& "group".equals(curEvent.getMessageType()) | ||
&& "array".equals(curEvent.getMessageFormat()) | ||
&& config.getQQGroupId().equals(curEvent.getGroupId().toString()) | ||
) { | ||
JSONArray message = curEvent.getMessage(); | ||
List<String> messages = new ArrayList<>(); | ||
for (int i = 0; i < message.size(); i++) { | ||
JSONObject part = message.getJSONObject(i); | ||
if (part.getString("type").equals("text")) { | ||
messages.add(part.getJSONObject("data").getString("text")); | ||
} | ||
} | ||
String content = String.join(" ", messages); | ||
chatHub.getEventHub().onUserChat(new MessageEvent( | ||
Platform.QQ, null, curEvent.getSender().getNickname(), content | ||
)); | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/zhanganzhi/chathub/platforms/qq/QQFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.zhanganzhi.chathub.platforms.qq; | ||
|
||
import com.zhanganzhi.chathub.core.formatter.AbstractFormatter; | ||
import com.zhanganzhi.chathub.core.formatter.FormattingContent; | ||
import com.zhanganzhi.chathub.platforms.Platform; | ||
|
||
public class QQFormatter extends AbstractFormatter { | ||
protected QQFormatter() { | ||
super(Platform.QQ); | ||
} | ||
|
||
@Override | ||
protected String replaceAll(String message, FormattingContent content) { | ||
return getPlainString(super.replaceAll(message, content)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/zhanganzhi/chathub/platforms/qq/dto/QQEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.zhanganzhi.chathub.platforms.qq.dto; | ||
|
||
import com.alibaba.fastjson2.JSONArray; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class QQEvent { | ||
private Long selfId; | ||
private Long messageId; | ||
private Long realId; | ||
private Long groupId; | ||
private Long time; | ||
private String postType; | ||
private String metaEventType; | ||
private String messageType; | ||
private Sender sender; | ||
private String rawMessage; | ||
private String subType; | ||
private JSONArray message; | ||
private String messageFormat; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/zhanganzhi/chathub/platforms/qq/dto/Sender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.zhanganzhi.chathub.platforms.qq.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Sender { | ||
private Long userId; | ||
private String nickname; | ||
private String card; | ||
private String role; | ||
} |
Oops, something went wrong.