Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Telegram component supports both consumer and producer endpoints. It can also be used in reactive chat-bot mode (to consume, then produce messages).

Producer Example

The following is a basic example of how to send a message to a Telegram chat through the Telegram Bot API.

in Java DSL

Code Block
languagejava
from("direct:start").to("telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L");

or in Spring XML

Code Block
languagexml
<route>
    <from uri="direct:start"/>
    <to uri="telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L"/>
</route>

The code 123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L is the authorization token corresponding to the Bot.

When using the producer endpoint without specifying the chat id option, the target chat will be identified using information contained in the body or headers of the message. The following message bodies are allowed for a producer endpoint (messages of type OutgoingXXXMessage belong to the package org.apache.camel.component.telegram.model)

Div
classconfluenceTableSmall
Java TypeDescription

OutgoingTextMessage

To send a text message to a chat

OutgoingPhotoMessage

To send a photo (JPG, PNG) to a chat

OutgoingAudioMessage

To send a mp3 audio to a chat

OutgoingVideoMessage

To send a mp4 video to a chat

byte[]

To send any media type supported. It requires the CamelTelegramMediaType header to be set to the appropriate media type

String

To send a text message to a chat. It gets converted automatically into a OutgoingTextMessage

 

Consumer Example

The following is a basic example of how to receive all messages that telegram users are sending to the configured Bot.

In Java DSL

Code Block
languagejava
from("telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L")
.bean(ProcessorBean.class)

or in Spring XML

Code Block
languagexml
<route>
    <from uri="telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L"/>
    <bean ref="myBean" />
</route>
<bean id="myBean" class="com.example.MyBean"/>

The MyBean is a simple bean that will receive the messages

Code Block
languagejava
public class MyBean {
    public void process(String message) {
        // or Exchange, or org.apache.camel.component.telegram.model.IncomingMessage (or both)
        // do process
    }
}

Supported types for incoming messages are

Div
classconfluenceTableSmall
Java TypeDescription

IncomingMessage

The full object representation of an incoming message

String

The content of the message, for text messages only

Reactive Chat-Bot Example

The reactive chat-bot mode is a simple way of using the Camel component to build a simple chat bot that replies directly to chat messages received from the Telegram users.

The following is a basic configuration of the chat-bot in Java DSL

Code Block
languagejava
from("telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L")
.bean(ChatBotLogic.class)
.to("telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L");

or in Spring XML

Code Block
languagexml
<route>
    <from uri="telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L"/>
    <bean ref="chatBotLogic" />
    <to uri="telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L"/>
</route>
<bean id="chatBotLogic" class="com.example.ChatBotLogic"/>

The ChatBotLogic is a simple bean that implements a generic String-to-String method.

Code Block
languagejava
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
    }
}

Every non-null string returned by the chatBotProcess method is automatically routed to the chat that originated the request (as the CamelTelegramChatId header is used to route the message).