Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Sending a telegram with Apache Camel K and Visual Studio Code

September 30, 2019
Brian Fitzpatrick
Related topics:
Developer ToolsIDEs

Share:

    When I was introduced to Apache Camel K a few months ago, I was amazed at how quickly developers could write and deploy an Apache Camel-based integration on Kubernetes. We immediately started work on creating Microsoft Visual Studio (VS) Code-based tools to make things even easier.

    What is Camel K? It’s a lightweight integration framework built from Apache Camel and designed for a serverless/microsystem world that runs natively on Kubernetes. It lets developers write integrations in their favorite Camel DSL and quickly deploy them on Kubernetes or Red Hat OpenShift. You can even write your integrations in lightweight languages like Groovy or JavaScript.

    We already built language support in VS Code for Apache Camel using the Language Server Protocol (LSP), offering auto-completion for Camel component URIs in both XML and Java. More recently, we began adding the same support for Groovy, JavaScript, YAML, and Kotlin. (See the Apache Camel LSP client project for details.)

    Now, with our new Tooling for Apache Camel K by Red Hat extension, we add support for Camel K in your IDE. To illustrate the tools in action, let’s start with a simple user story inspired by an article that Nicola Ferraro wrote a few years ago (Creating a Telegram Bot in 5 minutes with Apache Camel).

    • As a developer, I want to send myself a telegram notice any time somebody mentions the hashtag #camelkrocks so that I can better engage with our social media community.

    To get started, let’s assume that you have the following:

    • Telegram account and access to the Telegram messaging client or access via https://web.telegram.org/.
    • Apache Camel K running in an accessible Kubernetes instance, with the Kubernetes and Camel K command line tools installed locally.
    • Microsoft Visual Studio Code running locally with the Tooling for Apache Camel K by Red Hat extension installed.

    Before you create the Camel K side of the Telegram bot, you have to create your bot on Telegram. Nicola did a great job detailing those steps in his article; see the section on creating the Telegram bot there and give your bot a unique name, like camelk-hashtag-telegram-bot. Be sure to note the authorization token given by the @BotFather.

    Now in VS Code, create a new workspace with an empty directory called hashtagbot. In the hashtagbot directory, create a file named hashtagbot.groovy and copy this text into the file:

    from("telegram:bots/replace-me-with-your-telegram-token")
        .choice()
            .when()
                .simple('${in.body} != null')
                .to("direct:response1")
    
    from("direct:response1")
        .log('Incoming message from Telegram is ${in.body}')
        .setBody()
            .simple('You said \"${in.body}\"?')
            .to("telegram:bots/replace-me-with-your-telegram-token")

    Note: Remember to replace “replace-me-with-your-telegram-token” with your Telegram authorization token given by the @BotFather.

    After you save your file, right-click it and select Start Apache Camel K Integration from the pop-up menu. A drop-down appears in the command palette with a few choices. Select Dev Mode - Apache Camel K Integration in Dev Mode.

    As your integration starts to run, watch the Apache Camel K output channel that appears:

    In your Telegram app, find your bot (use the name you specified when you created the bot in @BotFather) and start a new conversation. Everything you type in Telegram should be repeated with “You said “<whatyoutyped>?” For example:

    Next, update your route to check whether a given string has our hashtag #camelkrocks in it and respond accordingly.

    Change the second route in your hashtagbot.groovy file as follows:

    from("direct:response1")
        .log('Incoming message from Telegram is ${in.body}')
            .choice()
            .when(simple('${bodyAs(java.lang.String)} contains "#camelkrocks"'))
            .setBody(simple('Did somebody say #camelrocks? Of course it does!'))
            .to("telegram:bots/replace-me-with-your-telegram-token")
                    .end()

    Once you save your route, it updates the deployed integration and you can try it out. For example:

    Though this is a very simple example, you could use the approach as a basis to do things like routing messages to a database or to an email address for follow-up later.

    We also have different deployment options that can save you from having to include your telegram token in the body of your route.

    One way is to use the Apache Camel K “property” option.

    1. In the hashtagbot.groovy file, edit the route by replacing your token with {{telegram-token}}.
    2. When you start the integration, deploy the route by selecting the Property - Apache Camel K Integration with Property option in the drop-down menu.
    3. Specify a property with the name telegram-token and the value as your telegram authentication token. The runtime will replace {{telegram-token}} with your token value.

    Another way is to use the Apache Camel K ConfigMap or Secret method.  With this method, your token value is secured in the Kubernetes system.

    For example, to use the Secret method:

    1. Create a new application.properties file and save it to your hashtagbot folder.
    2. In the application.properties file, add the following line (replace the value with your actual token):
      telegram-token=replace-me-with-your-telegram-token
    3. Right-click on the application.properties file and then select Create Kubernetes Secret from File.
    4. Name the new secret telegram-secret.
    5. In the hashtagbot.groovy file, edit the route by replacing your token with {{telegram-token}}.
    6. When you start the integration, deploy the route by selecting the Secret - Apache Camel K Integration with Kubernetes Secret option in the drop-down menu.
    7. In the drop-down list that appears, specify the telegram-secret that you created. The Camel K runtime will replace {{telegram-token}} with your token value.

    To learn more, check out the latest on Apache Camel K and see the Tooling for Apache Camel K by Red Hat extension for Microsoft VS Code.

    Happy coding!

    Last updated: July 1, 2020

    Recent Posts

    • GuideLLM: Evaluate LLM deployments for real-world inference

    • Unleashing multimodal magic with RamaLama

    • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

    • Streamline multi-cloud operations with Ansible and ServiceNow

    • Automate dynamic application security testing with RapiDAST

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue