53.8. 重新主动 Chat-Bot 示例


reactive chat-bot 模式是使用 Camel 组件构建简单的 chat bot 的一种简单方式,它直接回复来自 gramgram 用户收到的 chat 消息。

以下是 Java DSL 中 chat-bot 的基本配置

from("telegram:bots?authorizationToken=123456789:insertYourAuthorizationTokenHere")
.bean(ChatBotLogic.class)
.to("telegram:bots?authorizationToken=123456789:insertYourAuthorizationTokenHere");
Copy to Clipboard Toggle word wrap

或者在 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"/>
Copy to Clipboard Toggle word wrap

ChatBotLogic 是一种简单的 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
    }

}
Copy to Clipboard Toggle word wrap

chatBotProcess 方法返回的每个非空字符串会自动路由到源自请求的 chat (因为 CamelTelegramChatId 标头用于路由消息)。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat