iT邦幫忙

2022 iThome 鐵人賽

DAY 25
0
自我挑戰組

30天建構出一個簡單 LineBot 機器人系列 第 25

DAY 25 專案介紹-4 (飲料訂購機器人實作-3)

  • 分享至 

  • xImage
  •  

上篇做出選擇飲料的冰塊、甜度、加什麼配料以及杯數還有購買確認,這篇會講結帳,下篇講修改訂單~

結帳

  • 確認完訂單無誤後按下結帳,就會出現結帳畫面。
  • view.py 設定當回傳事件是 F 時,回傳結帳 Flex Message。
  • 將回傳程式寫在 func.py

↓ view,py

#view,py
        elif event.postback.data[0:1] == "F": #結帳
            func.sendRECEIPT(event)

↓ func.py
設定 data 為 F 以讓程式回傳結帳。

除了想回傳結帳 Flex Message 外還想傳送一段話給消費者,因此這裡使用了多則訊息傳送(最多五則)。

  1. 設好一個空陣列以加入訊息。
    EX:messageA = []
  2. 之後在將信息 .append() 進入陣列。
    EX:messageA.append(FlexSendMessage(...))
    messageA.append(TextSendMessage(...))
#func.py
def sendRECEIPT(event):     
    try:
        messageA = []

利用Django語法拿取存取在資料庫使用者所訂購的飲料。

  • 拿取id最前面(小到大)的資料:"群組名".object.order_by('id').first()
  • 計算總共有多少條資訊:"群組名".object.order_by('id').count()
        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='結帳錯誤'))   

手機上的樣子(❀╹◡╹)


這樣就做完結帳囉~(◍•ᴗ•◍)ゝ

下篇會介紹如何修改訂單,也是飲料機器人的最後一篇,敬請期待!


上一篇
DAY 24 專案介紹-3 (飲料訂購機器人實作-2)
下一篇
DAY 26 專案介紹-5 (飲料訂購機器人實作-4)
系列文
30天建構出一個簡單 LineBot 機器人30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言