---
slideOptions:
transition: slide
---
# 建立LINE bot
---
### 什麼是LINE bot
----
讓LINE帳號可以接收用戶訊息並自動回傳訊息的應用程式,也就是LINE的聊天機器人
----
#### 如何運作
當使用者傳了一個訊息時,會透過LINE Platform 傳一個 request 到設定的webhook,應用程式會透過 Messaging API 去處理及傳遞 request 到 LINE Platform 然後回應給使用者,這些 request 必須透過 HTTPS 去傳遞
[原文](https://developers.line.me/en/docs/messaging-api/overview/)
----
#### Messaging API 可以做什麼
- Send reply messages
- Send push messages
- Send various message types
-- including [text](https://developers.line.me/en/docs/messaging-api/message-types#text-messages), [stickers](https://developers.line.me/en/docs/messaging-api/message-types#stickers), [images](https://developers.line.me/en/docs/messaging-api/message-types#image), [videos](https://developers.line.me/en/docs/messaging-api/message-types#video), [audio](https://developers.line.me/en/docs/messaging-api/message-types#audio), [location](https://developers.line.me/en/docs/messaging-api/message-types#location), [imagemaps](https://developers.line.me/en/docs/messaging-api/message-types#imagemap-messages), and [template](https://developers.line.me/en/docs/messaging-api/message-types#template-messages) messages
- Get user profiles
-- You can get users' display names, profile images and status messages.
- Join group chats
---
### 建立LINE bot
----
#### 需求
----
- Line bot帳號
- https server(使用heroku,LINE 官方示範)
- Node.js >= 4, preferably >=6
- npm preferably >=5
----
- [登入LINE帳號](https://developers.line.me/en/)
- 建立provider![](https://i.imgur.com/OFgQgHZ.png)
- 建立bot channel![](https://i.imgur.com/KzZVLtQ.png)
![](https://i.imgur.com/Ylrkpk5.png)
![](https://i.imgur.com/qAgTD4c.png)
![](https://i.imgur.com/t9lroE4.png)
----
### 建立heroku帳號
- [註冊帳號](https://www.heroku.com/home)
- 建立app![](https://i.imgur.com/XAoKcTh.png)
![](https://i.imgur.com/8g4Fekz.png)
![](https://i.imgur.com/jcKTFn5.png)
![](https://i.imgur.com/m8bo2JX.png)
---
### 設定LINE bot未完成的部分
![](https://i.imgur.com/KPUS2n4.png)
![](https://i.imgur.com/2VgmDU3.png)
![](https://i.imgur.com/uREcZ50.png)
![](https://i.imgur.com/Vsm8odg.png)
---
### 建立專案
- `mkdir line-bot && cd line-bot`
- `npm init`
- `npm install --save express @line/bot-sdk`
- `touch index.js .gitignore`
----
```js=
const express = require('express');
const line = require('@line/bot-sdk');
const config = {
channelAccessToken: '比較長的那串',
channelSecret: '比較短的'
};
const app = express();
app.post('/', line.middleware(config), (req, res) => {
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result));
});
const client = new line.Client(config);
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
return Promise.resolve(null);
}
return client.replyMessage(event.replyToken, {
type: 'text',
text: event.message.text
});
}
app.listen(process.env.PORT || 8080, function() {
console.log("App now running on port");
});
```
----
在package.json加上 "start":"node index.js"
```js=
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
```
記得在.gitignore裡,加上node_modules
----
`git add .`
`git commit -m 'init'`
`git push https://git.heroku.com/linedemobot.git
master`
---
### 參考
- [line-bot-sdk-nodejs](https://line.github.io/line-bot-sdk-nodejs/)
- [![line developer](https://developers.line.me/assets/img/structures/header/logo.svg)](https://developers.line.me/en/)