在下面的代码中,当 your_command
在 slack 中执行时调用 your_method
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
如何从这个 python 程序中的另一个函数调用这个 your_method
。
例如。
def print:
a = your_method('hello','world')
这给我错误=>
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)
最佳答案
根据签名,这个函数只接受关键字参数。
def your_method(**kwargs):
你用位置参数来调用它。
your_method('hello', 'world')
你要么需要更改签名
def your_method(*args, **kwargs)
或者换个说法
your_method(something='hello', something_else='world')
关于python - 如何调用由 post 请求调用的 python flask 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34924953/