---
# System prepended metadata

title: Line Bot API
tags: [Line Bot 簡易上手教學]

---

# Line Bot API
###### tags: `Line Bot 簡易上手教學`

## 回應訊息基本語法
* 當使用者傳送訊息給Line Bot時，會觸發MessageEvent事件，這裡僅處理收到的文字訊息
```python=
#TextMessage表示收到的是文字訊息
@handler.add(MessageEvent, message = TextMessage)
```

* 建立路由的函式
```python=
def 函式名稱(event)
```
* 參數event包含傳回的各項訊息

* 通常處理文字的第一步是取得使用者傳送的文字
```python=
傳送文字變數 = event.message.text
```
* 綜合後
```python=
@handler.add(MessageEvent, message = TextMessage)
def handle_message(event):
    mtext = event.message.text
    if mtext == "傳送文字1":
        處理1
    #......
```

## 回傳文字訊息
* 回復訊息
```python=
#訊息種類分為text, image, location, sticker, audio, vedio, template
line_bot_api.reply_message(event, reply_token, 訊息種類)
```
* 訊息命令
```python=
訊息命令(參數1 = 值1, 參數2 = 值2, ......)
```
* 最簡單的回傳文字
```python=
line_bot_api.reply_message(event, reply_token, TextSendMessage(text = 文字訊息內容))
```

* 增加可讀性的話......
```python=
message = TextSendMessage(
    text = 文字訊息內容
)

line_bot_api.reply_message(event, reply_token, message)
```

## 建立回應訊息的Line Bot
1. 建立Messaging API Channel
2. 建立Flask程式
3. 執行Flask程式可啟動本機伺服器，再啟動ngrok伺服器，設定Messaging API Channel的Webhook URL為ngrok的伺服器網址

* 一樣先自行建立圖文選單
![](https://i.imgur.com/k5L7fst.png)

* @傳送文字....自行輸入
![](https://i.imgur.com/YX3EqJb.png)
![](https://i.imgur.com/OZIUQsB.png)

* 回到python檔
```python=
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, StickerSendMessage, LocationSendMessage, QuickReply, QuickReplyButton, MessageAction

@handler.add(MessageEvent, message = TextMessage)
def handler_message(event):
    mtext = event.message.text
    if mtext == "@傳送文字":
        try:
            message = TextSendMessage(
                text = "我是xia_bot，\n您好!"
            )
            line_bot_api.reply_message(event.reply_token, message)
        except:
            line_bot_api.reply_message(event.reply_token, TextSendMessage(text = '發生錯誤'))
```
* 啟動本機即ngrok伺服器
![](https://i.imgur.com/zPNa01Z.png)
* 記得修改Webhook settings

* 結果
![](https://i.imgur.com/lra2R21.png)

## 回傳圖片
* 語法為
```python=
訊息變數 = ImageSendMessage(
    original_content_url = 原始圖片網址,
    preview_image_url = 預覽圖片網址
)
```
* 通常圖片會存在雲端上
```python=
#續
elif mtext == "@傳送圖片":
    try:
        message = ImageSendMessage(
            original_content_url = 'https://i.imgur.com/4QfKuz1.png',
            preview_image_url = 'https://i.imgur.com/4QfKuz1.png'
        )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text = '發生錯誤'))
```
* 結果
![](https://i.imgur.com/s6EAjxc.png)

## 回傳貼圖訊息
* 語法
```python=
訊息變數 = StickerSendMessage(
    package_id = '群組id',
    sticker_id = '貼圖id'
)

line_bot_api.reply_message(event.reply_token, 訊息變數)
```
* Line[貼圖查詢](https://developers.line.biz/en/docs/messaging-api/sticker-list/)
![](https://i.imgur.com/TN28rm1.png)

* 續
```python=
elif mtext == "@傳送貼圖":
    try:
        message = StickerSendMessage(
                package_id = 446,
                sticker_id = 2021
            )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text = '發生錯誤'))
```

* 結果
![](https://i.imgur.com/bb4w2rq.jpg)

## 同時傳送多個訊息
* 使用串列
```python=
訊息變數 = [
    第一個,
    第二個
    ......
]
```
* 範例
```python=
elif mtext == "@多項傳送":
    try:
        message = [
                StickerSendMessage(
                    package_id = 446,
                    sticker_id = 2021
                ),
                TextSendMessage(
                    text = "這是pizza圖片"
                ),
                ImageSendMessage(
                    original_content_url = 'https://i.imgur.com/4QfKuz1.png',
                    preview_image_url = 'https://i.imgur.com/4QfKuz1.png'
                )
            ]
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text = '發生錯誤'))
```
* 結果
![](https://i.imgur.com/QoFo31r.jpg)

## 回傳位置
* 語法
```python=
訊息變數 = LocationSendMessage(
    title = 標題,
    address = 地址,
    latitude = 緯度,
    longtiude = 經度
)

line_bot_api.reply_message(event.reply_token, 訊息變數)
```
* 續
```python=
elif mtext == "@傳送位置":
    try:
        message = LocationSendMessage(
            title = '101大樓',
            address = '台北市信義區五段7號',
            latitude = 25.034207,
            longitude = 121.564590
        )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text = '發生錯誤'))
```

* 結果
![](https://i.imgur.com/kTPTWG2.jpg)

## 快速選單
* 語法
```python=
message = TextSendMessage(
    text = "提示文字",
    quick_reply = QuickReply(
        items = [
            QuickReplyButton(action = MessageAction(label = "顯示值", text = '選取值')),
                ]
            )
        )
line_bot_api.reply_message(event.reply_token, message)
```

* 續
```python=
elif mtext == '@快速選單':
    try:
        message = TextSendMessage(
            text = "最喜歡的程式語言",
            quick_reply = QuickReply(
                items = [
                    QuickReplyButton(action = MessageAction(label = "Python", text = 'python')),
                    QuickReplyButton(action = MessageAction(label = "Java", text = 'Java')),
                    QuickReplyButton(action = MessageAction(label = "C++", text = 'C++')),
                    QuickReplyButton(action = MessageAction(label = "C#", text = 'C#')),
                ]
            )
        )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text = '發生錯誤'))  
```

* 結果
![](https://i.imgur.com/e2ieDah.png)

## 回應多媒體訊息
* 回傳聲音訊息
```python=
訊息變數 = AudioSendMessage(
    original_content_url = 聲音檔網址,
    duration = 時間長度
)
```
* 先在line manager上建一個......
![](https://i.imgur.com/U9dXjpk.png)

* 背景圖
![](https://i.imgur.com/o7M4zXo.png)

* 續
```python=
baseurl = "https://c80caaf03cbc.ngrok.io/static"

#//續
elif mtext == '@傳送聲音':
    try:
        message = AudioSendMessage(
            original_content_url = baseurl + 'mario.m4a',
            duration = 20000  #20s
            )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text = '發生錯誤'))
```

* 結果
![](https://i.imgur.com/JKfl2ro.png)

##  傳送影片

* 續
```python=
elif mtext == '@傳送影片':
    try:
        message = VideoSendMessage(
            original_content_url=baseurl + 'robot.mp4',  #影片檔置於static資料夾
            preview_image_url=baseurl + 'robot.jpg'
        )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤！'))
```

{%hackmd S1DMFioCO %}