camioljoyce
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Camiol用JAVA + Heroku CLI 建立自己的Line Robot **Step1:** 到[LINE Developers](https://developers.line.biz/zh-hant/)申請Line Robot,選擇右上角登入自己的line帳號 ![](https://i.imgur.com/ymsf8Ym.jpg) 幫自己的Developer取名,填寫Email,按下同意後,建立Developer account ![](https://i.imgur.com/uixhILk.jpg) 接著按下按鈕,開始建立Provider ![](https://i.imgur.com/g64d3TQ.jpg) 幫自己的Provider取名,按下Create ![](https://i.imgur.com/YrdGgyW.jpg) 接著要建立Channel,今天我們要建立的是可以發送和接收訊息的Messaging API Channel ![](https://i.imgur.com/O9rSAfC.jpg) 設定國籍,和我們機器人的大頭貼 ![](https://i.imgur.com/fBsxoe4.jpg) 設定機器人的名稱,和描述,還有選擇分類和子分類 ![](https://i.imgur.com/6uESfpT.jpg) 把兩個同意打勾後,按下Create ![](https://i.imgur.com/R392UTy.jpg) 會跳出確認視窗,按下OK ![](https://i.imgur.com/VkSgSLQ.jpg) 然後按下同意 ![](https://i.imgur.com/FIkgYSx.jpg) 可以在Basic settings看到我們機器人的基本資料,Channel ID另外記下來,之後會用到 ![](https://i.imgur.com/vfBsBYE.jpg) 往下拉,將Channel secret和 Your user ID也記下來 ![](https://i.imgur.com/FWBNa6c.jpg) 選擇Messaging API,Bot basic ID就是機器人的帳號,可以把機器人加入好友,或是用QR code也可以 ![](https://i.imgur.com/GIlCgKl.jpg) 往下拉,可以看到API的權限,可以回覆訊息和發送訊息 ![](https://i.imgur.com/gcbziij.jpg) 拉到最下面有Channel access token(long-lived),按下Issue產生token ![](https://i.imgur.com/hFQbNUz.jpg) 把產生出來的token另外抄下來,之後會用到 ![](https://i.imgur.com/ZTZDf4I.jpg) 這樣就建立好一個基本的Line Robot Messaging API了! --- **Step2:** **註冊與設定Heroku平台** 我們預計要把Line Bot部屬在Heroku平台,因為在Webhook URL中須設定Line Robot的IP位置,其中Line官方的規範中,Line Robot須是走Https協定且不能是明碼,所以我們使用Heroku免費的雲端平台進行測試,使用 Heroku網站來充當webhook Heroku是一個PaaS(Platform as a Service,平台即服務)。使用 PaaS,我們只要把寫好的專案部屬到平台上,網站就能開始運作了。 --- 到[Heroku](https://www.heroku.com/)去申請帳號 按下右上角的Sign up ![](https://i.imgur.com/0cf4dp6.jpg) 填完基本資料之後,「職業 (Role)」可以選擇學生 (student) 或「業餘愛好者 (hobbyist) 」,語言選擇JAVA,然後按下Create free account ![](https://i.imgur.com/RUuWhr4.jpg) 然後他會寄認證信到你填的信箱,再去信箱收信認證 ![](https://i.imgur.com/IHxTaRO.jpg) 認證完後設定密碼 ![](https://i.imgur.com/T5zhcdQ.jpg) 設定完後按下按鈕 ![](https://i.imgur.com/voEx4L9.jpg) 按下Accept同意條款 ![](https://i.imgur.com/eYuJFPI.jpg) 按下Create new app ![](https://i.imgur.com/p7wOiHB.jpg) 替你的App取個名字,名稱只能用英文、數字和連字符號,如果名稱欄位底下出現紅字,代表名稱包含不被接受的字元或者該名稱已經被佔用了。然後按下Create app ![](https://i.imgur.com/iOQwhcX.jpg) 即可在Heroku平台上建立好執行App的環境。你的雲端程式路徑將是: ``` https://[剛取的app name].herokuapp.com/ ``` --- **然後下載、安裝與執行Heroku CLI** 在dashboard這邊下載安裝Heroku CLI ![](https://i.imgur.com/A52DxwV.jpg) 選擇適合自己作業系統的執行檔下載,我是Windows-64bit ![](https://i.imgur.com/aiIWR24.jpg) 下載下來後,一直按next即可 ![](https://i.imgur.com/kC2RpHd.jpg) 安裝完畢後,開啟Windows「命令提示字元」或者Mac的終端機,輸入"heroku login"命令,測試登入你的Heroku平台: ![](https://i.imgur.com/uLZVNYT.jpg) 按下enter,開啟瀏覽器,登入Heroku,然後按下Log In ![](https://i.imgur.com/cN7V1y2.jpg) 可以看到Log In成功 ![](https://i.imgur.com/LWrryGg.jpg) 回來cmd這邊,可以看到已成功登入Heroku ![](https://i.imgur.com/1chqf33.jpg) --- **Step3:用Spring boot來寫JAVA Line Robot** 可參考之前寫的這篇[建立一個SpringBoot + Spring + JPA 的Web專案](https://hackmd.io/i3T9xRyQR0OOVczCQmtkZQ)來建立一個基礎的Spring boot專案。 ![](https://i.imgur.com/pw9WEtG.jpg) ![](https://i.imgur.com/cu6u0I5.jpg) 建立完後,去[Maven Depository](https://mvnrepository.com/search?q=com.linecorp.bot)搜尋com.linecorp.bot,下載Line Bot Spring Boot的4.3版(目前最新版4.9版檔案有問題沒辦法正常使用),將它加到POM檔裡面 ![](https://i.imgur.com/4TU4xLx.jpg) ``` <!-- https://mvnrepository.com/artifact/com.linecorp.bot/line-bot-spring-boot --> <dependency> <groupId>com.linecorp.bot</groupId> <artifactId>line-bot-spring-boot</artifactId> <version>4.3.0</version> </dependency> ``` 然後在application.properties這邊設定line bot相關設定檔: ![](https://i.imgur.com/vnRjzGW.jpg) token的部分填入之前申請時的token 然後建立一個RestController來測試是否專案正常啟動 ![](https://i.imgur.com/bGI8Tvn.jpg) ```java= package camiol.com.linebot.controller; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/robot") @RestController public class RobotController { @GetMapping("/test") public ResponseEntity<String> test() { return new ResponseEntity<String>("Hello JAVA", HttpStatus.OK); } } ``` 然後啟動application後,在網址列輸入http://localhost:8080/robot/test, 可以看到成功啟動的測試畫面。 ![](https://i.imgur.com/XhhiUYj.jpg) 然後新增Handler來管理收到的訊息,寫個基本的echo message測試 ![](https://i.imgur.com/67o5zTR.jpg) ```java= package camiol.com.linebot.handler; 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.TextMessage; import com.linecorp.bot.spring.boot.annotation.EventMapping; import com.linecorp.bot.spring.boot.annotation.LineMessageHandler; @LineMessageHandler public class Handler { @EventMapping public TextMessage handleTextMessageEvent(MessageEvent<TextMessageContent> event) { // 這邊做的就是簡單的 echo System.out.println("event: " + event); return new TextMessage(event.getMessage().getText()); } @EventMapping public void handleDefaultMessageEvent(Event event) { // 就是加入聊天室, 離開聊天室, 還有一些有的沒的事件 System.out.println("event: " + event); } } ``` --- **Step4: 將寫好的程式佈署到Heroku平台並測試我們剛寫的echo bot** 回到LINE機器人的頻道設定頁面,在Messaging API這邊設定Webhook settings,按下Edit ![](https://i.imgur.com/TFMC2J0.jpg) 填入佈署在heroku平台的雲端程式路徑, https://[剛取的app name].herokuapp.com/(填入你在application.properties填的handler路徑) 以我的專案來做舉例: ```= https://camiol-line-bot.herokuapp.com/callback ``` 輸入完後按下update ![](https://i.imgur.com/C8L0lG4.jpg) 然後開啟Use webhook選項 ![](https://i.imgur.com/RkpjhdS.jpg) 再來要將程式佈署到Heroku上面,利用之前申請過的Heroku CLI,打開cmd,按照之前寫的登入方法操作,成功登入後,執行cd命令切換到line bot程式的根路徑: ![](https://i.imgur.com/UZwSTxn.jpg) 在cmd上面輸入 ```console git init ``` 設定我們本機的heroku git的repository位置 ![](https://i.imgur.com/FfxqIGU.jpg) 再來將這個git和我們之前在heroku上面設定app連接起來,輸入 ```console heroku git:remote -a 'Heroku應用程式名稱' ``` ,用來設定遠端的git repository位置 ![](https://i.imgur.com/9QkiugM.jpg) 輸入 ```console git add . ``` 把當前路徑包含子目錄的所有檔案都加進去git ![](https://i.imgur.com/xj4eVOx.jpg) 輸入 ```console git commit -am "想記錄的話" ``` 將檔案commit到本機的repository ![](https://i.imgur.com/x3EKrBJ.jpg) 輸入 ```console git push heroku master ``` 將檔案push到Heroku平台上面並安裝 ![](https://i.imgur.com/HGnVhn4.jpg) ![](https://i.imgur.com/GFXUu5s.jpg) 輸入 ```console heroku open ``` 就會自動打開瀏覽器,開啟Heroku應用程式 ![](https://i.imgur.com/YQ9QQTO.jpg) ![](https://i.imgur.com/zSrwAp0.jpg) 在我們的網址後面輸入我們之前設定測試的網址 ``` /robot/test ``` 就可以看到Hello JAVA了,代表程式正常運作中 ![](https://i.imgur.com/ZYyJQZ5.jpg) 在cmd輸入 ```console heroku logs --tail ``` 就可以看到專案的log ![](https://i.imgur.com/E7kZIyk.jpg) 再來我們回到LINE機器人的頻道設定頁面,在Messaging API這邊的Webhook settings的Webhook URL,按下Verify,正常運作的話會回應Success ![](https://i.imgur.com/7qejlcx.jpg) 最後回到Line上面跟機器人對話,它就會回覆跟你一樣的話,完成echo bot test。 ![](https://i.imgur.com/ofgaeyA.jpg) 預設每次LINE機器人都會先回覆一則罐頭訊息,若要取消它,請將LINE頻道設定頁面的Auto-reply messages(自動回覆訊息)欄位設定成Disabled(取消): 在這邊按下Edit ![](https://i.imgur.com/ghe9FTj.jpg) 進到機器人設定畫面,這邊可以設定加入好友的歡迎訊息,然後我們把自動回應訊息給停用 ![](https://i.imgur.com/RJxkGmw.jpg) 再次發送訊息,就可以看到自動回覆訊息取消了。 ![](https://i.imgur.com/fMa92wX.jpg) 這樣基本的line echo bot就完成了! ###### tags: `Spring boot` `Java` `Line bot`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    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

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully