On this page
Chatbots
This module eases the creation of Telegram Bots, Basecamp Bots with the Micronaut Framework.
For this project, you can find a list of releases (with release notes) here:
You can develop a Telegram Bot quickly with Micronaut Chatbots.
Create a Telegram Bot by talking with the @BotFather.
|
Note
|
To see what actions the BotFather can perform for you, send /help in the chat.
|
First send the command /newbot to the BotFather and follow the instructions to create a new bot.
At the end of this process, you will receive a token to access the HTTP API.
micronaut.chatbots.telegram.bots.mnexample.token=xxxyyyzzz
micronaut.chatbots.telegram.bots.mnexample.at-username=@MicronautExampleBotmicronaut.chabots.telegram.bots.*.token matches the value specified as secret_token while setting the 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}"-
httpApiTokenYou obtain this value via the @BotFather -
webhookUrlis the endpoint of your Micronaut application. -
{secretToken}- A secret token, Telegram sends in a headerX-Telegram-Bot-Api-Secret-Tokenin every webhook request.
To develop your bot, create beans of type TelegramHandler.
@Singleton
class HelloWorldHandler implements TelegramHandler<SendMessage> {
private final SpaceParser<Update, Chat> spaceParser;
HelloWorldHandler(SpaceParser<Update, Chat> spaceParser) {
this.spaceParser = spaceParser;
}
@Override
public boolean canHandle(@Nullable TelegramBotConfiguration bot, @NotNull Update input) {
return input.getMessage().getText().contains("hello");
}
@Override
public Optional<SendMessage> handle(@Nullable TelegramBotConfiguration bot, @NotNull Update input) {
return SendMessageUtils.compose(spaceParser, input, "Hello World");
}
}To respond to Telegram Bot commands, with static content, you can extend from CommandHandler.
For example to respond to an /about command with a static message, you create a CommandHandler bean:
@Singleton
class AboutCommandHandler extends CommandHandler {
private static final String COMMAND_ABOUT = "/about";
AboutCommandHandler(
TelegramSlashCommandParser slashCommandParser,
TextResourceLoader textResourceLoader,
SpaceParser<Update, Chat> spaceParser
) {
super(slashCommandParser, textResourceLoader, spaceParser);
}
@Override
@NonNull
public String getCommand() {
return COMMAND_ABOUT;
}
}And then create some content with a matching name in the botcommands directory:
Bot developed with 💙 using [Micronaut](https://micronaut.io)The botcommands directory may be configured via the micronaut.chatbots.folder configuration property.
Chatbot handlers have order, and the first handler to support a command is the one used.
To change the order of handlers, you can override the getOrder method in your handler.
If you want to deploy to AWS Lambda, you can use the following dependency:
implementation("io.micronaut.chatbots:micronaut-chatbots-telegram-lambda")Use the class io.micronaut.chatbots.telegram.lambda.Handler
as the AWS Lambda function handler.
If you want to deploy to Google Cloud Functions, you can use the following dependency:
implementation("io.micronaut.chatbots:micronaut-chatbots-telegram-gcp-function")You can use the following entry point:
Use the class io.micronaut.chatbots.telegram.googlecloud.Handler
as the Http Function entrypoint.
If you want to deploy to an Azure function, you can use the following dependency:
implementation("io.micronaut.chatbots:micronaut-chatbots-telegram-azure-function")This adds an AzureFunction with the name TelegramTrigger via the class io.micronaut.chatbots.telegram.azurefunction.Handler
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:micronaut-mironaut-chatbots-telegram-http")You can configure the path with:
You can develop Basecamp Chatbots quickly with Micronaut Chatbots.
You have to set the webhook via the Basecamp API.
curl -s \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"service_name":"${BOTNAME}","command_url":"${YOUR_HTTP_TRIGGER_URL}"}' \
https://3.basecampapi.com/${APP_ID}/buckets/${BUCKET_ID}/chats/${CHAT_ID}/integrations.json-
BOTNAMEis the name of your bot -
ACCESS_TOKENis your Oauth2 Basecamp access token -
YOUR_HTTP_TRIGGER_URLis the https address of your URL of your controller or function -
APP_ID,BUCKET_IDandCHAT_IDare the IDs of your Basecamp application, bucket and chat respectively
To develop your bot, create beans of type BasecampHandler.
@Singleton
class HelloWorldHandler implements BasecampHandler {
@Override
public boolean canHandle(BasecampBotConfiguration bot, @NonNull @NotNull Query input) {
return input.getCommand().contains("hello");
}
@NonNull
@Override
public Optional<String> handle(BasecampBotConfiguration bot, @NonNull @NotNull Query input) {
return Optional.of("Hello World");
}
}To respond to specific messages with static content, you extend from BasecampHandler and utilize the TextResourceLoader class to load the content.
For example to respond to an /about command with a static message, you create a bean:
@Singleton
class AboutCommandHandler implements BasecampHandler {
public static final String ABOUT = "about";
private final TextResourceLoader textResourceLoader;
AboutCommandHandler(TextResourceLoader textResourceLoader) {
this.textResourceLoader = textResourceLoader;
}
@Override
public boolean canHandle(@Nullable BasecampBotConfiguration bot, @NonNull @NotNull Query input) {
return input.getCommand().equalsIgnoreCase("/" + ABOUT);
}
@Override
public @NonNull Optional<String> handle(@Nullable BasecampBotConfiguration bot, @NonNull @NotNull Query input) {
return textResourceLoader
.composeCommandResponse(ABOUT)
.map(CommandResponse::text);
}
}And then create some content with a matching name in the botcommands directory:
Bot developed with 💙 using [Micronaut](https://micronaut.io)The botcommands directory may be configured via the micronaut.chatbots.folder configuration property.
Chatbot handlers have order, and the first handler to support a command is the one used.
To change the order of handlers, you can override the getOrder method in your handler.
If you want to deploy to AWS Lambda, you can use the following dependency:
implementation("io.micronaut.chatbots:micronaut-chatbots-basecamp-lambda")Use the class io.micronaut.chatbots.basecamp.lambda.Handler
as the AWS Lambda function handler.
If you want to deploy to Google Cloud Functions, you can use the following dependency:
implementation("io.micronaut.chatbots:micronaut-chatbots-basecamp-gcp-function")You can use the following entry point:
Use the class: io.micronaut.chatbots.basecamp.googlecloud.Handler as the Http Function entrypoint.
If you want to deploy to an Azure function, you can use the following dependency:
implementation("io.micronaut.chatbots:micronaut-chatbots-basecamp-azure-function")This adds an AzureFunction with the name BasecampTrigger via the class io.micronaut.chatbots.basecamp.azurefunction.Handler
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")You can configure the path with:
This section documents breaking changes between Micronaut Chatbots versions:
Micronaut Chatbots 2.0.0
Deprecations
-
The private class field
io.micronaut.chatbots.google.api.Space.typedeprecated previously has been removed. As a consequence the corresponding accessor/mutator methodsgetType()andsetType(Type)have also been removed, even though they were not deprecated explicitly. Use the accessor/mutator methods forsingleUserBotDmorspaceType(developer preview) instead.
You can find the source code of this project in this repository: