莊英勤
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # [<i class="fa fa-book"></i>](/@NCHUIT/1101-line-bot)往生堂 LINE BOT 服務 壹 ## 序 西元2021年11月2日下午 04:00,「赤團開時」卡池開啟,覺得還好的旅行者原本不以為然,但是看著B站有關胡桃的視頻,實在可愛動人,再加上身邊同儕的曬卡壓力,錢包最終還是扛不住了,是什麼魔法讓原石突然變多了!? 命運使我和這位古靈精怪的少女有了交集——你不知道嗎,往生堂第七十七代堂主就是胡桃我啦! ![](https://i.imgur.com/Zys7seF.jpg) 某天,胡桃看到酒館「天使的饋贈」推出 LINE BOT 做宣傳以及提供服務。 <img src="https://i.imgur.com/YwlRHkt.jpg" width="40%"> 霎時,一個小宇宙瞬間在胡桃的腦中爆發開來—— <strong>「在木牌後面寫廣告詞是很難做到業務拓展的,往生堂的服務也該與時俱進推出 LINE BOT 才不會被時代淘汰!!」</strong> 這個聲音在胡桃小腦袋瓜裡來回反彈,久久不能散去。 「啊!剛剛把胡桃我抽出來的旅行者,不就是工程師嗎?真的是太剛好了!!」胡桃如此大聲說道。 我只想知道,胡桃什麼時候多了新語音了!? 「旅行者,幫我個忙,往生堂最近想搞 LINE BOT 做宣傳,你不是工程師嗎?這對你來說應該小菜一疊,就靠你了!!」 我還沒來的及反應,她就已經把工作指派給我了。 好吧,九百九十新台幣硬是給他保底出來了,抽她出來就是為了疼她的,於是我就開始研究 LINE BOT。 --- ## 關於 LINE BOT 為什麼學 LINE BOT?LINE BOT 的優勢為何? 1. 相比做一個 APP,LINE BOT 的開發成本較低,如果是做一些簡單的小生意,會是不錯的選擇。 2. LINE BOT 較不會占用使用者手機的空間。試想一種情況,有五家店提供會員服務但都要求消費者下載他們的 APP,導致手機裡會有五個不同店家的APP;而另一種情況是他們只要求消費者加他們的 LINE BOT 好友,手機裡只要有 LINE 就可以了。 3. 在台灣,LINE相當普及,幾乎每個人都有一個 LINE 帳號。若店家要提供商業用服務,比起要使用者下載一個 APP,新增 LINE BOT 好友對消費者會是一個比較方便、且成本較低的選擇。 隨著各種不同類型的聊天機器人一隻接一隻的誕生,AI 產業的指標公司 Google,也在2017年底發表了用來評估聊天機器人效能的平台 Chatbase,用來幫助了解客戶的使用習慣,分析聊天機器人無法正確處理的個案比例等等。發展至今,這個分析平台已經可以適用於各種語音以及各式信息平台。可以說,各行各業都在探討藉由聊天機器人發展新的商業模式以及新的商業契機。聊天機器人的工作,從天氣、查找餐廳、購買衣服、預訂票券,幾乎無所不包,就連心理諮商,都有專業的團隊利用本身的知識,打造出以認知行為為手段的聊天機器人 Woebot,希望藉著無所不在的機器人,作為第一道防護網,治療並幫助憂鬱症患者。 --- ## LINE BOT SDK LINE聊天機器人的運作過程中牽涉三個角色,<strong>「使用者」</strong>、<strong>「LINE平台」</strong>以及<strong>「聊天機器人」</strong>。 ![](https://i.imgur.com/hI4E7s4.png) - <strong>使用者:</strong>透過LINE這個通訊軟體進行各種操作的使用者,聊天機器人的聊天對象。當使用LINE時,各種操作會形成一封一封「通知」,送過去「LINE平台」。也可以從「LINE平台」接收通知。 - <strong>LINE平台:</strong>LINE官方提供的通知中繼站。負責接收「使用者」以及「聊天機器人」送來的「通知」,並轉送到指定的地方。 - <strong>聊天機器人:</strong>程式設計師創造出來的虛擬使用者,當收到「通知」時,會根據設計師寫下的程式碼來處理每一封「通知」,產生適當的回應,再以「通知」的形式送到「LINE平台」。 ### 註冊 首先進到[LINE Developers](https://developers.line.biz/zh-hant/)開始註冊我們的機器人吧!進來之後會看到以下介面! ![](https://i.imgur.com/8qrRRJo.jpg) <br/> <strong>陰陽有序,命運無常。 以此蝶火,為汝指路。</strong> <br/> :fire: <strong>Step 1:</strong> 點擊畫面右上角的「Log in」按鈕,做帳號登入的動作。 ![](https://i.imgur.com/f03GWEA.jpg)<br/> :fire: <strong>Step 2:</strong> 若完成登入,應該會進到以下頁面。點擊畫面中的「Create」按鈕。 ![](https://i.imgur.com/rjqwXRY.png)<br/> :fire: <strong>Step 3:</strong> 輸入合法 Provider Name,然後點選「Create」。 ![](https://i.imgur.com/LqChhYa.png)<br/> :fire: <strong>Step 4:</strong> 到此頁面後,點選 "Creat a Messaging API channel",代表我們想創造一個聊天機器人。 ![](https://i.imgur.com/K99JhKU.png)<br/> :fire: <strong>Step 5:</strong> 接著會來到以下頁面,"Channel icon" 可設定 LINE BOT 頭像,而 "Channel name" 可設定 LINE BOT 名稱。 ![](https://i.imgur.com/gMVk6B4.png)<br/> :fire: <strong>Step 6:</strong> 同樣的頁面,簡單填一下 "Channel description"、"Category" 以及 "Subcategory"。 ![](https://i.imgur.com/71YVW1l.png)<br/> :fire: <strong>Step 7:</strong> 同樣的頁面,把最底下的兩個勾勾起來,按下「Create」按鈕。 ![](https://i.imgur.com/BI6PRWf.png)<br/> :fire: <strong>Step last: 最後看到以下頁面代表我的 LINE BOT 於此成功誕生了!!</strong> ![](https://i.imgur.com/RVjcCud.png) ### 設定簡易 Auto-reply message & Greeting messages 🌸 第一步:點擊 "Messaging API" 欄位。 ![](https://i.imgur.com/wpYiUTV.png) 🌸 第二步:找到 "LINE Official Account features" 的部分,點擊 "Greeting messages" 或 "Auto-reply messages" 的 超連結字樣 "Edit" (點哪個都會進到同一頁)。 ![](https://i.imgur.com/dhQnsFv.png) ![](https://i.imgur.com/ij1s44B.png) 🌸 第三步: ### Line Bot 可加入群組設定 :musical_note: <strong>大丘丘病了:</strong> 點擊 "Messaging API" 欄位。 ![](https://i.imgur.com/wpYiUTV.png) :musical_note: <strong>二丘丘翹:</strong> 找到 "LINE Official Account features" 的部分,並點擊 "Allow bot to join group chats" 欄位的超連結字樣 "Edit"。 ![](https://i.imgur.com/wDFsbgh.png) :musical_note: <strong>三丘丘採藥:</strong> 跳轉到新頁面後,找到 Toggle features 的部分,將 "Group and multi-person chats" 下面那個選項選起來。 ![](https://i.imgur.com/8KrG0tX.png) :musical_note: <strong>四丘丘嗷:</strong> --- ## Python ### python環境架設 先進到 [python](https://www.python.org/) 官網看看吧! <strong>蝶火燎原!</strong> :Butterfly: <strong>Step 1:</strong> 到 python 官網點擊 Downloads 欄位底下的 Python 3.XX.X 按鈕。 ![](https://i.imgur.com/AVdqcWt.png) :Butterfly: <strong>Step 2:</strong> 打開剛下載的執行檔以安裝 Python。畫面如下,先將底下的勾選方塊勾起來(重要),接著再按上面的 "Install Now"。 ![](https://i.imgur.com/Knm77FX.png) :Butterfly: <strong>Step 3:</strong> 等待安裝。 ![](https://i.imgur.com/DU9n29n.png) :Butterfly: <strong>Step 4:</strong> 安裝完成後,點擊 "Close" 就好了。 ![](https://i.imgur.com/xLFvbaz.png) :Butterfly: <strong>Step 5:</strong> 接著打開店腦中的 VS Code。點擊左側 "Extensions" 按鈕,並在搜尋欄位輸入 "python"。 ![](https://i.imgur.com/Dqt9TVT.png) :Butterfly: <strong>Step 6:</strong> 點擊 "Python" 後,點擊按鈕 " Install "。 ![](https://i.imgur.com/K2mDdZZ.png) :Butterfly: <strong>Step 7:</strong> 點擊 File 欄位底下的 "Open Folder..."。 ![](https://i.imgur.com/Ib5YwlK.png) :Butterfly: <strong>Step 8:</strong> 這邊隨便開啟一個空資料夾。 ![](https://i.imgur.com/nHogLC9.png) :Butterfly: <strong>Step 9:</strong> 點擊右鍵,再來點擊 "New File"。 ![](https://i.imgur.com/gVpSGVP.png) :Butterfly: <strong>Step 10:</strong> 輸入檔名 "`HelloWorld.py`"。 ![](https://i.imgur.com/AgwSccZ.png) :Butterfly: <strong>Step 11:</strong> 將底下程式碼,輸入進剛剛創建的檔案中。 ```python= print("Hello, World!") ``` :Butterfly: <strong>Step last:</strong> 點擊右上角的三角形,跑剛剛寫好的程式,如果結果跟底下一樣,就恭喜安裝成功搂!! ![](https://i.imgur.com/chsksh7.png) <strong>「恭喜各位搂🌸,送各位環境建置成功的冒險者一日往生體驗卷一張!」</strong> ![](https://i.imgur.com/5GyDDtq.gif) ### virtualenv虛擬環境 `python`有個很好用的套件管理程式`pip`,但只要使用`pip`安裝後就會直接裝到電腦內,如果有其他的程式需要其他版的套件時很容易出錯。要怎麼解決呢? 有個工具叫`virtualenv`,就是用來隔離不同環境使用的,甚至可以指定這個環境要用的`python`版本 先開啟`cmd`或`powershell`,輸入 ```= pip install virtualenv ``` #### 建立虛擬環境 接著在我們的專案目錄裡輸入 ```= virtualenv venv ``` 即可建立虛擬環境 #### 啟動環境 先用系統管理員權限開啟powershell ![](https://i.imgur.com/vfZ84yB.png) 並輸入以下指令 ```shell= Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser ``` 按A ![](https://i.imgur.com/tlMG8Ai.png) 回到vs code ```shell= .\venv\Scripts\activate ``` 如果命令提示符前方出現(venv),就代表啟動成功了 ![](https://i.imgur.com/cfwCu4Q.png) **小提示**:VS Code下可以查看左下角是否有`Python 3.x.x 64-bit('venv':venv)`,如果有可以直接新增一個終端機,即可直接開啟環境 ![](https://i.imgur.com/xlQkqaC.png) ## Flask ### 什麼是Flask * 輕量級網站框架,只實作核心功能(Routing) * 其他功能需要用延伸套件擴增 * 透過Werkzeug伺服器,跟python核心的WSGI協定溝通 * 並有模板引擎`Jinja2` ### 安裝 **先確認目前有在`(venv)`下** ```shell= pip install flask ``` ### Hello World! ```python= # 導入flask類 from flask import Flask # Flask類接收一個參數__name__ app = Flask(__name__) # 路由解析,通過用戶訪問的路徑,匹配相應的函數 @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run() ``` ### 路由、變數 ```python= from flask import Flask app = Flask(__name__) @app.route("/index") def hello(): return '你好' # 通過訪問路徑,獲得用戶字符串參數 @app.route("/user/<name>") def welcome(name): return "你好, %s"%name # 通過訪問路徑,獲得用戶整形參數 @app.route("/user/<int:id>") def welcome2(id): return "你好,%d號的會員"%id # 路由路徑不能重複,用戶通貨唯一路徑訪問特定函數 if __name__ == '__main__': app.run() ``` ### render_template 檔案 `app.py` ```python= from flask import Flask, render_template app = Flask(__name__) @app.route("/") def index(): return render_template("index.html") if __name__ == '__main__': app.run() ``` templates 資料夾中的 `index.html` ```html= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 歡迎光臨。 </body> </html> ``` ### 傳參數 檔案 `app.py` ```python= from flask import Flask, render_template import datetime app = Flask(__name__) @app.route("/") def index(): time = datetime.date.today() # 普通變量 name = ["Fe","Luder","Suisei"] # 列表類型 task = {"任務":"打掃衛生","時間":"3小時"} #字典類型 return render_template("index3.html", var = time, list = name, task = task) if __name__ == '__main__': app.run() ``` templates 資料夾中的 `index.html` ```html= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 今天是{{ var }},歡迎光臨。<br/> 今天值班的有:<br/> {% for data in list %} <!--用大括號和百分號刮起來的式控制結構,還有if--> <li> {{ data }} </li> {% endfor %} 任務:<br/> <!--了解一下如何在頁面打印表格,以及如何迭代--> <table border="1"> {% for key,value in task.items() %} <!--[(key,value),(key,value),(key,value)]--> <tr> <td>{{key}}</td> <td>{{value}}</td> </tr> {% endfor %} </table> </body> </html> ``` ### 表單提交 檔案 `app.py` ```python= from flask import Flask, render_template, request import datetime app = Flask(__name__) #表單提交 @app.route('/test/register') def register(): return render_template("test/register.html") #接收表單提交的路由,需要指定methods為POST @app.route('/result',methods=['POST']) def result(): if request.method == 'POST': result = request.form return render_template("test/result.html",result=result) if __name__ == '__main__': app.run() ``` templates 資料夾中的 test/register.html ```html= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="{{ url_for('result') }}" method="post"> <!--http://localhost:5000/result"--> <p>姓名:<input type="text" name="姓名"></p> <p>年齡:<input type="text" name="年齡"></p> <p>性別:<input type="text" name="性別"></p> <p>地址:<input type="text" name="地址"></p> <p><input type="submit" value="提交"></p> </form> </body> </html> ``` templates 資料夾中的 test/result.html ```html= <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> {% for key,value in result.items() %} <!--[(key,value),(key,value),(key,value)]--> <tr> <th>{{key}}</th> <td>{{value}}</td> </tr> {% endfor %} </table> </body> </html> ```

    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