Skip to content

Decorators

Psycho edited this page Oct 10, 2019 · 9 revisions

We do create our decorators and more will be added with time. These decorators are available by importing core.commons.commons

@deprecated

Marks a method as deprecated, automatically printing a warning if it is being used. Deprecation means the method used might disappear in a new coming version of Alice

@online

online(text: str = '', offlineHandler: Callable = None)

This decorator can be used (with or or without parameters) to define a function that requires ethernet. In the Default mode without arguments shown in the example it will either execute whats in the function or when alice is offline return a random offline answer. Using the parameter text:

 @online(text=<myText>)

a own text can be used when being offline aswell and using the parameter offlineHandler:

 @online(offlineHandler=<myFunc>)

a own offline handler can be called, which is especially helpful when the intent wants to use endDialog, continueDialog ... inside the decorated function -> does not return the text

Examples: An intent that requires ethernet can be defined the following ways:

 def onMessage(self, intent: str, session: DialogSession) -> bool:
   if not self.filterIntent(intent, session):
     return False

   if intent == self._INTENT_EXAMPLE:
     self.endDialog(sessionId=session.sessionId, text=self.exampleIntent())
   elif intent == self._INTENT_EXAMPLE_2:
     self.endDialog(sessionId=session.sessionId, text=self.exampleIntent2())
   elif intent == self._INTENT_EXAMPLE_3:
     self.exampleIntent2(session)

  return True

 @online
 def exampleIntent(self) -> str:
   return requests.get('http://api.open-notify.org').text

 @online(text='offlineText')
 def exampleIntent2(self) -> str:
   return requests.get('http://api.open-notify.org').text

 def offlineHandler(self, session: DialogSession):
   self.endDialog(sessionId=session.sessionId, text='offlineText')

 @online(text='offlineText')
 def exampleIntent2(self, session: DialogSession):
   request = requests.get('http://api.open-notify.org')
   self.endDialog(sessionId=session.sessionId, text=request.text)

If your module requires internet connectivity you can additionally mark it as online in the install file, so it can not be installed when the setting "stayCompletlyOffline" in alice config file is set to True