以下是我簡單做一下的模板,做完大概長這樣,左邊的圖片就會是顯示在line上面的樣子這邊還有一個重點是右下方有個Action的部分。
type主要分成
這時候右上角就可以點開看到這個樣板的JSON格式了
把這些JSON格式複製下來貼到程式碼去呼叫他就可以使用囉
<放模板的地方>.py
from abc import ABC, abstractmethod
from linebot.models import TemplateSendMessage, ButtonsTemplate, PostbackAction, MessageAction, URIAction, CarouselColumn, CarouselTemplate, PostbackTemplateAction, FlexSendMessage
class Message(ABC):
def __init__(self):
pass
@abstractmethod
def content(self):
pass
# 「功能列表」按鈕樣板訊息
class Featuresmodel():
def content(self):
flex_message = FlexSendMessage(
alt_text='hello',
contents={
"type": "bubble",
"hero": {
"type": "image",
"url": "https://img.technews.tw/wp-content/uploads/2022/08/12160410/Meta-Logo1.jpg",
"size": "3xl",
"aspectRatio": "20:13",
"aspectMode": "cover",
"action": {
"type": "uri",
"uri": "http://linecorp.com/"
}
},
"footer": {
"type": "box",
"layout": "vertical",
"spacing": "sm",
"contents": [
{
"type": "button",
"style": "primary",
"height": "sm",
"action": {
"type": "postback",
"label": "近期瀏覽",
"data": "近期瀏覽"
}
}
],
"flex": 0
}
}
)
return flex_message
views.py
主程式在跑的地方,當接收到訊息的時候會來這個地方做判斷。
for event in events:
if isinstance(event, MessageEvent): # 如果有訊息事件
if event.message.text == "home":
line_bot_api.reply_message( # 回復傳入的訊息文字
event.reply_token,
Featuresmodel().content()
)
在linebot裡輸入home即可呼叫樣板,如下。