各位版上大大好,
小弟我最近剛開始研究LineBot的相關技術,
我搜尋版上資料以及其他線上資源,一起實際演練操作後,
我在發送一般文字訊息LineBot都可以正常回復回應,
我想說要進階一點使用FlexMessage的方式來呈現,卻一直無法成功,
因此想要麻煩版上的大大可以幫忙解答,謝謝。
#=======code======
from flask_ngrok import run_with_ngrok
from flask import Flask, request
from linebot import LineBotApi, WebhookHandler
from linebot.models import TextSendMessage, FlexSendMessage, BubbleContainer, ImageComponent
from datetime import datetime
import json, pandas, os
app = Flask(__name__)
@app.route("/", methods=['POST'])
def linebot():
body = request.get_data(as_text=True)
json_data = json.loads(body)
print(json_data)
try:
line_bot_api = LineBotApi(channel_access_token)
handler = WebhookHandler(channel_secret)
signature = request.headers['X-Line-Signature']
handler.handle(body, signature)
tk = json_data['events'][0]['replyToken']
msg = json_data['events'][0]['message']['text']
msg_time = json_data['events'][0]['timestamp']
if "test" in msg:
line_bot_api.reply_message(tk, FlexSendMessage(alt_text = 'test',
contents = json.load(open('test.json', 'r', encoding='utf-8'))))
except:
False
return 'OK'
if __name__ == "__main__":
app.run()
#=====test.json=====
{
"type": "carousel",
"contents": [
{
"type": "bubble",
"size": "nano",
"header": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "In Progress",
"color": "#ffffff",
"align": "start",
"size": "md",
"gravity": "center"
},
{
"type": "text",
"text": "70%",
"color": "#ffffff",
"align": "start",
"size": "xs",
"gravity": "center",
"margin": "lg"
},
{
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "filler"
}
],
"width": "70%",
"backgroundColor": "#0D8186",
"height": "6px"
}
],
"backgroundColor": "#9FD8E36E",
"height": "6px",
"margin": "sm"
}
],
"backgroundColor": "#27ACB2",
"paddingTop": "19px",
"paddingAll": "12px",
"paddingBottom": "16px"
},
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "box",
"layout": "horizontal",
"contents": [
{
"type": "text",
"text": "Buy milk and lettuce before class",
"color": "#8C8C8C",
"size": "sm",
"wrap": true
}
],
"flex": 1
}
],
"spacing": "md",
"paddingAll": "12px"
},
"styles": {
"footer": {
"separator": false
}
}
}
]
}
#===========
test.json內容是從flex message simulator直接copy下來進行測試的。
再麻煩版上大大們可以不吝賜教,謝謝!!