各位前輩好,目前遇到一個問題
在linebot中的flex模板能否製作出像carousel template一樣的效果,爬了一些文(https://developers.line.biz/en/reference/messaging-api/)
似乎能夠實現,但我一直無法成功,也套用Line官方提供的工具Flex Message Simulator(https://developers.line.biz/flex-simulator/)
僅能實現一個flex message reply(如case 1)
Case 1可以work,但Case 2卻無法成功
請問是哪裡出現問題了呢?
以下是我的code
Case 1:
if event.message.text == 'test1':
reply_header = {'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + 'xxx'}
reply_json = {
"replyToken": event.reply_token,
"messages": [
{
"type": "flex",
"altText": "Flex Message",
"contents": {
"type": "bubble",
"direction": "ltr",
"header": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "Header",
"align": "center"
}
]
},
"hero": {
"type": "image",
"url": "https://developers.line.me/assets/images/services/bot-designer-icon.png",
"size": "full",
"aspectRatio": "1.51:1",
"aspectMode": "fit"
},
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "Body",
"align": "center"
}
]
},
"footer": {
"type": "box",
"layout": "horizontal",
"contents": [
{
"type": "button",
"action": {
"type": "uri",
"label": "Button",
"uri": "https://linecorp.com"
}
}
]
}
}
}
]
}
r = requests.post("https://api.line.me/v2/bot/message/reply", headers=reply_header, json=reply_json)
Case 2:
if event.message.text == 'test2':
reply_header = {'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + 'xxx'}
reply_json = {
"replyToken": event.reply_token,
"type": "carousel",
"contents": [
{
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "First bubble"
}
]
}
},
{
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "Second bubble"
}
]
}
}
]
}
r = requests.post("https://api.line.me/v2/bot/message/reply", headers=reply_header, json=reply_json)
參數有缺,少了messages
, type
, altText
和contents
應該是這樣吧(還沒測試過)
{
"replyToken": event.reply_token,
"messages": [
{
"type": "flex",
"altText": "Flex Message",
"contents": {
"type": "carousel",
"contents": [
{
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "First bubble"
}
]
}
},
{
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "Second bubble"
}
]
}
}
]
}
}
]
}
Carousel也是屬於Flex Message, type必填flex