iT邦幫忙

0

Python Line Bot無法正常發送 FlexMessage 疑問

不明 2022-07-08 09:52:42947 瀏覽
  • 分享至 

  • xImage

各位版上大大好,
小弟我最近剛開始研究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下來進行測試的。
再麻煩版上大大們可以不吝賜教,謝謝!!

powerc iT邦研究生 5 級 ‧ 2022-07-08 11:47:36 檢舉
請擷取line api回傳給你的訊息,通常會告訴哪你邊不對,比如token、request body之類的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
熊熊工程師
iT邦研究生 1 級 ‧ 2022-07-09 19:59:36

我記得 contents 接收的好像只能是字串來著? 而且好像有長度上限?

我要發表回答

立即登入回答