這篇是修改訂單,也是飲料機器人最後一篇!
有時候選錯想要的東西卻無法修改時會感覺這種設計不用心,因此加入了基礎修改訂單的功能,並且 Flex message header 的顏色都設為紫色,暗示使用者現在在修改訂單階段。
在修改項目的程式邏輯編編是這樣思考的。
↓ view,py
#view,py
elif event.postback.data[0:1] == "H": #修改哪一項訂單
func.sendback_which(event, backdata)
↓ func,py
#func,py
def sendback_which(event, backdata):
try:
bubble = BubbleContainer(
direction='ltr',
header = BoxComponent(
layout='vertical',
background_color='#DBD3D8',
contents=[
TextComponent(
text = "請問要修改此訂單的哪個項",
size="md",
weight="bold",
),
]
),
body = BoxComponent(
layout='vertical',
spacing='xs',
contents=[
ButtonComponent(
style='secondary',
color="#E8F1E4",
action=PostbackTemplateAction(
label='飲料品項',
text='要修改飲料品項',
data='M1&'
)
),
ButtonComponent(
style='secondary',
color="#E8F1E4",
action=PostbackTemplateAction(
label='數量',
text='要修改數量',
data='M2&'
)
),
ButtonComponent(
style='secondary',
color="#E8F1E4",
action=PostbackTemplateAction(
label='糖度',
text='要修改糖度',
data='M3&'
)
),
ButtonComponent(
style='secondary',
color="#E8F1E4",
action=PostbackTemplateAction(
label='冰塊',
text='要修改冰塊',
data='M4&'
)
),
ButtonComponent(
style='secondary',
color="#E8F1E4",
action=PostbackTemplateAction(
label='加料',
text='要修改加料',
data='M5&'
)
),
]
),
)
message = FlexSendMessage(alt_text="修改哪項訂單",contents=bubble)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='不'))
手機上的樣子(❀╹◡╹)
為免此篇太冗贅因此以修改飲料以及加料為範例,其餘程式修改方式差不多。
↓ view,py
#view,py
elif event.postback.data[0:2] == "M1": #修改飲料
func.sendModifyButton(event, backdata)
↓ func,py
內容除了 data 外基本跟一開始點擊我要點餐出來的 Flex message 一樣。
#func,py
def sendModifyButton(event, backdata):
try:
message = TemplateSendMessage(
alt_text='Buttons template',
template = ButtonsTemplate(
thumbnail_image_url="https://raw.githubusercontent.com/shakuneko/pic0412/main/%E6%88%91%E8%A6%81%E8%8F%9C%E5%96%AE.JPG",
title='Menu',
text='請問要哪個種類的飲料',
actions=[
PostbackTemplateAction(
label='啪噠原茶',
text='啪噠原茶',
data='action=M啪噠原茶'
),
PostbackTemplateAction(
label='精靈純鮮奶茶',
text='精靈純鮮奶茶',
data='action=M精靈純鮮奶茶'
),
PostbackTemplateAction(
label='啪噠奶茶',
text='啪噠奶茶',
data='action=M啪噠奶茶'
)
]
)
)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='不'))
之後根據選擇的種類回傳對應的飲料項目。
手機上的樣子(❀╹◡╹)
↓ view,py
#view,py
elif backdata.get('action') == 'M啪噠原茶':
func.sendback_Modifyoriginal(event, backdata)
elif backdata.get('action') == 'M精靈純鮮奶茶':
func.sendback_Modifypure(event, backdata)
elif backdata.get('action') == 'M啪噠奶茶':
func.sendback_Modifymilktea(event, backdata)
啪噠原茶↓ func,py
內容除了 data 外基本跟一開始點擊我要點餐後回傳的的 Flex message 一樣,這裡一樣用啪噠原茶作範例。
#func,py
def sendback_Modifyoriginal(event, backdata):
try:
flex_message = FlexSendMessage(
alt_text='啪噠原茶',
contents={ #就把JSON貼過來吧
"type": "carousel",
"contents": [
{
'type': 'bubble',
'direction': 'ltr',
"header": {
"type": "box",
"layout": "vertical",
"backgroundColor": "#DBD3D8",
"contents": [
{
"type": "text",
"text": "啪噠紅茶",
"weight": "bold",
"margin": "sm",
"size": "md",
"wrap": True
}
]
},
'hero': {
'type': 'image',
'url': 'https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%A3%B2%E6%96%99/%E5%95%AA%E5%99%A0%E7%B4%85%E8%8C%B6.JPG',
'size': 'full',
'aspectRatio': '1:1',
'aspectMode': 'cover',
},
'body':{
"type": "box",
"layout": "vertical",
"spacing":"sm",
"contents": [
{
"type": "button",
"style": "secondary",
"color":"#F6DCCB",
"action": {
'type': 'postback',
'label':'M 啪噠紅茶',
'text':'改成 M啪噠紅茶',
'data':'do'
}
},
{
"type": "button",
"style": "secondary",
"color":"#C4DABB",
"action": {
'type': 'postback',
'label':'L 啪噠紅茶',
'text':'改成 L啪噠紅茶',
'data':'do'
}
},
]
}
},
{
'type': 'bubble',
'direction': 'ltr',
"header": {
"type": "box",
"backgroundColor": "#DBD3D8",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "四季烏龍",
#"color":"#9A8492",
"weight": "bold",
"margin": "sm",
"size": "md",
"wrap": True
}
]
},
'hero': {
'type': 'image',
'url': 'https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%A3%B2%E6%96%99/%E5%9B%9B%E5%AD%A3%E7%83%8F%E9%BE%8D.JPG',
'size': 'full',
'aspectRatio': '1:1',
'aspectMode': 'cover',
},
'body':{
"type": "box",
"layout": "vertical",
"spacing":"sm",
"contents": [
{
"type": "button",
"style": "secondary",
"color":"#F6DCCB",
"action": {
'type': 'postback',
'label':'M 四季烏龍',
'text':'改成 M四季烏龍',
'data':'do'
}
},
{
"type": "button",
"style": "secondary",
"color":"#C4DABB",
"action": {
'type': 'postback',
'label':'L 四季烏龍',
'text':'改成 L四季烏龍',
'data':'do'
}
},
]
}
},
{
'type': 'bubble',
'direction': 'ltr',
"header": {
"type": "box",
"backgroundColor": "#DBD3D8",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "啪噠普洱茶",
#"color":"#9A8492",
"weight": "bold",
"margin": "sm",
"size": "md",
"wrap": True
}
]
},
'hero': {
'type': 'image',
'url': 'https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%A3%B2%E6%96%99/%E5%95%AA%E5%99%A0%E6%99%AE%E6%B4%B1%E8%8C%B6.JPG',
'size': 'full',
'aspectRatio': '1:1',
'aspectMode': 'cover',
},
'body':{
"type": "box",
"layout": "vertical",
"spacing":"sm",
"contents": [
{
"type": "button",
"style": "secondary",
"color":"#F6DCCB",
"action": {
'type': 'postback',
'label':'M 啪噠普洱茶',
'text':'改成 M啪噠普洱茶',
'data':'do'
}
},
{
"type": "button",
"style": "secondary",
"color":"#C4DABB",
"action": {
'type': 'postback',
'label':'L 啪噠普洱茶',
'text':'改成 L啪噠普洱茶',
'data':'do'
}
},
]
}
},
{
'type': 'bubble',
'direction': 'ltr',
"header": {
"backgroundColor": "#DBD3D8",
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "啪噠焙茶",
#"color":"#9A8492",
"weight": "bold",
"margin": "sm",
"size": "md",
"wrap": True
}
]
},
'hero': {
'type': 'image',
'url': 'https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%A3%B2%E6%96%99/%E5%95%AA%E5%99%A0%E7%84%99%E8%8C%B6.JPG',
'size': 'full',
'aspectRatio': '1:1',
'aspectMode': 'cover',
},
'body':{
"type": "box",
"layout": "vertical",
"spacing":"sm",
"contents": [
{
"type": "button",
"style": "secondary",
"color":"#F6DCCB",
"action": {
'type': 'postback',
'label':'M 啪噠焙茶',
'text':'改成 M啪噠焙茶',
'data':'do'
}
},
{
"type": "button",
"style": "secondary",
"color":"#C4DABB",
"action": {
'type': 'postback',
'label':'L 啪噠焙茶',
'text':'改成 L啪噠焙茶',
'data':'do'
}
},
]
}
},
]
}
)
line_bot_api.reply_message(event.reply_token, flex_message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='修改啪噠原茶失敗'))
手機上的樣子(❀╹◡╹)
↓ view,py
#view,py
elif event.postback.data[0:2] == "M5": #修改加料
func.sendback_Modifyadd(event, backdata)
↓ func,py
內容除了 data 外基本跟加料選擇的 Flex message 一樣。
#func,py
def sendback_Modifyadd(event, backdata):
try:
bubble = BubbleContainer(
direction='ltr',
header=BoxComponent(
layout='vertical',
background_color='#DBD3D8',
contents=[
TextComponent(
text="加料 +10,只能選一種!",
size="md",
weight="bold",
margin="sm"
)
]
),
body = BoxComponent(
layout='vertical',
margin="sm",
contents=[
BoxComponent(
layout='horizontal',
spacing='xs',
contents=[
ImageComponent(
url="https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%85%8D%E6%96%99/%E9%BB%91%E7%B3%96%E7%8F%8D%E7%8F%A0.JPG",
size="lg",
aspect_mode="cover",
action=PostbackTemplateAction(
label='黑糖珍珠',
text='改成 黑糖珍珠',
data='aa'
)
),
ImageComponent(
url="https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%85%8D%E6%96%99/%E6%A4%B0%E6%9E%9C.JPG",
size="lg",
aspect_mode="cover",
action=PostbackTemplateAction(
label='椰果',
text='改成 椰果',
data='aa'
)
),
ImageComponent(
url="https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%85%8D%E6%96%99/%E5%B0%8F%E8%8A%8B%E5%9C%93.JPG",
size="lg",
aspect_mode="cover",
action=PostbackTemplateAction(
label='小芋圓',
text='改成 小芋圓',
data='aa'
)
),
]
),
BoxComponent(
layout='horizontal',
spacing='xs',
contents=[
ImageComponent(
url="https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%85%8D%E6%96%99/%E8%9C%82%E8%9C%9C%E7%99%BD%E7%8E%89.JPG",
size="lg",
aspect_mode="cover",
action=PostbackTemplateAction(
label='蜂蜜白玉',
text='改成 蜂蜜白玉',
data='aa'
)
),
ImageComponent(
url="https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%85%8D%E6%96%99/%E4%BB%99%E8%8D%89.JPG",
size="lg",
aspect_mode="cover",
action=PostbackTemplateAction(
label='仙草',
text='改成 仙草',
data='aa'
)
),
ImageComponent(
url="https://raw.githubusercontent.com/shakuneko/pic0412/main/%E9%85%8D%E6%96%99/%E5%B8%83%E4%B8%81.JPG",
size="lg",
aspect_mode="cover",
action=PostbackTemplateAction(
label='布丁',
text='改成 布丁',
data='aa'
)
),
]
),
]
),
footer=BoxComponent(
layout='horizontal',
spacing='xs',
contents=[
ButtonComponent(
style='secondary',
color="#C4DABB",
action=PostbackTemplateAction(
label='不用加料',
text='改成 不用加料',
data='aa'
)
)
]
)
)
message = FlexSendMessage(alt_text="修改加料",contents=bubble)
line_bot_api.reply_message(event.reply_token, message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='不add'))
手機上的樣子(❀╹◡╹)
避免使用者誤打然後些改了非所設定好的選項,因此這裡會先判斷是否為設定的選項再修改資料庫上的訂單。
↓ view,py
#view,py
elif mtext[:2] =="改成":
stuff = mtext[3:] #拿取修改哪項項目的名稱,EX:飲料
func.sendback_modifyConfirm(event,stuff)
為了要判斷是否為所設定好的選項,就必須讓程式知道有什麼選項,因此在 func.py 最上面要再加上資料陣列。
↓ func,py
iceMenu = ['正常','少冰','微冰','去冰','常溫']
sugerMenu = ['全糖','少糖','半糖','微糖','無糖']
amountMenu = ['1','2','3','4','5','6','7','8','9']
addMenu = ['黑糖珍珠','蜂蜜白玉','仙草','小芋園','椰果','布丁','不用加料']
利用 Django 語法拿取待修改資料。
#func,py
def sendback_modifyConfirm(event,stuff): #點完加料
try:
p = order.objects.order_by('id').last()
if stuff in menu:
p.drink = stuff
p.price = menu[stuff]
p.save()
whichOne = "飲料品項"
elif stuff in iceMenu:
p.ice = stuff
p.save()
whichOne = "冰塊"
elif stuff in sugerMenu:
p.suger = stuff
p.save()
whichOne = "糖度"
elif stuff[:1] in amountMenu:
stuff = stuff[:1]
p.amount = stuff
p.save()
whichOne = "數量"
elif stuff in addMenu:
p.add = stuff
p.save()
whichOne = "加料"
drink = p.drink
ice = p.ice
suger = p.suger
add = p.add
amount = p.amount
if add == "不用加料": #判斷價格
p.price = menu[drink]
p.save()
else:
p.price = menu_e[drink]
p.save()
price = p.price
bubble = BubbleContainer(
direction='ltr',
header = BoxComponent(
layout='vertical',
background_color='#DBD3D8',
contents=[
TextComponent(
text = "確認修改訂單",
size="md",
weight="bold",
),
]
),
body = BoxComponent(
layout='vertical',
contents=[
TextComponent(
text = "\n修改 " + whichOne + " 為: " +stuff,
weight="bold",
wrap=True
),
TextComponent(
text = "\n"
),
TextComponent(
text = "\n修改後訂單全貌:"
),
TextComponent(
text = "\n飲料品項:" + drink
),
TextComponent(
text = "\n數量:" + amount
),
TextComponent(
text = "\n冰塊:" + ice
),
TextComponent(
text = "\n糖度:" + suger
),
TextComponent(
text = "\n加料:" + add
),
TextComponent(
text = " 加料 + 10 元喔!",
color="#C8BCC3",
size="xs",
margin='xs'
),
TextComponent(
text = "\n一杯單價:" + price
),
TextComponent(
text = "\n"
),
TextComponent(
text = "\n請問要結束購買嗎",
weight="bold",
),
]
),
footer=BoxComponent(
layout='vertical',
spacing='xs',
contents=[
BoxComponent(
layout='horizontal',
spacing='xs',
contents=[
ButtonComponent(
style='secondary',
color="#E8F1E4",
action=PostbackTemplateAction(
label='修改此筆訂單',
text='修改此筆訂單',
data='H'
)
),
ButtonComponent(
style='secondary',
color="#C4DABB",
action=PostbackTemplateAction(
label='繼續購物',
text='繼續購物',
data='G'
)
)
]
),
ButtonComponent(
style='secondary',
color="#F6DCCB",
action=PostbackTemplateAction(
label='結帳',
text='結帳',
data='F'
)
)
]
)
)
message = FlexSendMessage(alt_text="修改後訂單",contents=bubble)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='修改失敗'))
最後結帳,就完成點餐囉!
手機上的樣子(❀╹◡╹)
進入 http://127.0.0.1:8000/admin ,輸入完帳密後就可以進去資料庫。
點擊進入透過 models.py 跟 admin.py 設定的 order ,就可以看到剛剛所選的飲料~
這樣就完成飲料機器人囉,雖然程式並沒有很厲害,可能還有些累贅,也不能多個使用者用,但是有做出基本功能仍舊讓編編們有點開心><,希望未來會越來越進步!
⁽⁽ ◟(∗ ˊωˋ ∗)◞ ⁾⁾