Skip to content

Instantly share code, notes, and snippets.

@hodrigohamalho
Last active June 24, 2020 12:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hodrigohamalho/34db35c3abacea8e63ddea32b37cbdbc to your computer and use it in GitHub Desktop.
Save hodrigohamalho/34db35c3abacea8e63ddea32b37cbdbc to your computer and use it in GitHub Desktop.
Camel K demo

Camel K

Pre requirements

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}}");
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment