All Projects → yaoandy107 → Line Bot Tutorial

yaoandy107 / Line Bot Tutorial

Line bot tutorial.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Line Bot Tutorial

Line Bot Tutorial
line-bot-tutorial use python flask
Stars: ✭ 267 (+47.51%)
Mutual labels:  bot, heroku, line
Line Bot Sdk Perl
LINE Messaging API SDK for Perl
Stars: ✭ 69 (-61.88%)
Mutual labels:  bot, line
Jonsnow
App Store/Google Play review watcher, deliver new reviews to slack channel
Stars: ✭ 65 (-64.09%)
Mutual labels:  bot, heroku
Steem Fossbot Voter
A curation bot built for the Steemit social media platform, decides which posts to vote for and casts vote on behalf of a registered user
Stars: ✭ 92 (-49.17%)
Mutual labels:  bot, heroku
Lita Line
A Line adapter for Lita
Stars: ✭ 15 (-91.71%)
Mutual labels:  bot, line
Modmail
A feature rich discord Modmail bot
Stars: ✭ 957 (+428.73%)
Mutual labels:  bot, heroku
Whatsapp Node Api
A Simple NodeJS API Wrapper for WhatsApp
Stars: ✭ 81 (-55.25%)
Mutual labels:  bot, heroku
Line Bot Sdk Go
LINE Messaging API SDK for Go
Stars: ✭ 654 (+261.33%)
Mutual labels:  bot, line
Whatsapp Api
Rest API to send messages through whatsapp
Stars: ✭ 112 (-38.12%)
Mutual labels:  bot, heroku
Telegram Clonebot
Simple Bot to clone Google Drive Files (or Folders) to your Team Drive[or Normal Drive]. P.S This is not a Mirror Bot. Enjoy ✌🏻
Stars: ✭ 114 (-37.02%)
Mutual labels:  bot, heroku
Messaging Apis
Messaging APIs for multi-platform
Stars: ✭ 1,754 (+869.06%)
Mutual labels:  bot, line
Notify
A dead simple Go library for sending notifications to various messaging services.
Stars: ✭ 727 (+301.66%)
Mutual labels:  bot, line
Integrations
Connect your App to Multiple Messaging Channels with the W3C Open standard.
Stars: ✭ 721 (+298.34%)
Mutual labels:  bot, line
Node Line Messaging Api
Unofficial SDK for LINE Messaging API 🤖 💬
Stars: ✭ 41 (-77.35%)
Mutual labels:  bot, line
Line Bot Sdk Nodejs
LINE Messaging API SDK for Node.js
Stars: ✭ 683 (+277.35%)
Mutual labels:  bot, line
Line Bot Sdk Python
LINE Messaging API SDK for Python
Stars: ✭ 1,198 (+561.88%)
Mutual labels:  bot, line
Linebottemplate
A simple Golang LINE bot Template and tutorial how to setup on Heroku for LINE bot API
Stars: ✭ 156 (-13.81%)
Mutual labels:  heroku, line
Line Bot Sdk Java
LINE Messaging API SDK for Java
Stars: ✭ 484 (+167.4%)
Mutual labels:  bot, line
Line Bot Sdk Php
LINE Messaging API SDK for PHP
Stars: ✭ 601 (+232.04%)
Mutual labels:  bot, line
Linesimulator
LINESimulator
Stars: ✭ 103 (-43.09%)
Mutual labels:  bot, line

Line Bot 教學

本教程介紹如何使用 Python LINE Bot SDK 在 Heroku 上架設一個簡單的回話機器人。

如果您想以另一種語言架設範例 bot,請參閱以下 LINE Bot SDK repositories。

在你開始之前

確保您具有以下內容:

  • 擁有 Line 帳號
  • 擁有 Heroku 帳戶(您可以免費創建一個)

建立 Heroku 專案

  1. 登入 Heroku 後, 在 Heroku 頁面中,點選 New -> Create New App
  2. 輸入自己喜歡的 App name ,然後點擊 Create app

創建 Line Bot 頻道

  1. 進入 Line 控制台
  2. 創建提供者
  3. 填入提供者名稱
  4. 點擊 Create
  5. 點擊 Create Channel
  6. 填入 Bot 資訊
  7. 同意 Line 條款,並按 Create
  8. 選擇剛剛創建的 Bot

設定範例機器人

按照以下步驟架設一個回話機器人。

  1. 下載 範例程式碼

  2. 進入 Line 控制台,選擇你剛剛創建的機器人

  3. 開啟 webhook

  4. 關閉預設罐頭回覆訊息

  5. 產生 Channel access token

  6. 取得 Channel access token

  7. 取得 Channel secret

  8. 使用編輯器開啟範例程式碼資料夾內的 app.py,將剛剛取得的 channel secretchannel access token 填入

將程式推到 Heroku 上

  1. 下載並安裝 Heroku CLIGit

  2. 開啟剛剛下載的範例程式碼資料夾,在路徑上輸入 cmd

  3. 使用終端或命令行應用程序登錄到 Heroku

    heroku login
    
  4. 初始化 git

    $ git config --global user.name "你的名字"
    $ git config --global user.email 你的信箱
    

    注意:你的名字你的信箱 要換成各自的 名字信箱

  5. 初始化 git

    git init
    

    注意:僅第一次使用時要輸入

  6. 用 git 將資料夾與 heroku 連接

    heroku git:remote -a {HEROKU_APP_NAME}
    

    注意:{HEROKU_APP_NAME} 是 Heroku 應用的名稱

  7. 輸入以下指令,將程式碼推上 Heroku,如果有跳出錯誤請重新輸入

    git add .
    git commit -m "Add code"
    git push -f heroku master
    

    每當需要更新 Bot 時,請重新輸入上述指令

將 Heroku 與 Line 綁定

  1. 進入 Line 控制台,選擇你剛剛創建的 Bot

  2. 在 webhook URL 中輸入 Heroku 網址

    {HEROKU_APP_NAME}.herokuapp.com/callback
    

    注意:{HEROKU_APP_NAME} 是 Heroku 應用的名稱

測試範例成果

  1. 進入 Line 控制台,選擇你剛剛創建的 Bot
  2. 通過在控制台的 “Channel settings” 頁面上掃描 QR Code,將您的 Bot 添加到 LINE 的朋友中
  3. 在 Line 上向您的 Bot 發送文字訊息,並確認它會學你說話

錯誤尋找

當程式遇到問題時,可查看日誌以找出錯誤

要查看您的 Bot 在 Heroku 的日誌,請按照以下步驟。

  1. 如果沒登入,請先透過 Heroku CLI 登入

    heroku login
    
  2. 顯示 app 日誌

    heroku logs --tail --app {HEROKU_APP_NAME}
    

    注意:{HEROKU_APP_NAME} 是上述步驟2中的應用名稱。

    --tail                     # 持續打印日誌
    --app {HEROKU_APP_NAME}    # 指定 App
    

程式檔案解說

資料夾裡需含有兩份文件來讓你的程式能在 heroku 上運行

  • Procfile:heroku 執行命令,web: {語言} {檔案},這邊語言為 python,要自動執行的檔案為 app.py,因此我們改成 web: python app.py
  • requirements.txt:列出所有用到的套件,heroku 會依據這份文件來安裝需要套件

app.py (主程式)

可透過修改程式裡的 handle_message() 方法內的程式碼來控制機器人的訊息回覆

新版範例程式碼內附註解 如想更多了解此程式,可以去研究 Git、Python3、Flask 套件Line bot sdk

進階操作

官方文件

回覆訊息

只有當有訊息傳來,才能回覆訊息

line_bot_api.reply_message(reply_token, 訊息物件)

主動傳送訊息

Bot 需要有開啟 push 功能才可以做,否則程式會出錯

line_bot_api.push_message(push_token, 訊息物件)

訊息物件分類

官方文件

修改範例程式碼中, handle_message() 方法內的程式碼,可實現多種功能

TextSendMessage (文字訊息)

![](https://i.imgur.com/LieCFAb.png =250x)

message = TextSendMessage(text='Hello, world')
line_bot_api.reply_message(event.reply_token, message)

ImageSendMessage(圖片訊息)

![](https://i.imgur.com/RaH7gqo.png =250x)

message = ImageSendMessage(
    original_content_url='https://example.com/original.jpg',
    preview_image_url='https://example.com/preview.jpg'
)
line_bot_api.reply_message(event.reply_token, message)

VideoSendMessage(影片訊息)

![](https://i.imgur.com/o6cvf3o.png =250x)

message = VideoSendMessage(
    original_content_url='https://example.com/original.mp4',
    preview_image_url='https://example.com/preview.jpg'
)
line_bot_api.reply_message(event.reply_token, message)

AudioSendMessage(音訊訊息)

![](https://i.imgur.com/w5szZag.png =250x)

message = AudioSendMessage(
    original_content_url='https://example.com/original.m4a',
    duration=240000
)
line_bot_api.reply_message(event.reply_token, message)

LocationSendMessage(位置訊息)

![](https://i.imgur.com/tXE7Aus.png =250x)

message = LocationSendMessage(
    title='my location',
    address='Tokyo',
    latitude=35.65910807942215,
    longitude=139.70372892916203
)
line_bot_api.reply_message(event.reply_token, message)

StickerSendMessage(貼圖訊息)

![](https://i.imgur.com/7x0mgK1.png =250x)

message = StickerSendMessage(
    package_id='1',
    sticker_id='1'
)
line_bot_api.reply_message(event.reply_token, message)

ImagemapSendMessage (組圖訊息)

![](https://i.imgur.com/MoSf2D6.png =250x)

message = ImagemapSendMessage(
    base_url='https://example.com/base',
    alt_text='this is an imagemap',
    base_size=BaseSize(height=1040, width=1040),
    actions=[
        URIImagemapAction(
            link_uri='https://example.com/',
            area=ImagemapArea(
                x=0, y=0, width=520, height=1040
            )
        ),
        MessageImagemapAction(
            text='hello',
            area=ImagemapArea(
                x=520, y=0, width=520, height=1040
            )
        )
    ]
)
line_bot_api.reply_message(event.reply_token, message)

TemplateSendMessage - ButtonsTemplate (按鈕介面訊息)

![](https://i.imgur.com/41lXWjP.png =250x)

message = TemplateSendMessage(
    alt_text='Buttons template',
    template=ButtonsTemplate(
        thumbnail_image_url='https://example.com/image.jpg',
        title='Menu',
        text='Please select',
        actions=[
            PostbackTemplateAction(
                label='postback',
                text='postback text',
                data='action=buy&itemid=1'
            ),
            MessageTemplateAction(
                label='message',
                text='message text'
            ),
            URITemplateAction(
                label='uri',
                uri='http://example.com/'
            )
        ]
    )
)
line_bot_api.reply_message(event.reply_token, message)

TemplateSendMessage - ConfirmTemplate(確認介面訊息)

![](https://i.imgur.com/U8NDhrt.png =250x)

message = TemplateSendMessage(
    alt_text='Confirm template',
    template=ConfirmTemplate(
        text='Are you sure?',
        actions=[
            PostbackTemplateAction(
                label='postback',
                text='postback text',
                data='action=buy&itemid=1'
            ),
            MessageTemplateAction(
                label='message',
                text='message text'
            )
        ]
    )
)
line_bot_api.reply_message(event.reply_token, message)

TemplateSendMessage - CarouselTemplate

![](https://i.imgur.com/982Glgo.png =250x)

message = TemplateSendMessage(
    alt_text='Carousel template',
    template=CarouselTemplate(
        columns=[
            CarouselColumn(
                thumbnail_image_url='https://example.com/item1.jpg',
                title='this is menu1',
                text='description1',
                actions=[
                    PostbackTemplateAction(
                        label='postback1',
                        text='postback text1',
                        data='action=buy&itemid=1'
                    ),
                    MessageTemplateAction(
                        label='message1',
                        text='message text1'
                    ),
                    URITemplateAction(
                        label='uri1',
                        uri='http://example.com/1'
                    )
                ]
            ),
            CarouselColumn(
                thumbnail_image_url='https://example.com/item2.jpg',
                title='this is menu2',
                text='description2',
                actions=[
                    PostbackTemplateAction(
                        label='postback2',
                        text='postback text2',
                        data='action=buy&itemid=2'
                    ),
                    MessageTemplateAction(
                        label='message2',
                        text='message text2'
                    ),
                    URITemplateAction(
                        label='uri2',
                        uri='http://example.com/2'
                    )
                ]
            )
        ]
    )
)
line_bot_api.reply_message(event.reply_token, message)

TemplateSendMessage - ImageCarouselTemplate

![](https://i.imgur.com/2ys1qqc.png =250x)

message = TemplateSendMessage(
    alt_text='ImageCarousel template',
    template=ImageCarouselTemplate(
        columns=[
            ImageCarouselColumn(
                image_url='https://example.com/item1.jpg',
                action=PostbackTemplateAction(
                    label='postback1',
                    text='postback text1',
                    data='action=buy&itemid=1'
                )
            ),
            ImageCarouselColumn(
                image_url='https://example.com/item2.jpg',
                action=PostbackTemplateAction(
                    label='postback2',
                    text='postback text2',
                    data='action=buy&itemid=2'
                )
            )
        ]
    )
)
line_bot_api.reply_message(event.reply_token, message)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].