python - Not able to decode slack's response to my request URL on button press, ValueError in json.loads -
i working slack's api , sent message user
{ "text": "would play game?", "attachments": [ { "text": "choose game play", "fallback": "you unable choose game", "callback_id": "wopr_game", "color": "#3aa3e3", "attachment_type": "default", "actions": [ { "name": "game", "text": "chess", "type": "button", "value": "chess" }, { "name": "game", "text": "falken's maze", "type": "button", "value": "maze" }, { "name": "game", "text": "thermonuclear war", "style": "danger", "type": "button", "value": "war", "confirm": { "title": "are sure?", "text": "wouldn't prefer game of chess?", "ok_text": "yes", "dismiss_text": "no" } } ] } ] }
and when button pressed user, slack sent request python backend. trying read this:
json.loads(self.request.body.decode('utf-8'))
but error coming. here traceback(updated):
traceback (most recent call last): file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/core/handlers/exception.py", line 39, in inner response = get_response(request) file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response response = self._get_response(request) file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/utils/decorators.py", line 67, in _wrapper return bound_func(*args, **kwargs) file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/utils/decorators.py", line 63, in bound_func return func.__get__(self, type(self))(*args2, **kwargs2) file "/users/akash/dropbox/work/miral/eb_miral/botlogic/listeners/slack.py", line 50, in dispatch return generic.view.dispatch(self, request, *args, **kwargs) file "/users/akash/virtualenvs/miral_env/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch return handler(request, *args, **kwargs) file "/users/akash/dropbox/work/miral/eb_miral/botlogic/listeners/slack.py", line 58, in post incoming = json.loads(self.request.body.decode('utf-8')) file "/library/frameworks/python.framework/versions/3.4/lib/python3.4/json/__init__.py", line 318, in loads return _default_decoder.decode(s) file "/library/frameworks/python.framework/versions/3.4/lib/python3.4/json/decoder.py", line 343, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) file "/library/frameworks/python.framework/versions/3.4/lib/python3.4/json/decoder.py", line 361, in raw_decode raise valueerror(errmsg("expecting value", s, err.value)) none valueerror: expecting value: line 1 column 1 (char 0)
i not able understand went wrong in this? works in case when user sends normal messages not in case of button press.
update: using django , if this:
payload = request.post.get('payload', none) incoming = json.loads(payload)
then works. miss out on important 'keys' other 'payload' slack must have returned , not generic solution. handling incoming requests in same server whether have 'payload' or not.
Comments
Post a Comment