send.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 import sys
16 import json
17 from mycroft.messagebus.client.ws import WebsocketClient
18 from mycroft.messagebus.message import Message
19 from mycroft.configuration import ConfigurationManager
20 from websocket import create_connection
21 
22 
23 def main():
24  """
25  Main function, will run if executed from command line.
26 
27  Sends parameters from commandline.
28 
29  Param 1: message string
30  Param 2: data (json string)
31  """
32  # Parse the command line
33  if len(sys.argv) == 2:
34  messageToSend = sys.argv[1]
35  dataToSend = {}
36  elif len(sys.argv) == 3:
37  messageToSend = sys.argv[1]
38  try:
39  dataToSend = json.loads(sys.argv[2])
40  except BaseException:
41  print("Second argument must be a JSON string")
42  print("Ex: python -m mycroft.messagebus.send speak "
43  "'{\"utterance\" : \"hello\"}'")
44  exit()
45  else:
46  print("Command line interface to the mycroft-core messagebus.")
47  print("Usage: python -m mycroft.messagebus.send message")
48  print(" python -m mycroft.messagebus.send message JSON-string\n")
49  print("Examples: python -m mycroft.messagebus.send system.wifi.setup")
50  print("Ex: python -m mycroft.messagebus.send speak "
51  "'{\"utterance\" : \"hello\"}'")
52  exit()
53 
54  send(messageToSend, dataToSend)
55 
56 
57 def send(messageToSend, dataToSend=None):
58  """
59  Send a single message over the websocket.
60 
61  Args:
62  messageToSend (str): Message to send
63  dataToSend (dict): data structure to go along with the
64  message, defaults to empty dict.
65  """
66  dataToSend = dataToSend or {}
67 
68  # Calculate the standard Mycroft messagebus websocket address
69  config = ConfigurationManager.get().get("websocket")
70  url = WebsocketClient.build_url(config.get("host"),
71  config.get("port"),
72  config.get("route"),
73  config.get("ssl"))
74 
75  # Send the provided message/data
76  ws = create_connection(url)
77  packet = Message(messageToSend, dataToSend).serialize()
78  ws.send(packet)
79  ws.close()
80 
81 
82 if __name__ == '__main__':
83  try:
84  main()
85  except IOError:
86  print('Could not connect to websocket, no message sent')
def send(messageToSend, dataToSend=None)
Definition: send.py:57
def get(phrase, lang=None, context=None)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40