0

I'm working on an Alexa skill with Flask-Ask. I want to log all intent calls and their arguments, and since intents are all conveniently annotated with the ask.intent decorator, I figured that I could either change the code of Flask-Ask to do the logging in the code of the decorator, or I could decorate myself, which is preferable to avoid problems with patching the library when deploying to production.

I've built the solution below, but the problem is that *args and **kwargs are always empty, instead of being passed on like on the most voted solution for this issue.

def tracked_intent(intent_name, mapping={}, convert={}, default={}):
    def decorator(f):
        @ask.intent(intent_name, mapping, convert, default)
        def new_func(*args, **kwargs):
            print('Got intent {} with arguments {} and {}'.format(intent_name, str(args), str(kwargs)))
            return f(*args, **kwargs)
        new_func.__name__ = f.__name__
        return new_func
    return decorator

Thus, when I have an intent call that requires an argument, I get something like TypeError: function() missing 2 required positional arguments: 'arg1' and 'arg2'

How can I fix my decorator to "extend" the ask.intent decorator while correctly forwarding the function arguments?

  • Did you manage to solve this? I have the same issue (needing to decorate a flask-ask view that receives arguments)... for your use case, you could probably get away with `app.before_request` [see docs](http://flask.readthedocs.io/en/latest/api/#flask.Flask.before_request) – farridav Feb 09 '18 at 06:36
  • hi @farridav! It seems app.before_request is indeed the best solution for this case, if one is sticking to flask-ask (which I decided to move away from). Unfortunately I didn't find a solution with decorators, and given some other implementation details, I decided to move to parsing the JSON requests and forming the JSON responses manually then. – Paulo Henrique Feb 12 '18 at 10:34

0 Answers0