Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to (un)marshal SlackPreparedMessage from JSON string? #260

Open
boobiq opened this issue Jan 11, 2019 · 0 comments
Open

Is it possible to (un)marshal SlackPreparedMessage from JSON string? #260

boobiq opened this issue Jan 11, 2019 · 0 comments

Comments

@boobiq
Copy link

boobiq commented Jan 11, 2019

Hi,

I'm trying to use actions (https://api.slack.com/docs/message-buttons) in messages. I have simple webserver with endpoint for handling callbacks running and getting response after clicking triggering actions from message works well.

In request Slack makes as callback, there is payload JSON containing original message (JSON string). Since I want to update previous message (imagine simple message with 1 button which will be changed to "thank you" message after clicked), I'd like to create SlackPreparedMessage from that JSON, update attachments, texts etc and than get JSON string from it so I can send it back to Slack.

Is this possible somehow? currently I was doing something like this just to test if it works (using org.json.JSONObject) in Kotlin:

// ctx.formParam("payload") is JSON string containin data sent by Slack after clicking on action
val requestData = JSONObject(ctx.formParam("payload"))
val responseUrl = requestData.getString("response_url")
val callbackId = requestData.getString("callback_id")
val originalMessage = requestData.getJSONObject("original_message")

val oldAttachments = originalMessage.getJSONArray("attachments")
oldAttachments.getJSONObject(0)
    .put("callback_id", "new callb")
    .put("text", "some new text")
    .put(
        "actions", JSONArray(
            arrayOf(
                // new actions to replace old buttons
            )
        )
    )

// send message back to responseUrl via Fuel
val (_, response, result) = Fuel.post(responseUrl)
    .body(originalMessage.toString())
    .header("Content-Type" to "application/json")
    .responseString()

I would like to do something like (kind of pseudo-code 😄)

val newAttachment = SlackAttachment().apply {
    // ...setting attachment fields
}
val msg = SlackPreparedMessage.parse(originalMessage)
    .withAttachments(listOf(newAttachments))
    .toJson()

//send POST request with msg

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant