# 鸚鵡 Line Bot ###### tags: `Line Bot 簡易上手教學` {%hackmd S1DMFioCO %} * 先重新建立一隻Line Bot  * 在Basic Setting 找到 Channel secret  * Messaging API settings 的 Channel access token  ## 安裝 Line Bot SDK ```python= pip install line-bot-sdk==1.18.0 ``` * 使用Flask建立網站 ```python= from flask import Flask app = Flask(__name__) from flask import request, abort from linebot import LineBotApi, WebhookHandler from linebot.exceptions import InvalidSignatureError from linebot.models import MessageEvent, TextMessage, TextSendMessage import json with open('line_bot.json', "r", encoding = 'utf8') as data: jsondata = json.load(data) line_bot_api = LineBotApi(jsondata["Channel_access_token"]) handler = WebhookHandler(jsondata["Channel_secret"]) #檢查路由,確認資料是否正確 @app.route('/callback', methods = ['POST']) def callback(): signature = request.headers['X_Line_Signature'] body = request.get_data(as_text = True) try: handler.handle(body, signature) except InvalidSignatureError: abort(400) return 'OK' #回傳 @handler.add(MessageEvent, message = TextMessage) def handler_message(event): line_bot_api.reply_message(event.reply_token, TextSendMessage(text = event.message.text)) if __name__ == '__main__': app.run(debug = True) ``` * json ```json= { "Channel_access_token":"你的Channel_access_token", "Channel_secret":"你的Channel_secret" } ``` ## 使用ngrok建立https伺服器 * [下載](https://ngrok.com/download)  * 解壓縮後丟進資料夾  * 啟動伺服器 ```ngrok= ngrok http 埠位號碼 ``` * 切換到當前資料夾 ```ngrok ngrok http 5000 ``` * 複製最長的那部分的網址  * 到Message API  * 輸入 ```htmlembedded= https://{剛剛複製的網址}/callback ``` * npdate,即啟用  * 重設 ```python= # ngrok伺服器重新啟動後,就必須修改一次Webhook URL的值 ``` * 修正  * Line Bot 實際畫面 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up