|
| 1 | +# line-bot |
| 2 | + |
| 3 | +[](https://github.com/custom-components/hacs) |
| 4 | + |
| 5 | +[English](README.md) | [繁體中文](README-zh.md) |
| 6 | + |
| 7 | +Home Assistant 的 LINE bot 整合套件 |
| 8 | + |
| 9 | +_本專案修改自 [yun-s-oh/Homeassistant](https://github.com/yun-s-oh/Homeassistant)_ |
| 10 | + |
| 11 | +## ⚠ 注意 |
| 12 | + |
| 13 | +本套件僅適用於 LINE bot |
| 14 | + |
| 15 | +若要整合 LIEN Notify 請改用上列 yun-s-oh 的套件 |
| 16 | + |
| 17 | +## 安裝 |
| 18 | + |
| 19 | +### 透過 HACS |
| 20 | + |
| 21 | +建議使用 [HACS](https://hacs.xyz/) 安裝 `line-bot`,步驟如下: |
| 22 | + |
| 23 | +HACS > Integrations > 右上三點選單鈕 > Custom repositories > URL: `osk2/line-bot` > Category: Integration |
| 24 | + |
| 25 | +### 手動安裝 |
| 26 | + |
| 27 | +將 `custom_components/line_bot` 複製至 `custom_components` |
| 28 | + |
| 29 | +## 設定方式 |
| 30 | + |
| 31 | +將下列設定複製至 `configuration.yaml` |
| 32 | + |
| 33 | +```yaml |
| 34 | +notify: |
| 35 | + - name: line_bot |
| 36 | + platform: line_bot |
| 37 | + client_id: 'CLIENT_ID' |
| 38 | + access_token: 'CHANNEL_ACCESS_TOKEN' |
| 39 | +``` |
| 40 | +
|
| 41 | +請閱讀 [額外資訊](#額外資訊) 一節以了解 `client_id` 及 `access_token` 的取得方式 |
| 42 | + |
| 43 | +### 透過介面建立設定檔 |
| 44 | + |
| 45 | +設定檔為 `client_id` 及 `access_token` 的組合 |
| 46 | + |
| 47 | +除了可透過上一章節的步驟建立通知服務外,也可透過介面來建立以簡化流程 |
| 48 | + |
| 49 | +1. 設定 > 整合 > 新增整合 > LINE Bot |
| 50 | +2. 輸入設定檔名稱(應不同於 configuration.yaml 中的名稱)、client_id 及 access_token |
| 51 | + |
| 52 | +請閱讀 [更改設定檔](#更改設定檔) 一節以了解如何使用設定檔 |
| 53 | + |
| 54 | +## 使用方式 |
| 55 | + |
| 56 | +將 LINE 訊息物件傳入服務 |
| 57 | + |
| 58 | +```yaml |
| 59 | +service: notify.line_bot |
| 60 | +data: |
| 61 | + message: >- |
| 62 | + {"messages":[{"type": "text", "text": "Hello, world"}] |
| 63 | +``` |
| 64 | + |
| 65 | +若你懶得打字,沒關係,純文字也是支援的 |
| 66 | + |
| 67 | +```yaml |
| 68 | +service: notify.line_bot |
| 69 | +data: |
| 70 | + message: 'Hello, world' |
| 71 | +``` |
| 72 | + |
| 73 | +請閱讀 [額外資訊](#額外資訊) 一節以了解 LINE 訊息物件格式 |
| 74 | + |
| 75 | +### 更改設定檔 |
| 76 | + |
| 77 | +```yaml |
| 78 | +service: notify.line_bot |
| 79 | +data: |
| 80 | + message: >- |
| 81 | + {"messages":[{"type": "text", "text": "Hello, world"}] |
| 82 | + data: |
| 83 | + profile: cool_line_bot # 設定檔名稱 |
| 84 | +``` |
| 85 | + |
| 86 | +## 額外資訊 |
| 87 | + |
| 88 | +### client_id |
| 89 | + |
| 90 | +> `client_id` 是 LINE 的用戶 ID 或群組 ID |
| 91 | + |
| 92 | +取得 `client_id` 的步驟不太容易,以下是我的方法 |
| 93 | + |
| 94 | +1. 建立 Firebase [Cloud Functions](https://console.firebase.google.com/) |
| 95 | +2. 將下列程式部署至 Cloud Functions |
| 96 | + |
| 97 | +```js |
| 98 | +const functions = require('firebase-functions') |
| 99 | +
|
| 100 | +exports.helloWorld = functions.https.onRequest((request, response) => { |
| 101 | + const events = request.body.events |
| 102 | + const source = events.length > 0 ? events[0].source : null |
| 103 | +
|
| 104 | + if (source) { |
| 105 | + functions.logger.info(source.groupId || source.userId) |
| 106 | + } |
| 107 | + response.send('Hello from Firebase!') |
| 108 | +}) |
| 109 | +``` |
| 110 | + |
| 111 | +3. 在 LINE Messaging API 設定中啟用 webhook |
| 112 | +  |
| 113 | +4. 將 bot 帳號加入好友或邀請進群組,並跟 bot 對話 |
| 114 | +5. 這時就可以在 Cloud Funtions 的 log 看見 `client_id` |
| 115 | +  |
| 116 | +6. 記得再次停用 webhook,否則 log 可能會被塞爆 |
| 117 | + |
| 118 | +### access_token |
| 119 | + |
| 120 | +> `access_token` 可於 LINE Developer 網站取得 |
| 121 | + |
| 122 | +前往 `https://developers.line.biz/console/channel/<YOUR CHANNEL ID>/messaging-api` |
| 123 | + |
| 124 | +可於 `Channel access token` 新增並取得 token |
| 125 | + |
| 126 | + |
| 127 | +### LINE 訊息物件 (LINE Message Object) |
| 128 | + |
| 129 | +此套件支援 [Messaging API reference](https://developers.line.biz/en/reference/messaging-api/#message-objects) 中列出的所有訊息類別 |
| 130 | + |
| 131 | +文字訊息範例: |
| 132 | + |
| 133 | +```json |
| 134 | +{ |
| 135 | + "messages": [ |
| 136 | + { |
| 137 | + "type": "text", |
| 138 | + "text": "Hello, world" |
| 139 | + } |
| 140 | + ] |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +Flex 訊息範例: |
| 145 | + |
| 146 | +```json |
| 147 | +{ |
| 148 | + "type": "flex", |
| 149 | + "altText": "this is a flex message", |
| 150 | + "contents": { |
| 151 | + "type": "bubble", |
| 152 | + "body": { |
| 153 | + "type": "box", |
| 154 | + "layout": "vertical", |
| 155 | + "contents": [ |
| 156 | + { |
| 157 | + "type": "text", |
| 158 | + "text": "hello" |
| 159 | + }, |
| 160 | + { |
| 161 | + "type": "text", |
| 162 | + "text": "world" |
| 163 | + } |
| 164 | + ] |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +## 延伸閱讀 |
| 171 | + |
| 172 | +1. [Flex Message Simulator](https://developers.line.biz/flex-simulator/) 協助你快速打造 flex 訊息物件 |
| 173 | +2. [#教學 打造你的智慧家庭吧! 把 LINE 提醒變得更有型](https://www.dcard.tw/f/smart_home/p/235787775) (特別感謝 [Jason Lee](https://www.dcard.tw/@jas0n.1ee.com) 👏) |
| 174 | + |
| 175 | +## 版權 |
| 176 | + |
| 177 | +此專案依 MIT 授權釋出,請閱讀 [LICENSE](LICENSE) 以獲得詳細資訊 |
0 commit comments