...
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 | ||
---|---|---|
| ||
from("direct:start").to("telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L"); |
or in Spring XML
Code Block | ||
---|---|---|
| ||
<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 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||
|
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 | ||
---|---|---|
| ||
from("telegram:bots/123456789:AAE_dLq5C19xwGjw3yiC2NvEUrZcejK21-Q987654321:AAE_dLq5C19xwOmg5yiC2NvSrkT3wj5Q1-L") .bean(ProcessorBean.class) |
or in Spring XML
Code Block | ||
---|---|---|
| ||
<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 | ||
---|---|---|
| ||
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
|
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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
<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 | ||
---|---|---|
| ||
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).