343.8. Reactive Chat-Bot 示例
被动 chat-bot 模式是一个简单的使用 Camel 组件来构建简单的 chat bot,它直接回复从 Telegram 用户接收的消息。
以下是 Java DSL 中 chat-bot 的基本配置
from("telegram:bots/123456789:insertYourAuthorizationTokenHere")
.bean(ChatBotLogic.class)
.to("telegram:bots/123456789:insertYourAuthorizationTokenHere");
或在 Spring XML 中
<route>
<from uri="telegram:bots/123456789:insertYourAuthorizationTokenHere"/>
<bean ref="chatBotLogic" />
<to uri="telegram:bots/123456789:insertYourAuthorizationTokenHere"/>
<route>
<bean id="chatBotLogic" class="com.example.ChatBotLogic"/>
ChatBotLogic 是一个实现通用 String-to-String 方法的简单 bean。
public class ChatBotLogic {
public String chatBotProcess(String message) {
if( "do-not-reply".equals(message) ) {
return null; // no response in the chat
}
return "echo from the bot: " + message; // echoes the message
}
}
Chat BotProcess 方法返回的每个非null 字符串都会自动路由到发起请求的聊天方式(因为 CamelTelegramChatId 标头用于路由消息)。