-
Openshift
Create a project
oc new-project livedemo
Install Kamel operator
kamel install --cluster-setup kamel install
Create a secret called application.properties with telegram and slack credentials:
camel.component.slack.webhook-url=https://hooks.slack.com/services/your-token camel.component.telegram.authorization-token=SEU_TOKEN_TELEGRAM
Create a CamelBot.java class and run just to validate if everything is working fine
import org.apache.camel.builder.RouteBuilder;
public class CamelBot extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:demo")
.log("working :)")
}
}
kamel run CamelBot.java --dev
Start to work in the Telegram Bot, the final version will be something like it:
CamelBot.java
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.Exchange;
import org.apache.camel.model.dataformat.JsonLibrary;
public class CamelBot extends RouteBuilder {
@Override
public void configure() throws Exception {
from("telegram:bots")
.log("command received ${body}")
.convertBodyTo(String.class)
.choice()
.when(simple("${body} == 'joke'"))
.log("action joke triggered")
.to("http://api.icndb.com/jokes/random")
.unmarshal().json(JsonLibrary.Jackson)
.transform(simple("${body[value][joke]}"))
.to("telegram:bots/{{token}}")
.to("slack:#integration")
.when(simple("${body} == 'publish'"))
.log("action publish triggered")
.otherwise()
.setBody().simple("Action not found. Supported actions:\n *joke\n*publish")
.to("telegram:bots")
.to("slack:#integration");
}
}
kamel run CamelBot.java --dev --secret=camelk-bot
To run Slack Integration
Rafael Soares did a bot that search for CNPJs
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.Exchange; import org.apache.camel.model.dataformat.JsonLibrary;
public class FuseBot extends RouteBuilder {
@Override
public void configure() throws Exception {
from("telegram:bots/{{telegramToken}}")
.log("command received from Bot: ${body}")
.convertBodyTo(String.class)
.choice()
.when(simple("${body} == '/joke'"))
.log("requesting Chuck to handle this...")
.to("http://api.icndb.com/jokes/random?throwExceptionOnFailure=false")
.unmarshal().json(JsonLibrary.Jackson)
.transform(simple("${body[value][joke]}"))
.to("telegram:bots/{{telegramToken}}")
.when(simple("${body} == '/hello'"))
.log("Hello BANESE!!!")
.setBody().simple("Olá BANESE!!!")
.to("telegram:bots/{{telegramToken}}")
.when(simple("${body} starts with '/cnpj'"))
.log("CNPJ (${body.length}): [${body.substring(6,${body.length})}]")
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.HTTP_PATH)
.simple("v1/cnpj/${body.substring(6,${body.length})}")
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http://www.receitaws.com.br/")
.unmarshal().json(JsonLibrary.Jackson)
.transform(simple("${body[nome]}"))
.to("telegram:bots/{{telegramToken}}")
.otherwise()
.setBody().simple("Action not found. Supported actions:\n *joke\n*publish")
.to("telegram:bots/{{telegramToken}}");
}
}