21 """Holds and manipulates data sent over the websocket 23 Message objects will be used to send information back and forth 24 between processes of Mycroft. 27 type (str): type of data sent within the message. 28 data (dict): data sent within the message 29 context: info about the message not part of data such as source, 30 destination or domain. 33 def __init__(self, type, data=None, context=None):
34 """Used to construct a message object 36 Message objects will be used to send information back and fourth 37 bettween processes of mycroft service, voice, skill and cli 44 """This returns a string of the message info. 46 This makes it easy to send over a websocket. This uses 47 json dumps to generate the string with type, data and context 50 str: a json string representation of the message. 60 """This takes a string and constructs a message object. 62 This makes it easy to take strings from the websocket and create 63 a message object. This uses json loads to get the info and generate 67 value(str): This is the json string received from the websocket 70 Message: message object constructed from the json string passed 72 value(str): This is the string received from the websocket 74 obj = json.loads(value)
75 return Message(obj.get(
'type')
or '',
76 obj.get(
'data')
or {},
77 obj.get(
'context')
or {})
79 def reply(self, type, data=None, context=None):
80 """Construct a reply message for a given message 82 This will take the same parameters as a message object but use 83 the current message object as a reference. It will copy the context 84 from the existing message object and add any context passed in to 85 the function. Check for a target passed in to the function from 86 the data object and add that to the context as a target. If the 87 context has a client name then that will become the target in the 88 context. The new message will then have data passed in plus the 89 new context generated. 92 type (str): type of message 93 data (dict): data for message 94 context: intented context for new message 97 Message: Message object to be used on the reply to the message 100 context = context
or {}
104 new_context[key] = context[key]
106 new_context[
'target'] = data[
'target']
107 elif 'client_name' in context:
108 context[
'target'] = context[
'client_name']
109 return Message(type, data, context=new_context)
112 """Construct a response message for the message 114 Constructs a reply with the data and appends the expected 115 ".response" to the message 118 data (dict): message data 119 context (dict): message context 121 (Message) message with the type modified to match default response 123 response_message = self.
reply(self.
type, data
or {}, context)
124 response_message.type +=
'.response' 125 return response_message
129 Copy the original context and add passed in context. Delete 130 any target in the new context. Return a new message object with 131 passed in data and new context. Type remains unchanged. 134 type (str): type of message 135 data (dict): date to send with message 136 context: context added to existing context 139 Message: Message object to publish 141 context = context
or {}
142 new_context = self.context.copy()
144 new_context[key] = context[key]
146 if 'target' in new_context:
147 del new_context[
'target']
149 return Message(type, data, context=new_context)
153 For intents get the portion not consumed by Adapt. 155 For example: if they say 'Turn on the family room light' and there are 156 entity matches for "turn on" and "light", then it will leave behind 157 " the family room " which is then normalized to "family room". 160 str: Leftover words or None if not an utterance. 162 utt =
normalize(self.data.get(
"utterance",
""))
163 if utt
and "__tags__" in self.
data:
164 for token
in self.
data[
"__tags__"]:
166 utt = re.sub(
r'\b' + token.get(
"key",
"") +
r"\b",
"", utt)
def utterance_remainder(self)
def __init__(self, type, data=None, context=None)
def reply(self, type, data=None, context=None)
def publish(self, type, data, context=None)
def response(self, data=None, context=None)
def normalize(text, lang=None, remove_articles=True)