neurotransmitter.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, is_api_call=self.is_api_call)
  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. is_api_call=self.is_api_call,
  49. no_voice=self.no_voice)
  50. found = True
  51. break
  52. if not found: # the answer do not correspond to any answer. We run the default synapse
  53. self.run_synapse_by_name(self.default, high_priority=True, is_api_call=self.is_api_call)
  54. def _is_parameters_ok(self):
  55. """
  56. Check if received links are ok to perform operations
  57. :return: true if the neuron is well configured, raise an exception otherwise
  58. .. raises:: MissingParameterException, InvalidParameterException
  59. """
  60. # with the neuron the user has the choice of a direct link that call another synapse,
  61. # or a link with an answer caught from the STT engine
  62. # we cannot use at the same time a direct redirection and a link with question
  63. if self.direct_link is not None and self.from_answer_link is not None:
  64. raise InvalidParameterException("neurotransmitter cannot be used with both direct_link and from_answer_link")
  65. if self.direct_link is None and self.from_answer_link is None:
  66. raise MissingParameterException("neurotransmitter must be used with direct_link or from_answer_link")
  67. if self.from_answer_link is not None:
  68. if self.default is None:
  69. raise InvalidParameterException("default parameter is required and must contain a valid synapse name")
  70. for el in self.from_answer_link:
  71. if "synapse" not in el:
  72. raise MissingParameterException("Links must contain a synapse name: %s" % el)
  73. if "answers" not in el:
  74. raise MissingParameterException("Links must contain answers: %s" % el)
  75. return True