关于持续会话 #122
关于持续会话
#122
Replies: 1 comment 1 reply
-
/**
* 监听好友消息:
* <p>
* 好友发送:改名字
* bot:改什么名字?(1分钟限时)
* 好友:xxx(名字)
* bot:更改成功!
*/
@Listener
@Filter("改名字")
public void demo(FriendMessageEvent event, ContinuousSessionContext context) {
event.replyBlocking("改什么名字?");
MessageContent nextMessage;
try {
// 等待并获取下一个类型为 FriendMessageEvent 的消息事件中的消息,
// 且使用当前 event 作为基础过滤条件(目标用户的ID等基础信息),
// 并且限时一分钟,否则抛出 TimeoutCancellationException 异常
nextMessage = context.nextMessage(event, FriendMessageEvent.Key, Duration.ofMinutes(1));
} catch (TimeoutCancellationException timeout) {
// 超时
event.replyBlocking("超过1分钟了喔!");
return;
}
String newName = nextMessage.getPlainText().trim();
if (newName.isEmpty()) {
// 验证失败
event.replyBlocking("新名字不可为空喔!");
}
event.replyBlocking("更改成功! 新名字为: " + newName);
}
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
88Fanchen
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
涉及的编程语言
Java
涉及的组件库
mirai (QQ)
疑问描述
新人上线期待回复,看完持续对话的文档还是有点云里雾里,能否给出一个针对私聊的持续对话的简单样例,感激不尽,谢谢!祝生活愉快
Beta Was this translation helpful? Give feedback.
All reactions