简体   繁体   中英

How to parse/access json from slack action using ruby on rails

I am connecting to the slack API successfully using the subscription events which sends a response which looks like this:

Parameters: {"token"=>"[FILTERED]", "team_id"=>"xxx", "api_app_id"=>"yyy", "event"=>{"bot_id"=>"zzz",............

I can parse that info using this:

json_params = JSON.parse(request.raw_post)
user = json_params['event']['bot_id']

Now to the problem. I am then adding action buttons, which makes a HTML post response to my URL but the call is in a different format than the subscription event post and JSON.parse is not working.

The format looks like this:

Parameters: {"payload"=>"{\"type\":\"block_actions\",\"user\":{\"id\":\"xxx\",\"username\":\"yyy\",\"name\":\"my name\"

If I do the same: json_params = JSON.parse(request.raw_post) It throws this error:

JSON::ParserError (783: unexpected token at 'payload=%7B%22type%22%3A%22block_............

Also tried

json_params = JSON.parse(request)

That returns //TypeError (no implicit conversion of ActionDispatch::Request into String):

And I tried

puts request and puts request.raw and JSON.parse(request.body) and puts request.body too and they didn't work, I am new to rails so struggling on this for ages, any ideas?

Parameters: {"payload"=>"{\"type\":\"block_actions\",...

This is the request body and query parameters parsed into a Ruby Hash held in params . The key "payload" contains the JSON. JSON parse only params["payload"] .

See Action Controller Overview - Parameters .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM