Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Enhanced SiriPlugin.ask to streamline the creation of conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
wrboyce committed Nov 24, 2011
1 parent 8a7eae8 commit b2cf07e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
16 changes: 14 additions & 2 deletions sirious/plugins/__init__.py
@@ -1,3 +1,5 @@
from pydispatch import dispatcher

from sirious import SiriObjects


Expand All @@ -11,8 +13,18 @@ def respond(self, text, speakableText=None, dialogueIdentifier='Misc#ident', lis
root.views.append(SiriObjects.Utterance(text=text, speakableText=speakableText, dialogueIdentifier=dialogueIdentifier, listenAfterSpeaking=listenAfterSpeaking))
self.proxy.inject_plist(root.to_dict())

def ask(self, text, speakableText=None, dialogueIdentifier='Misc#ident', listenAfterSpeaking=True):
self.respond(text, speakableText, dialogueIdentifier, listenAfterSpeaking)
def ask(self, handler, text, speakableText=None, dialogueIdentifier='Misc#ident'):
self.respond(text, speakableText, dialogueIdentifier, listenAfterSpeaking=True)
self.proxy.blocking = 1
def handle_answer(*a, **kw):
## if the question caused unknown intent, we'll get told about it twice
if kw['plist']['class'] == 'AddViews':
return
del(kw['sender'])
del(kw['signal'])
handler(*a, **kw)
dispatcher.disconnect(handle_answer, signal='consume_phrase')
dispatcher.connect(handle_answer, signal='consume_phrase')

def plist_from_server(self, plist):
return plist
Expand Down
7 changes: 7 additions & 0 deletions sirious/plugins/sirious_test.py
Expand Up @@ -5,3 +5,10 @@ class SiriousTest(SiriPlugin):
def sirious(self, phrase, plist):
self.respond("You're damn right I am. Sirious is up and running!")
sirious.triggers = ['are you serious']

def ask_test(self, phrase, plist):
self.ask(self.ask_test_response, "Do you think this is a test?")
ask_test.triggers = ['is this a test']

def ask_test_response(self, phrase, plist):
self.respond("Well, you've failed.")
33 changes: 25 additions & 8 deletions sirious/proxy.py
Expand Up @@ -4,6 +4,7 @@
import zlib

from biplist import readPlistFromString, writePlistToString
from pydispatch import dispatcher
from twisted.internet import protocol, reactor, ssl
from twisted.protocols.basic import LineReceiver
from twisted.protocols.portforward import ProxyClientFactory
Expand Down Expand Up @@ -50,14 +51,26 @@ def rawDataReceived(self, data):
plist = readPlistFromString(body)
plist = self.process_plist(plist)
if plist:
block = False
if self.blocking and self.ref_id != plist['refId']:
self.blocking = False
if not self.blocking:
if isinstance(self.blocking, bool) and self.blocking:
block = True
if isinstance(self.blocking, int) and self.blocking > 0:
self.blocking -= 1
block = True
if not block:
self.inject_plist(plist)
pass
else:
print "!", plist['class'], self.blocking
return plist

def process_plist(self, plist):
direction = '>' if self.__class__ == SiriProxyServer else '<'
print direction, plist['class'], plist.get('refId', None)
#from pprint import pprint
#pprint(plist)
#print
## Offer plugins a chance to intercept plists
for plugin in self.plugins:
plugin.proxy = self
Expand Down Expand Up @@ -106,18 +119,22 @@ def process_speech(self, plist):
phrase = plist['properties']['views'][1]['properties']['commands'][0]['properties']['commands'][0]['properties']['utterance'].split('^')[3]
if plist['class'] == 'SpeechRecognized':
phrase = ''
for phrase_plistect in plist['properties']['recognition']['properties']['phrases']:
for token in phrase_plistect['properties']['interpretations'][0]['properties']['tokens']:
for phrase_plist in plist['properties']['recognition']['properties']['phrases']:
for token in phrase_plist['properties']['interpretations'][0]['properties']['tokens']:
if token['properties']['removeSpaceBefore']:
phrase = phrase[:-1]
phrase += token['properties']['text']
if not token['properties']['removeSpaceAfter']:
phrase += ' '
if phrase:
#print '[Speech Recognised] %s' % phrase
for trigger, function in self.triggers:
if trigger.search(phrase):
function(phrase, plist)
#print '[Speech Recognised (%s)] %s' % (plist['class'], phrase)
try:
dispatcher.getAllReceivers(signal='consume_phrase').next()
dispatcher.send('consume_phrase', phrase=phrase, plist=plist)
except StopIteration:
for trigger, function in self.triggers:
if trigger.search(phrase):
function(phrase, plist)


class SiriProxyClientFactory(ProxyClientFactory):
Expand Down

0 comments on commit b2cf07e

Please sign in to comment.