Skip to content

Instantly share code, notes, and snippets.

@Kaljurand
Created August 3, 2017 23:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Kaljurand/df635ed92f1c6fb5c3a91231d13d4ade to your computer and use it in GitHub Desktop.
Simple web server that allows talking to Mycroft via HTTP queries like "curl http://192.168.0.23:8000?q=tell+me+a+joke" and getting a JSON response
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Simple web server that allows talking to Mycroft via HTTP queries like
# curl http://192.168.0.23:8000?q=tell+me+a+joke
# The response is a JSON object. It could be something general or something
# caller specific. (The examples are callbacks for K6nele.)
#
# Installation:
# 1. Allow access to the HTTP port: sudo ufw allow 8000
# 2. Install https://github.com/MycroftAI/enclosure-picroft/blob/master/home/pi/say_command.py
# to /home/pi/my/say_command.py
# 3. Start this script: python my-server.py
from __future__ import division, unicode_literals, print_function
from threading import Thread
import sys
import json
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import urlparse
from subprocess import call
# TODO: support multiple localizable and user-provided response templates
RESPONSE_1_1 = {
"component": "ee.ioc.phon.android.speak/.activity.SpeechActionActivity",
"extras": {
"android.speech.extra.PROMPT": "OK. Anything else?",
"android.speech.extra.MAX_RESULTS": 1,
"android.speech.extra.LANGUAGE": "en",
"ee.ioc.phon.android.extra.RESULT_UTTERANCE": "(.+)",
"ee.ioc.phon.android.extra.RESULT_COMMAND": "activity",
"ee.ioc.phon.android.extra.RESULT_ARG1": json.dumps({
"component": "ee.ioc.phon.android.speak/.activity.FetchUrlActivity",
"data": "http://192.168.0.23:8000/?q=$1",
"extras": {"ee.ioc.phon.android.extra.RESULT_LAUNCH_AS_ACTIVITY": True}
})
}
}
RESPONSE_1_2 = { "code": "ERROR" }
def say(text):
"""Says the given text to Mycroft.
Just calls:
https://github.com/MycroftAI/enclosure-picroft/blob/master/home/pi/say_command.py
TODO: be faster, specify the input language, return Mycroft's response, solve it without an external script, ...
"""
call(['/usr/local/bin/python', '/home/pi/my/say_command.py', str(text)])
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
def do_GET(self):
self._set_headers()
o = urlparse.urlparse(self.path)
d = urlparse.parse_qs(o.query)
q = d.get('q', None)
# TODO: get language
# TODO: get response template(s)
if q:
print('Saying: {0}'.format(q))
thread = Thread(target=say, args=(q))
thread.daemon = True
thread.start()
self.wfile.write(json.dumps(RESPONSE_1_1))
else:
self.wfile.write(json.dumps(RESPONSE_1_2))
def run(server_class=HTTPServer, handler_class=S, port=8000):
print('Starting on port ' + str(port))
server_address = ('', port)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment