343.8. リアクティブなチャットボットの例

リアクティブチャットボットモードは、Camel コンポーネントを使用して、Telegram ユーザーから受信したチャットメッセージに直接返信する単純なチャットボットを構築する簡単な方法です。

以下は、Java DSL でのチャットボットの基本設定です。

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
    }

}

chatBotProcess メソッドによって返されるすべての非 null 文字列は、リクエストを発信したチャットに自動的にルーティングされます (メッセージのルーティングには CamelTelegramChatId ヘッダーが使用されるため)。