-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.py
72 lines (56 loc) · 2.59 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
import json
class ActionCharge(Action):
def name(self) -> Text:
return "action_charge"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
# Opening JSON file
f = open('XingFuTangBobaList.json', encoding="utf8")
menus = json.load(f)
price1 = {}
for menu in menus:
drinkType = {}
if "options" in menu:
for temp in menu["options"]:
temperature = temp["temperature"]
price = temp["price"]
drinkType[temperature] = price
if not bool(drinkType):
price1[menu["zhName"]] = menu["price"]
else:
price1[menu["zhName"]] = drinkType
inputDrinkType = tracker.get_slot("type")
inputDrinkTemperature = tracker.get_slot("temperature")
inputAddOn = tracker.get_slot("addon")
inputConfirm = tracker.get_slot("respond")
inputDrinkType = inputDrinkType.replace(" ", "")
inputDrinkTemperature = inputDrinkTemperature.replace(" ", "")
inputAddOn = inputAddOn.replace(" ", "")
inputConfirm = inputConfirm.replace(" ", "")
if inputAddOn == "加奶":
addon = price1["加奶"]
elif inputAddOn == "加珍珠":
addon = price1["加珍珠"]
else:
addon = 0
if inputDrinkTemperature == "热" or inputDrinkTemperature == "热的":
inputDrinkTemperature = "hot"
try:
ttl_price = price1[inputDrinkType][inputDrinkTemperature] + addon
dispatcher.utter_message("一共 %s 令吉" % (ttl_price))
dispatcher.utter_message("谢谢您!")
except:
dispatcher.utter_message("亲!您的饮料只有冷饮,这里会自动帮你换成冷的哦~")
ttl_price = price1[inputDrinkType]["cold"] + addon
dispatcher.utter_message("一共 %s 令吉" % (ttl_price))
dispatcher.utter_message("谢谢您!")
elif inputDrinkTemperature == "冷" or inputDrinkTemperature == "冷的":
inputDrinkTemperature = "cold"
ttl_price = price1[inputDrinkType][inputDrinkTemperature] + addon
dispatcher.utter_message("一共 %s 令吉" % (ttl_price))
dispatcher.utter_message("谢谢您!")
return []