上篇做出選擇飲料的冰塊、甜度、加什麼配料以及杯數還有購買確認,這篇會講結帳,下篇講修改訂單~
↓ view,py
#view,py
elif event.postback.data[0:1] == "F": #結帳
func.sendRECEIPT(event)
↓ func.py
設定 data 為 F 以讓程式回傳結帳。
除了想回傳結帳 Flex Message 外還想傳送一段話給消費者,因此這裡使用了多則訊息傳送(最多五則)。
#func.py
def sendRECEIPT(event):
try:
messageA = []
利用Django語法拿取存取在資料庫使用者所訂購的飲料。
p = order.objects.order_by('id').first()
j = order.objects.order_by('id').count()
c = p.id
for g in range(j): #巡迴在1~資訊總數
if c == p.id: #當此筆資料的id為c時拿取以下資料,此為第一筆資訊
drink = p.drink
ice = p.ice
suger = p.suger
add = p.add
amount = p.amount
price = p.price
m = int(amount) #將數量的資料型態從str轉為int,不然無法運算!!
text1 = "您的第訂購如下:"
drinkA = drink
iceA = ice
sugerA = suger
addA = add
priceA = "$" +price
amountA = amount
tt = drink + ":"
tt += ice + ","
tt += suger + ","
tt += add
total = int(price)*int(amount) #算出總價
c+=1
else: #拿取其他筆資訊
now = order.objects.get(id=c)
drink = now.drink
ice = now.ice
suger = now.suger
add = now.add
amount = now.amount
price = now.price
m += int(amount)
#將同項目資料做整理
text1 += "\n"
drinkA += "\n" + drink
iceA += "\n" + ice
sugerA += "\n" + suger
addA += "\n" + add
priceA += "\n$" + price
amountA += "\n" + amount
tt += "\n" + drink + ":"
tt += ice + ","
tt += suger + ","
tt += add
total += int(price)*int(amount)
c+=1
將剛剛獲得的資料放入 Flex Message。
數字須轉為str才能傳送出去,不然會一直錯誤。
bubble = BubbleContainer(
direction='ltr',
header = BoxComponent(
layout='vertical',
contents=[
TextComponent(
text = "結帳 RECEIPT",
color = "#CCAE8F",
size="md",
weight="bold",
),
TextComponent(
text = "啪噠啪噠河馬",
# = "#1DB446",
size="35px",
weight="bold",
wrap=True,
margin="md"
),
TextComponent(
text = "河馬調製的茶飲能讓您開心一整天!",
# = "#1DB446",
size="xs",
weight="bold",
color="#C8BCC3",
),
SeparatorComponent(
color="#C8BCC3",
margin="xxl"
),
BoxComponent(
layout="vertical",
margin="xxl",
spacing="sm",
contents=[
BoxComponent(
layout="horizontal",
contents=[
TextComponent(
text = '飲料' ,
color="#4B8F8C",
flex=4,
size="sm",
wrap=True
),
TextComponent(
text = '杯' ,
color="#4B8F8C",
align="end",
size="sm",
wrap=True
),
TextComponent(
text = '單價' ,
color="#4B8F8C",
align="end",
size="sm",
wrap=True
),
]
),
BoxComponent(
layout="horizontal",
contents=[
TextComponent(
text = drinkA ,
color="#555555",
flex=4,
wrap=True
),
TextComponent(
text = amountA ,
color="#111111",
align="end",
wrap=True
),
TextComponent(
text = priceA ,
color="#C171BD",
align="end",
wrap=True
),
]
)
]
),
SeparatorComponent(
color="#C8BCC3",
margin="xxl"
),
BoxComponent(
layout="vertical",
margin="xxl",
spacing="sm",
contents=[
BoxComponent(
layout="horizontal",
contents=[
TextComponent(
text = '詳細飲料清單' ,
color="#4B8F8C",
size="sm",
wrap=True
),
]
),
BoxComponent(
layout="horizontal",
contents=[
TextComponent(
text = tt ,
color="#555555",
flex=0,
wrap=True
),
]
)
]
),
SeparatorComponent(
color="#C8BCC3",
margin="xxl"
),
BoxComponent(
layout="vertical",
margin="xxl",
spacing="sm",
contents=[
BoxComponent(
layout="horizontal",
contents=[
TextComponent(
text = "飲料總杯數" ,
color="#555555",
flex=0,
wrap=True
),
TextComponent(
text = str(m),
color="#111111",
align="end",
wrap=True
),
]
),
BoxComponent(
layout="horizontal",
margin="xl",
contents=[
TextComponent(
text = "總價" ,
color="#C171BD",
flex=0,
size="xl",
wrap=True
),
TextComponent(
text = "$" + str(total),
color="#C171BD",
align="end",
size="xl",
wrap=True
),
]
)
]
),
]
),
)
messageA.append(FlexSendMessage(alt_text="結帳",contents=bubble))
messageA.append(TextSendMessage(text="我們會盡快幫您準備飲料,請您稍等片刻~"))
line_bot_api.reply_message(event.reply_token,messageA)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='結帳錯誤'))
手機上的樣子(❀╹◡╹)
這樣就做完結帳囉~(◍•ᴗ•◍)ゝ
下篇會介紹如何修改訂單,也是飲料機器人的最後一篇,敬請期待!