Skip to content

Instantly share code, notes, and snippets.

@jshep321
Last active December 2, 2019 21:47
Show Gist options
  • Save jshep321/3b13f105ffb8b9eace9fa57f03fed6d7 to your computer and use it in GitHub Desktop.
Save jshep321/3b13f105ffb8b9eace9fa57f03fed6d7 to your computer and use it in GitHub Desktop.
send a remote "utterance" (voice command) to mycroft as if spoken locally
#! /usr/bin/env python
# Purpose: Sends an utterance to mycroft as if spoken
# Usage: python sendutt.py "utterance string goes here"
# Example usage from command line: python sendutt.py "what time is it"
#example data format--> data ='{"utterances": ["what time is it"],"lang":"en-us"}'
#example message format--> message = '{"type": "' + type + '", "data": ' + data +'}'
#example fully spelled out websocket message-->
# result = ws.send('{"type": "'"recognizer_loop:utterance"'", "data": ''{"utterances": ["what time is it"],"lang":"en-us"}''}')
IP="127.0.0.1" #IP of mycroft, 127.0.0.1 for on-device usage should work
type="recognizer_loop:utterance"
import sys
from websocket import create_connection
####Create message####
uri = 'ws://' + IP + ':8181/core'
utterance = sys.argv[1]
data = '{"utterances": [\"'+utterance+'\"],"lang":"en-us"}'
message = '{"type": "' + type + '", "data": ' + data +'}'
####Send message & receive response####
ws = create_connection(uri)
print ("sending message: "+ message+" to "+uri)
result = ws.send(message)
print ("Receiving...")
result = ws.recv()
print ("Received '%s'" % result)
ws.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment