neurotransmitter.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import logging
  2. from kalliope.core.NeuronModule import NeuronModule, MissingParameterException, InvalidParameterException
  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 parameters
  9. self.from_answer_link = kwargs.get('from_answer_link', None)
  10. self.default = kwargs.get('default', None)
  11. self.direct_link = kwargs.get('direct_link', None)
  12. self.is_api_call = kwargs.get('is_api_call', False)
  13. self.answer = kwargs.get('answer', None)
  14. # do some check
  15. if self._is_parameters_ok():
  16. if self.direct_link is not None:
  17. logger.debug("Neurotransmitter directly call to the synapse name: %s" % self.direct_link)
  18. self.run_synapse_by_name(self.direct_link, high_priority=True)
  19. else:
  20. if self.is_api_call:
  21. if self.answer is not None:
  22. self.callback(self.answer)
  23. else:
  24. self.is_waiting_for_answer = True
  25. else:
  26. # the user is using a from_answer_link, we call the stt to get an audio
  27. self.get_audio_from_stt(callback=self.callback)
  28. def callback(self, audio):
  29. """
  30. The callback used by the STT module to get the linked synapse
  31. :param audio: the audio to play by STT
  32. """
  33. logger.debug("Neurotransmitter, receiver audio from STT: %s" % audio)
  34. # print self.links
  35. # set a bool to know if we have found a valid answer
  36. if audio is None:
  37. self.run_synapse_by_name(self.default, high_priority=True)
  38. else:
  39. found = False
  40. for el in self.from_answer_link:
  41. for answer in el["answers"]:
  42. if self.is_order_matching(audio, answer):
  43. logger.debug("Neurotransmitter: match answer: %s" % answer)
  44. self.run_synapse_by_name(synapse_name=el["synapse"],
  45. user_order=audio,
  46. synapse_order=answer,
  47. high_priority=True)
  48. found = True
  49. break
  50. if not found: # the answer do not correspond to any answer. We run the default synapse
  51. self.run_synapse_by_name(self.default, high_priority=True)
  52. def _is_parameters_ok(self):
  53. """
  54. Check if received links are ok to perform operations
  55. :return: true if the neuron is well configured, raise an exception otherwise
  56. .. raises:: MissingParameterException, InvalidParameterException
  57. """
  58. # with the neuron the user has the choice of a direct link that call another synapse,
  59. # or a link with an answer caught from the STT engine
  60. # we cannot use at the same time a direct redirection and a link with question
  61. if self.direct_link is not None and self.from_answer_link is not None:
  62. raise InvalidParameterException("neurotransmitter cannot be used with both direct_link and from_answer_link")
  63. if self.direct_link is None and self.from_answer_link is None:
  64. raise MissingParameterException("neurotransmitter must be used with direct_link or from_answer_link")
  65. if self.from_answer_link is not None:
  66. if self.default is None:
  67. raise InvalidParameterException("default parameter is required and must contain a valid synapse name")
  68. for el in self.from_answer_link:
  69. if "synapse" not in el:
  70. raise MissingParameterException("Links must contain a synapse name: %s" % el)
  71. if "answers" not in el:
  72. raise MissingParameterException("Links must contain answers: %s" % el)
  73. return True