---
# System prepended metadata

title: 'LineBot 聊天機器人（Python+Flask+Heroku)建立紀錄 Part 2-2 : 指令爬取表特版照片（串接回LineBot）'
tags: [LineBot]

---

<style>

html, body, .ui-content {
    background-color: #333;
    color: #ddd;
}

body > .ui-infobar {
    display: none;
}

.ui-view-area > .ui-infobar {
    display: block;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
    color: #ddd;
}

.markdown-body h1,
.markdown-body h2 {
    border-bottom-color: #ffffff69;
}

.markdown-body h1 .octicon-link,
.markdown-body h2 .octicon-link,
.markdown-body h3 .octicon-link,
.markdown-body h4 .octicon-link,
.markdown-body h5 .octicon-link,
.markdown-body h6 .octicon-link {
    color: #fff;
}

.markdown-body img {
    background-color: transparent;
}

.ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a {
    color: white;
    border-left: 2px solid white;
}

.expand-toggle:hover, 
.expand-toggle:focus, 
.back-to-top:hover, 
.back-to-top:focus, 
.go-to-bottom:hover, 
.go-to-bottom:focus {
    color: white;
}


.ui-toc-dropdown {
    background-color: #333;
}

.ui-toc-label.btn {
    background-color: #191919;
    color: white;
}

.ui-toc-dropdown .nav>li>a:focus, 
.ui-toc-dropdown .nav>li>a:hover {
    color: white;
    border-left: 1px solid white;
}

.markdown-body blockquote {
    color: #bcbcbc;
}

.markdown-body table tr {
    background-color: #5f5f5f;
}

.markdown-body table tr:nth-child(2n) {
    background-color: #4f4f4f;
}

.markdown-body code,
.markdown-body tt {
    color: #eee;
    background-color: rgba(230, 230, 230, 0.36);
}

a,
.open-files-container li.selected a {
    color: #5EB7E0;
}


</style>}
# LineBot 聊天機器人（Python+Flask+Heroku)建立紀錄 Part 2-2 : 指令爬取表特版照片（串接回LineBot）
###### tags: `LineBot`


[上一篇](https://hackmd.io/A6N2vZU_ShqtNVlDzy6Hhg)已經成功爬取表特版的照片並回傳正確的網址，雖然速度有夠慢，但還是先串上LineBot試試看功能正不正常，先求有再求好。

### 事前準備

將上一篇寫得Function import 進來

```
from helper import get_picture
```

### 機器人主體

跟之前一模一樣

```
@app.route('/callback', methods= ['POST'])
def callback():
    signature = request.headers['X-Line-Signature']

    body = request.get_data(as_text = True)
    app.logger.info("Request body: " + body)
    print(body)

    try:
        print(body, signature)
        handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)
    return 'OK'

```

### Get 妹子功能

```
@handler.add(MessageEvent, message = TextMessage)
def handle_message(event):

    if event.source.user_id != "Udeadbeefdeadbeefdeadbeefdeadbeef":
        
        if event.message.text == '表特抽抽':
            pic = get_picture() 
            message1 = TextSendMessage(text = '拿去打手槍')
            message2 = ImageSendMessage(original_content_url=pic, preview_image_url=pic)
            message = [message1, message2]
            line_bot_api.reply_message(event.reply_token, message)

```

當接收到表特抽抽的訊息時進入if 迴圈`if event.message.text == '表特抽抽':` 接著使用`pic = get_picture()`將圖片網址存在pic變數內， 再藉由Line的`reply_message`功能傳回至聊天視窗。實際運行結果如下：

![](https://i.imgur.com/BfGInia.png)

## 心得

成功但慢，爬蟲是一個易學難精的東西。