Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65893598/how-to-parse-access-json-from-slack-action-using-ruby-on-rails

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...