The Python ask-sdk for writing alexa skills provides two ways to write intent handlers. One by deriving from AbstractRequestHandler
and implementing the two functions can_handle()
and handle()
. And another one using a function decorator (@SkillBuilder.request_handler()
).
Using the second way with the decorator I am not able to call the handler functions directly (in unit tests). Trying to access the function the interpreter shows the error TypeError: 'NoneType' object is not callable
.
The following is a minimal example of the intent handler as well as the testing code (which works similar to the suggestion at this github issue).
Intent handler
sb = SkillBuilder()
# Built-in Intent Handlers
@sb.request_handler(can_handle_func=is_request_type("LaunchRequest"))
def test_intent_handler(handler_input):
return "Hello, test handler"
Test function
def test_intent():
request = new_request('TestIntent', {})
# THE FOLLOWING LINE THROWS THE ERROR
response = test_intent_handler(request)
expected_response = "Hello, test handler"
assert response == expected_response
According to the answers to this question, the decorator function has to return a function, but this seems to be the case for request_handler()
already as you can see on github
The decorator function does return a wrapper function, so test_intent_handler
should be a function type. What am I missing?
EDIT
The answer of Adam Smith is a good workaround for this problem.
The reason that this happens is that the function SkillBuilder.request_handler
returns a wrapper function which does not return anything. This wrapper function is used as the decorator for the handler function. Since the result of the decorator is assigned to test_intent_handler
and the decorator (wrapper) does not return anything, the result is NoneType.
So after decorating the handler with @sb.request_handler
the original function is not accessible anymore.
To solve this the wrapper function just needs to return the passed-in handler function. Following Adam Smith's suggestion I created a pull request to change that, so that the "Alexa Skills Kit SDK for Python" can become more testable.