Micronaut Chatbots

Ease the creation of Telegram, Google Chat, Discord chatbots with the Micronaut Framework

Version:

1 Introduction

This module eases the creation of Telegram Bots, Basecamp Bots with the Micronaut Framework.

2 Release History

For this project, you can find a list of releases (with release notes) here:

3 Quick Start

4 Telegram

You can develop Telegram Bot quickly with Micronaut Chatbots.

First, create a Telegram Bot by talking with the @BotFather

4.1 Telegram Webhook

You have to set the webhook via the Telegram API.

curl -X "POST" "https://api.telegram.org/bot{httpApiToken}/setWebhook?url={webhookUrl}&secret_token={secretToken}"
  • httpApiToken You obtain this value via the @BotFather

  • webhookUrl is the endpoint of your Micronaut application.

  • {secretToken} - A secret token, Telegram sends in a header X-Telegram-Bot-Api-Secret-Token in every webhook request.

4.2 Telegram Bot Configuration

micronaut.chatbots.telegram.bots.mnexample.token=xxxyyyzzz
micronaut.chatbots.telegram.bots.mnexample.at-username=@MicronautExampleBot
micronaut:
  chatbots:
    telegram:
      bots:
        mnexample:
          token: 'xxxyyyzzz'
          at-username: '@MicronautExampleBot'
[micronaut]
  [micronaut.chatbots]
    [micronaut.chatbots.telegram]
      [micronaut.chatbots.telegram.bots]
        [micronaut.chatbots.telegram.bots.mnexample]
          token="xxxyyyzzz"
          at-username="@MicronautExampleBot"
micronaut {
  chatbots {
    telegram {
      bots {
        mnexample {
          token = "xxxyyyzzz"
          atUsername = "@MicronautExampleBot"
        }
      }
    }
  }
}
{
  micronaut {
    chatbots {
      telegram {
        bots {
          mnexample {
            token = "xxxyyyzzz"
            at-username = "@MicronautExampleBot"
          }
        }
      }
    }
  }
}
{
  "micronaut": {
    "chatbots": {
      "telegram": {
        "bots": {
          "mnexample": {
            "token": "xxxyyyzzz",
            "at-username": "@MicronautExampleBot"
          }
        }
      }
    }
  }
}

micronaut.chabots.telegram.bots.*.token matches the value specified as secret_token while setting the webhook.

4.3 Telegram Bot Handlers

To develop your bot, create beans of type TelegramHandler.

package io.micronaut.chatbots.telegram;

import io.micronaut.chatbots.core.SpaceParser;
import io.micronaut.chatbots.telegram.api.Chat;
import io.micronaut.chatbots.telegram.api.Update;
import io.micronaut.chatbots.telegram.api.send.SendMessage;
import io.micronaut.chatbots.telegram.core.SendMessageUtils;
import io.micronaut.chatbots.telegram.core.TelegramBotConfiguration;
import io.micronaut.chatbots.telegram.core.TelegramHandler;
import jakarta.inject.Singleton;

import javax.validation.constraints.NotNull;
import java.util.Optional;

@Singleton
class HelloWorldHandler implements TelegramHandler<SendMessage> {

    private final SpaceParser<Update, Chat> spaceParser;

    HelloWorldHandler(SpaceParser<Update, Chat> spaceParser) {
        this.spaceParser = spaceParser;
    }

    @Override
    public boolean canHandle(TelegramBotConfiguration bot, @NotNull Update input) {
        return true;
    }

    @Override
    public Optional<SendMessage> handle(TelegramBotConfiguration bot, @NotNull Update input) {
        return SendMessageUtils.compose(spaceParser, input, "Hello World");
    }
}
package io.micronaut.chatbots.telegram

import io.micronaut.chatbots.core.SpaceParser
import io.micronaut.chatbots.telegram.api.Chat
import io.micronaut.chatbots.telegram.api.Update
import io.micronaut.chatbots.telegram.api.send.SendMessage
import io.micronaut.chatbots.telegram.core.SendMessageUtils
import io.micronaut.chatbots.telegram.core.TelegramBotConfiguration
import io.micronaut.chatbots.telegram.core.TelegramHandler
import jakarta.inject.Singleton

import javax.validation.constraints.NotNull

@Singleton
class HelloWorldHandler implements TelegramHandler<SendMessage> {

    private final SpaceParser<Update, Chat> spaceParser

    HelloWorldHandler(SpaceParser<Update, Chat> spaceParser) {
        this.spaceParser = spaceParser
    }

    @Override
    boolean canHandle(TelegramBotConfiguration bot, @NotNull Update input) {
        true
    }

    @Override
    Optional<SendMessage> handle(TelegramBotConfiguration bot, @NotNull Update input) {
        SendMessageUtils.compose(spaceParser, input, "Hello World")
    }
}
package io.micronaut.chatbots.telegram

import io.micronaut.chatbots.core.SpaceParser
import io.micronaut.chatbots.telegram.api.Chat
import io.micronaut.chatbots.telegram.api.Update
import io.micronaut.chatbots.telegram.api.send.SendMessage
import io.micronaut.chatbots.telegram.core.SendMessageUtils
import io.micronaut.chatbots.telegram.core.TelegramBotConfiguration
import io.micronaut.chatbots.telegram.core.TelegramHandler
import jakarta.inject.Singleton
import java.util.*

@Singleton
class HelloWorldHandler(private val spaceParser: SpaceParser<Update, Chat>) : TelegramHandler<SendMessage> {
    override fun canHandle(bot: TelegramBotConfiguration?, input: Update): Boolean = true

    override fun handle(bot: TelegramBotConfiguration?, input: Update): Optional<SendMessage> =
        SendMessageUtils.compose(spaceParser, input, "Hello World")
}

To respond to Telegram Bot commands, you can extend from CommandHandler.

4.4 Telegram Chatbots as an AWS Lambda Function

If you want to deploy to AWS Lambda, you can use the following dependency:

implementation("io.micronaut.chatbots:chatbots-telegram-lambda")
<dependency>
    <groupId>io.micronaut.chatbots</groupId>
    <artifactId>chatbots-telegram-lambda</artifactId>
</dependency>

Use the class io.micronaut.chatbots.telegram.lambda.Handler as the AWS Lambda function handler.

4.5 Telegram Chatbots as a Google Cloud Function

If you want to deploy to Google Cloud Functions, you can use the following dependency:

implementation("io.micronaut.chatbots:micronaut-chatbots-telegram-gcp-function")
<dependency>
    <groupId>io.micronaut.chatbots</groupId>
    <artifactId>micronaut-chatbots-telegram-gcp-function</artifactId>
</dependency>

You can use the following entry point:

io.micronaut.chatbots.telegram.googlecloud.Handler

4.6 Telegram Chatbots as an Azure Function

4.7 Telegram Chatbots Controller

If you want to configure one endpoint as the Telegram chatbot webhook while using a runtime such as Netty or Servlet, you can include the following dependency:

implementation("io.micronaut.chatbots:chatbots-telegram-http")
<dependency>
    <groupId>io.micronaut.chatbots</groupId>
    <artifactId>chatbots-telegram-http</artifactId>
</dependency>

You can configure the path with the following:

🔗
Table 1. Configuration Properties for TelegramControllerConfiguration
Property Type Description

micronaut.chatbots.telegram.endpoint.enabled

boolean

Enables the controller. Default value true .

micronaut.chatbots.telegram.endpoint.path

java.lang.String

Path to the controller. Default value "/telegram" .

5 Basecamp

You can develop Basecamp Chatbots quickly with Micronaut Chatbots.

5.1 Basecamp Bot Handlers

To develop your bot, create beans of type BasecampHandler.

package io.micronaut.chatbots.basecamp;

import io.micronaut.chatbots.basecamp.api.Query;
import io.micronaut.chatbots.basecamp.core.BasecampBotConfiguration;
import io.micronaut.chatbots.basecamp.core.BasecampHandler;
import jakarta.inject.Singleton;
import io.micronaut.core.annotation.NonNull;
import javax.validation.constraints.NotNull;
import java.util.Optional;

@Singleton
class HelloWorldHandler implements BasecampHandler {

    @Override
    public boolean canHandle(BasecampBotConfiguration bot, @NonNull @NotNull Query input) {
        return true;
    }

    @NonNull
    @Override
    public Optional<String> handle(BasecampBotConfiguration bot, @NonNull @NotNull Query input) {
        return Optional.of("Hello World");
    }
}
package io.micronaut.chatbots.basecamp

import io.micronaut.chatbots.basecamp.api.Query
import io.micronaut.chatbots.basecamp.core.BasecampBotConfiguration
import io.micronaut.chatbots.basecamp.core.BasecampHandler
import jakarta.inject.Singleton

import javax.validation.constraints.NotNull

@Singleton
class HelloWorldHandler implements BasecampHandler {

    @Override
    boolean canHandle(BasecampBotConfiguration bot, @NotNull Query input) {
        true
    }

    @Override
    Optional<String> handle(BasecampBotConfiguration bot, @NotNull Query input) {
        Optional.of("Hello World")
    }
}
package io.micronaut.chatbots.basecamp

import io.micronaut.chatbots.basecamp.api.Query
import io.micronaut.chatbots.basecamp.core.BasecampBotConfiguration
import io.micronaut.chatbots.basecamp.core.BasecampHandler
import jakarta.inject.Singleton
import java.util.*

@Singleton
class HelloWorldHandler : BasecampHandler {
    override fun canHandle(bot: BasecampBotConfiguration, input: Query): Boolean = true
    override fun handle(bot: BasecampBotConfiguration, input: Query): Optional<String> = Optional.of("Hello World")
}

5.2 Basecamp Chatbots as an AWS Lambda Function

If you want to deploy to AWS Lambda, you can use the following dependency:

implementation("io.micronaut.chatbots:micronaut-chatbots-basecamp-lambda")
<dependency>
    <groupId>io.micronaut.chatbots</groupId>
    <artifactId>micronaut-chatbots-basecamp-lambda</artifactId>
</dependency>

Use the class io.micronaut.chatbots.basecamp.lambda.Handler as the AWS Lambda function handler.

5.3 Basecamp Chatbots as a Google Cloud Function

If you want to deploy to Google Cloud Functions, you can use the following dependency:

implementation("io.micronaut.chatbots:micronaut-chatbots-basecamp-gcp-function")
<dependency>
    <groupId>io.micronaut.chatbots</groupId>
    <artifactId>micronaut-chatbots-basecamp-gcp-function</artifactId>
</dependency>

You can use the following entry point:

io.micronaut.chatbots.basecamp.googlecloud.Handler

5.4 Basecamp Chatbots as an Azure Function

If you want to deploy to an [https://docs.microsoft.com/azure](Azure) function, you can use the following dependency:

implementation("io.micronaut.chatbots:micronaut-chatbots-basecamp-azure-function")
<dependency>
    <groupId>io.micronaut.chatbots</groupId>
    <artifactId>micronaut-chatbots-basecamp-azure-function</artifactId>
</dependency>

5.5 Basecamp Chatbots Controller

If you want to configure one endpoint as the Basecamp chatbot webhook while using a runtime such as Netty or Servlet, you can include the following dependency:

implementation("io.micronaut.chatbots:micronaut-chatbots-basecamp-http")
<dependency>
    <groupId>io.micronaut.chatbots</groupId>
    <artifactId>micronaut-chatbots-basecamp-http</artifactId>
</dependency>

You can configure the path with:

🔗
Table 1. Configuration Properties for BasecampControllerConfiguration
Property Type Description

micronaut.chatbots.basecamp.endpoint.enabled

boolean

Enables the controller. Default value true.

micronaut.chatbots.basecamp.endpoint.path

java.lang.String

Path to the controller. Default value "/basecamp".

6 Repository

You can find the source code of this project in this repository: