134.9. reactive Chat-Bot 示例
reactive chat-bot 模式是使用 Camel 组件构建一个简单的 chat bot,它直接回复 Telegram 用户收到的聊天消息。
以下是 Java DSL 中 chat-bot 的基本配置
from("telegram:bots?authorizationToken=123456789:insertYourAuthorizationTokenHere")
.bean(ChatBotLogic.class)
.to("telegram:bots?authorizationToken=123456789:insertYourAuthorizationTokenHere");
或在 Spring XML 中
<route>
<from uri="telegram:bots?authorizationToken=123456789:insertYourAuthorizationTokenHere"/>
<bean ref="chatBotLogic" />
<to uri="telegram:bots?authorizationToken=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
}
}
由 chatBotProcess 方法返回的每个非null 字符串会自动路由到源自请求的 chat (因为 CamelTelegramChatId 标头被用来路由消息)。