# [練習範例]Spring boot line bot practice ## 先做好事前準備 ### ngrok * 使用ngrok建立一個臨時的https網址(接收Line Bot傳送過來訊息的網頁須走https協定)。 * 註冊並+、下載[ngrok](https://ngrok.com/download),並解壓縮。 * 打開檔案,並輸入指令 ``` ngrok http 8080 ```  * 就會看到以下畫面,代表連線成功,我們需要的就是 Forwarding這裡的網址  ### 註冊line business account * [註冊line business account](https://developers.line.biz/en/) * 按照步驟建立channel * 記下Basic settings中的Channel secret ,以及Message API最底下的Channel access token,等等開發會用到 * 把ngrok中拿到的網址後面加上/callback並update Message API底下的Webhook URL,也把Use webhook打開 * 再到Auto-reply messages中,把自動回應訊息disable,以及啟動Webhook  ## echo pratice 好了,要開始弄髒手了喔! * 到[Spring Initializr](https://start.spring.io/)建立新的專案,記得spring boot的版本用2.7.5(我本來用3,結果搞了我2天才發現原來是版本問題導致跑不起來) * import常用的Dependencies並下載、解壓縮  * 加入line bot 的Dependency,加上上面的import常用的Dependencies並下載,pom檔主要依賴的內容如下 ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- line bot --> <dependency> <groupId>com.linecorp.bot</groupId> <artifactId>line-bot-spring-boot</artifactId> <version>5.0.3</version> </dependency> ``` * 首先application.properties,把剛剛拿到的Channel secret及Channel access token貼過來。  * 建立一個messageHandler ``` import com.linecorp.bot.model.event.Event; import com.linecorp.bot.model.event.MessageEvent; import com.linecorp.bot.model.event.message.TextMessageContent; import com.linecorp.bot.model.message.Message; import com.linecorp.bot.model.message.TextMessage; import com.linecorp.bot.spring.boot.annotation.EventMapping; import com.linecorp.bot.spring.boot.annotation.LineMessageHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @LineMessageHandler public class LinebotHandler { private final Logger log = LoggerFactory.getLogger(LinebotHandler.class); @EventMapping public Message handleTextMessageEvent(MessageEvent<TextMessageContent> event) { log.info("event: " + event); final String originalMessageText = event.getMessage().getText(); return new TextMessage(originalMessageText); } @EventMapping public void handleDefaultMessageEvent(Event event) { System.out.println("event: " + event); } } ```  * 啟動後,確認line bot 的api有作用  * 回到line developers的channel中,Message API 底下的Webhook URL,點擊Vertify,如果彈出success的視窗,就代表成功囉!  * 最後你就可以使用Message API中的QR code進行測試。  ### 後面比較複雜的功能就自己看囉!我就不寫了,基本上就是使用[官方提供在github](https://github.com/line/line-bot-sdk-java)上的sample-spring-boot-kitchensink這個專案修修改改就可以了! 本專案的[github](https://github.com/mister33221/spring-boot-linebot-practice.git),其中包含一些進階的使用以及連結mongoDB --------- ###### tags: `java` `spring boot` `` * 參考資料 [Line Bot](https://sites.google.com/im.fju.edu.tw/web/java/line-bot?pli=1) [三.Web Service的建立及LineBot webhook設定](https://www.21cs.tw/Nurse/showLiangArticle.xhtml?liangArticleId=501) [LineBot接收及回應訊息](https://www.21cs.tw/Nurse/showLiangArticle.xhtml?liangArticleId=503) [line official](https://developers.line.biz/zh-hant/) [line message developer API](https://developers.line.biz/en/docs/messaging-api/overview/) [line official github example](https://github.com/line/line-bot-sdk-java)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up