Search code examples
pythonpython-2.7flaskslack-apislack

How to call a python flask function which is call by a post request?


In below code, your_method is called when your_command is executed in slack

from flask_slack import Slack
slack = Slack(app)
app.add_url_rule('/', view_func=slack.dispatch)
@slack.command('your_command', token='your_token',
           team_id='your_team_id', methods=['POST'])
def your_method(**kwargs):
 text = kwargs.get('text')
 return text

How to call this your_method from another function in this python program.

Eg.

def print:
 a = your_method('hello','world')

This gives me error =>

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
return func(**kwargs)
File "sample.py", line 197
a = your_method('hello','world')
TypeError: your_method() takes exactly 0 arguments (1 given)

Solution

  • Based on the signature, tis function only accepts keyword arguments.

    def your_method(**kwargs):
    

    You call it with positional arguments.

    your_method('hello', 'world')
    

    You either need to change the signature

    def your_method(*args, **kwargs)
    

    or call it differently

    your_method(something='hello', something_else='world')