### Run Sample Bot with application.yml Source: https://github.com/line/line-bot-sdk-java/blob/master/samples/sample-spring-boot-echo-kotlin/README.md If you have configured the `application.yml` file, you can start the web server using this Gradle command. This method assumes configuration is already set up. ```bash ../gradlew bootRun ``` -------------------------------- ### Build Project with Gradle Source: https://github.com/line/line-bot-sdk-java/blob/master/CONTRIBUTING.md Run this command to download dependencies, check the build, and compile the project. Ensure JDK 17 or later is installed. ```bash ./gradlew build ``` -------------------------------- ### Full Echo Bot Application Example Source: https://github.com/line/line-bot-sdk-java/blob/master/spring-boot/line-bot-spring-boot-client/README.md This is a complete example of an echo bot using Spring Boot and the LINE Messaging API. It demonstrates setting up the application, handling text messages, and replying with the same text. Ensure you have the necessary LINE SDK dependencies. ```java package com.example.bot.spring.echo; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.linecorp.bot.messaging.client.MessagingApiClient; import com.linecorp.bot.messaging.model.ReplyMessageRequest; import com.linecorp.bot.messaging.model.TextMessage; import com.linecorp.bot.spring.boot.handler.annotation.EventMapping; import com.linecorp.bot.spring.boot.handler.annotation.LineMessageHandler; import com.linecorp.bot.webhook.model.Event; import com.linecorp.bot.webhook.model.MessageEvent; import com.linecorp.bot.webhook.model.TextMessageContent; @SpringBootApplication @LineMessageHandler public class EchoApplication { private final Logger log = LoggerFactory.getLogger(EchoApplication.class); private final MessagingApiClient messagingApiClient; public static void main(String[] args) { SpringApplication.run(EchoApplication.class, args); } public EchoApplication(MessagingApiClient messagingApiClient) { this.messagingApiClient = messagingApiClient; } @EventMapping public void handleTextMessageEvent(MessageEvent event) { log.info("event: " + event); if (event.message() instanceof TextMessageContent) { TextMessageContent message = (TextMessageContent) event.message(); final String originalMessageText = message.text(); messagingApiClient.replyMessage(new ReplyMessageRequest( event.replyToken(), List.of(new TextMessage(originalMessageText)), false)); } } @EventMapping public void handleDefaultMessageEvent(Event event) { System.out.println("event: " + event); } } ``` -------------------------------- ### Handling FollowEvent Source: https://github.com/line/line-bot-sdk-java/blob/master/spring-boot/line-bot-spring-boot-client/README.md This example shows how to handle a FollowEvent, which is triggered when a user follows your LINE bot. It returns a simple text message indicating the follow action. Ensure the class is annotated with @LineMessageHandler. ```java @LineMessageHandler public class EchoApplication { @EventMapping public TextMessage handleFollowEvent(FollowEvent event) { System.out.println("event: " + event); return new TextMessage("Folllowed"); } } ``` -------------------------------- ### Handling Any Event Type Source: https://github.com/line/line-bot-sdk-java/blob/master/spring-boot/line-bot-spring-boot-client/README.md This example demonstrates how to create a general event handler that catches any type of Event. Use this when you need to log or perform a default action for events not specifically handled by other methods. The class must be annotated with @LineMessageHandler. ```java @LineMessageHandler public class EchoApplication { @EventMapping public void handleDefaultMessageEvent(Event event) { System.out.println("event: " + event); } } ``` -------------------------------- ### Get x-line-request-id from API Response Source: https://github.com/line/line-bot-sdk-java/blob/master/README.md Retrieve the `x-line-request-id` header from an API response by accessing the `requestId()` method of the `Result` object. This is useful for tracking requests across different LINE APIs. ```java Result apiResponse = messagingApiClient .narrowcast(retryKey, new NarrowcastRequest.Builder(messages).build()) .get(); System.out.println("x-line-request-id: " + apiResponse.requestId()); ``` -------------------------------- ### Run Sample Bot with Gradle Source: https://github.com/line/line-bot-sdk-java/blob/master/samples/sample-spring-boot-echo/README.md Execute the sample bot using Gradle. You need to provide your Channel access token and Channel secret as command-line arguments. ```bash ../gradlew bootRun -Dline.bot.channelToken=YOUR_CHANNEL_TOKEN \ -Dline.bot.channelSecret=YOUR_CHANNEL_SECRET ``` ```bash ../gradlew bootRun ``` -------------------------------- ### Run Sample Bot with Gradle Source: https://github.com/line/line-bot-sdk-java/blob/master/samples/sample-spring-boot-echo-kotlin/README.md Use this command to run the sample bot with specific channel access token and secret. Ensure these values are replaced with your actual credentials. ```bash ../gradlew bootRun -Dline.bot.channelToken=YOUR_CHANNEL_TOKEN \ -Dline.bot.channelSecret=YOUR_CHANNEL_SECRET ``` -------------------------------- ### Run Sample Spring Boot Echo Bot Source: https://github.com/line/line-bot-sdk-java/blob/master/CONTRIBUTING.md Use this Gradle command to run the sample Spring Boot echo bot project locally for testing. ```bash ./gradlew :samples:sample-spring-boot-echo:bootRun ``` -------------------------------- ### Generate JavaDoc Documentation Source: https://github.com/line/line-bot-sdk-java/blob/master/CONTRIBUTING.md Execute this Gradle command to generate JavaDoc for the project. Ensure all new or modified code includes proper JavaDoc comments. ```bash ./gradlew javadoc ``` -------------------------------- ### Spring Boot Echo Bot Implementation Source: https://github.com/line/line-bot-sdk-java/blob/master/README.md This Java code demonstrates a basic Spring Boot application for a LINE echo bot. It handles incoming text messages and replies with the same text. Ensure you have the necessary Spring Boot and LINE SDK dependencies. ```java package com.example.bot.spring.echo; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.linecorp.bot.messaging.client.MessagingApiClient; import com.linecorp.bot.messaging.model.ReplyMessageRequest; import com.linecorp.bot.messaging.model.TextMessage; import com.linecorp.bot.spring.boot.handler.annotation.EventMapping; import com.linecorp.bot.spring.boot.handler.annotation.LineMessageHandler; import com.linecorp.bot.webhook.model.Event; import com.linecorp.bot.webhook.model.MessageEvent; import com.linecorp.bot.webhook.model.TextMessageContent; @SpringBootApplication @LineMessageHandler public class EchoApplication { private final MessagingApiClient messagingApiClient; public static void main(String[] args) { SpringApplication.run(EchoApplication.class, args); } public EchoApplication(MessagingApiClient messagingApiClient) { this.messagingApiClient = messagingApiClient; } @EventMapping public void handleTextMessageEvent(MessageEvent event) { System.out.println("event: " + event); final String originalMessageText = ((TextMessageContent) event.message()).text(); messagingApiClient.replyMessage( new ReplyMessageRequest.Builder(event.replyToken(), List.of(new TextMessage(originalMessageText))) .build() ); } @EventMapping public void handleDefaultMessageEvent(Event event) { System.out.println("event: " + event); } } ``` -------------------------------- ### Gradle Dependencies for LINE Messaging API SDK Source: https://github.com/line/line-bot-sdk-java/blob/master/README.md Add these dependencies to your Gradle build file to include the LINE Messaging API SDK modules. Replace with the desired version. Some modules are not required for explicit dependency. ```kotlin implementation("com.linecorp.bot:line-bot-messaging-api-client:") implementation("com.linecorp.bot:line-bot-insight-client:") implementation("com.linecorp.bot:line-bot-manage-audience-client:") implementation("com.linecorp.bot:line-bot-module-attach-client:") implementation("com.linecorp.bot:line-bot-module-client:") implementation("com.linecorp.bot:line-bot-shop-client:") implementation("com.linecorp.bot:line-channel-access-token-client:") implementation("com.linecorp.bot:line-liff-client:") implementation("com.linecorp.bot:line-bot-webhook:") implementation("com.linecorp.bot:line-bot-parser:") // You don't need to depend on this explicitly. implementation("com.linecorp.bot:line-bot-spring-boot-webmvc:") implementation("com.linecorp.bot:line-bot-spring-boot-client:") // If you want to write spring-boot API client implementation("com.linecorp.bot:line-bot-spring-boot-handler:") // You don't need to depend on this explicitly. implementation("com.linecorp.bot:line-bot-spring-boot-web:") // You don't need to depend on this explicitly. ``` -------------------------------- ### Handle Text Message Event with Destination Source: https://github.com/line/line-bot-sdk-java/blob/master/spring-boot/line-bot-spring-boot-client/README.md Use @LineBotDestination as the first parameter in an event handler to receive the destination field. The event object should be the second parameter. This is useful for identifying the source of the message. ```java import com.linecorp.bot.spring.boot.annotation.EventMapping; import com.linecorp.bot.spring.boot.annotation.LineMessageHandler; import com.linecorp.bot.spring.boot.annotation.LineBotDestination; import com.linecorp.bot.model.event.MessageEvent; import com.linecorp.bot.model.event.message.TextMessageContent; import com.linecorp.bot.model.message.TextMessage; @LineMessageHandler public class EchoApplication { @EventMapping public TextMessage handleTextMessageEvent(@LineBotDestination String destination, MessageEvent event) { System.out.println("event: " + event); return new TextMessage(event.getMessage().getText()); } } ``` -------------------------------- ### Configuring Proxy for Messaging API Client Source: https://github.com/line/line-bot-sdk-java/blob/master/README.md This Java code snippet shows how to configure the MessagingApiClient to use an HTTP proxy server. This is useful when your application needs to connect to the LINE Messaging API through a proxy. ```java api = MessagingApiClient.builder("MY_OWN_TOKEN") .apiEndPoint(URI.create("https://api.line.me/")) .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080) )) .build(); ``` -------------------------------- ### Echo Bot with TextMessage Return Type Source: https://github.com/line/line-bot-sdk-java/blob/master/spring-boot/line-bot-spring-boot-client/README.md This snippet shows a simplified echo bot handler that directly returns a TextMessage. It's suitable when you only need to echo text messages and don't require explicit client calls for replying. Ensure the class is annotated with @LineMessageHandler. ```java @LineMessageHandler public class EchoApplication { @EventMapping public TextMessage handleTextMessageEvent(MessageEvent event, TextMessageContent content) { System.out.println("event: " + event); return new TextMessage(content.getText()); } } ``` -------------------------------- ### Retrieve x-line-accepted-request-id from Error Response Source: https://github.com/line/line-bot-sdk-java/blob/master/README.md Access specific headers, such as `x-line-accepted-request-id`, from an error response by using the `getHeader()` method on the `MessagingApiClientException` object. ```java exception.getHeader("x-line-accepted-request-id") ``` -------------------------------- ### Handle MessagingApiClientException in Java Source: https://github.com/line/line-bot-sdk-java/blob/master/README.md Catch `MessagingApiClientException` to handle errors when using the `MessagingApiClient`. This allows you to access detailed error information such as the HTTP status code, response body, and a general error message. Ensure you are catching `ExecutionException` to access the underlying `MessagingApiClientException`. ```java try { messagingApiClient.replyMessage(new ReplyMessage(replyToken, messages)); } catch (ExecutionException e) { if (e.getCause() instanceof MessagingApiClientException){ MessagingApiClientException exception=(MessagingApiClientException)e.getCause(); System.out.println("Error http status code: " + exception.getCode()); System.out.println("Error response: " + exception.getDetails()); System.out.println("Error message: " + exception.getMessage()); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.