neurotransmitter.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import logging
  2. from core.NeuronModule import NeuronModule, MissingParameterException
  3. logging.basicConfig()
  4. logger = logging.getLogger("kalliope")
  5. class Neurotransmitter(NeuronModule):
  6. def __init__(self, **kwargs):
  7. super(Neurotransmitter, self).__init__(**kwargs)
  8. # get links
  9. self.links = kwargs.get('links', None)
  10. self.default = kwargs.get('default', None)
  11. # do some check
  12. if self._links_content_ok():
  13. # the brain seems fine, we call the stt to get an audio
  14. self.get_audio_from_stt(callback=self.callback)
  15. def callback(self, audio):
  16. logger.debug("Neurotransmitter, receiver audio from STT: %s" % audio)
  17. # print self.links
  18. # set a bool to know if we have found a valid answer
  19. found = False
  20. for el in self.links:
  21. if audio in el["answers"]:
  22. found = True
  23. self.run_synapse_ny_name(el["synapse"])
  24. # we don't need to check to rest of answer
  25. break
  26. if not found:
  27. # the answer do not correspond to any answer. We run the default synapse
  28. self.run_synapse_ny_name(self.default)
  29. def _links_content_ok(self):
  30. """
  31. Check the content of the links parameter
  32. :return:
  33. """
  34. if self.links is None:
  35. raise MissingParameterException("links parameter required and must contain at least one link")
  36. if self.default is None:
  37. raise MissingParameterException("default parameter is required and must contain a valid synapse name")
  38. for el in self.links:
  39. if "synapse" not in el:
  40. raise MissingParameterException("Links must contain a synapse name: %s" % el)
  41. if "answers" not in el:
  42. raise MissingParameterException("Links must contain answers: %s" % el)
  43. return True