#该readme教程为Line官网的教程,着重讲述鹦鹉学舌机器人和heroku的app部署
本人的程式将鹦鹉学舌的功能改写为由CNN神经网络训练的针对学校事务的问答机器人
本程式模型的详细训练过程请看NLP_project
PS:app.py中需要的line message api key,如下两处,请先前往官网注册机器
line_bot_api = LineBotApi('')
handler = WebhookHandler('')
本教程介紹如何使用 Python LINE Bot SDK 在 Heroku 上架設一個簡單的學舌鳥機器人。
如果您想以另一種語言架設範例 bot,請參閱以下 LINE Bot SDK repositories。
確保您具有以下內容:
按照以下步驟架設一個學舌鳥機器人。
- 登入 Heroku 後, 在 Heroku 頁面中,點選 New -> Create New App
- 輸入自己喜歡的 App name ,然後點擊 Create app
- 下載 範例程式碼
- 進入 Line 控制台,選擇你剛剛創建的機器人
- 取得 channel secret 和 channel access token,如果沒有內容,請點 Issue
- 使用編輯器開啟範例程式碼資料夾內的 app.py,填入 channel secret 和 channel access token
- 並使用 Heroku CLI 將程式部署到 Heroku 上面 (請參考 使用 Heroku CLI)
- 使用以下 URL 格式在控制台中輸入 webhook URL
{HEROKU_APP_NAME}.herokuapp.com/callback
注意:{HEROKU_APP_NAME} 是步驟2中的應用程序名稱 - 通過在控制台的 “Channel settings” 頁面上掃描 QR Code,將您的機器人添加到 LINE 的朋友中
- 在 Line 上向您的機器人發送文字訊息,並確認它使用相同的訊息進行回應
- 下載並安裝 Heroku CLI、Git
- 開啟範例程式碼資料夾,在路徑上輸入 cmd
- 使用終端或命令行應用程序登錄到 Heroku
heroku login
- 初始化 git
$ git config --global user.name "你的名字"
$ git config --global user.email 你的信箱
- 將資料夾初始成 git 空間
git init
- 用 git 將資料夾與 heroku 連接
heroku git:remote -a {HEROKU_APP_NAME}
注意:{HEROKU_APP_NAME} 是上述步驟2中的應用名稱
- 將資料夾底下所有檔案加入 git 清單,如跳出錯誤訊息請重新執行
git add .
- 儲存記錄點,如跳出錯誤訊息請詳讀
git commit -m "Init"
注意:"Init" 可使用任意文字替換,其為此紀錄點的敘述
- 將在 git 清單中的檔案上傳到 heroku,請確認訊息是否顯示成功
git push heroku master
每當需要更新 Bot 時,請重新執行 7、8、9 步驟
第一次操作完請回到 架設範例機器人 第 5 項繼續接下來的步驟
當成是遇到問題時,可查看日誌以找出錯誤
要查看您的機器人在 Heroku 的日誌,請按照以下步驟。
- 如果沒登入,請先透過 Heroku CLI 登入
heroku login
- 顯示 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 會依據這份文件來安裝需要套件
可透過修改程式裡的 handle_message() 方法內的程式碼來達成不同的回覆效果。
新版範例程式碼內附註解 如想更多了解此程式,可以去研究 Python3、Flask 套件、Line bot sdk
line_bot_api.reply_message(reply_token, 訊息物件)
bot 需要有 push 功能才可以做,否則會出錯
line_bot_api.push_message(push_token, 訊息物件)
修改範例程式碼中, handle_message() 方法內的程式碼,可實現多種功能
message = TextSendMessage(text='Hello, world')
line_bot_api.reply_message(event.reply_token, message)
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)
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)
message = AudioSendMessage(
original_content_url='https://example.com/original.m4a',
duration=240000
)
line_bot_api.reply_message(event.reply_token, message)
message = LocationSendMessage(
title='my location',
address='Tokyo',
latitude=35.65910807942215,
longitude=139.70372892916203
)
line_bot_api.reply_message(event.reply_token, message)
message = StickerSendMessage(
package_id='1',
sticker_id='1'
)
line_bot_api.reply_message(event.reply_token, message)
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)
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)
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)
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)
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)