1.開發者註冊
先至 https://developers.line.biz/ 註冊
建立一個message api
紀錄channel id/channel secret/channel secret token
2.官方帳號基礎設定
至https://manager.line.biz/ 登入
可以設置官方line基本設定,同時電腦可以預先下載line bot designer
手機下載line official account 登入官方line帳號後 line bot designer
3.安裝line bot sdk
CMD:pip install line-bot-sdk==1.18.0
4.載入套件
app.py
"""
from flask import Flask, jsonify, request, abort, render_template, redirect, url_for
from flask_cors import CORS
import requests
import os
from dotenv import load_dotenv
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import LineBotApiError, InvalidSignatureError
from linebot.models import MessageEvent,TextMessage, TextSendMessage
"""
5.建立路由物件
# 載入環境變數
load_dotenv()
app = Flask(__name__, static_folder='templates/assets', static_url_path='/assets')
CORS(app) # 允許跨域請求
@app.route('/')
def index():
return redirect(url_for('manage'))
@app.route('/manage')
def manage():
"""首頁路由"""
return render_template('manage.html')
6.載入環境變數
cmd:
cd <切換至項目根目錄>
pip install python-dotenv
cat > .env << EOF
LINE_CHANNEL_ACCESS_TOKEN="實際的Channel Access Token"
LINE_CHANNEL_SECRET="實際的Channel Secret"
EOF
cat .env
參考相關網址:
https://developers.line.biz/zh-hant/services/messaging-api/