JetBrains Space Help

MessagePayload Class

Relevant for

Chatbots, slash commands.

Sent on

Each time a user sends a message to the application chat.

Payload contents

For example, a user sends do 1234 to the chat:

{ "className": "MessagePayload", // instance of MessageContext class "message": { "messageId": "Cxa000Cxa", // channelId that you can use to get channel name "channelId": "3FhQeS2URbeY", "messageData": null, "body": { "className": "ChatMessage.Text", "text": "do 1234" }, "attachments": null, "externalId": null, "createdTime": "2021-05-27T10:05:03.796Z" }, "accessToken": "", "verificationToken": "abc1234", "userId": "1mEGCd1FvoAh", "serverUrl": "https://mycompany.jetbrains.space", "clientId": "8fd4d79a-d164-4a71-839a-ff8f8bcd6beb", "orgId": "2ulA3W2Vltg6" }

Application response

The application must respond with the 200 OK HTTP status.

Processing commands

If your application is a chatbot or a slash command, typically, it implies that a user must send some command to the application chat. This can be just a command name, for example, help, or a command with arguments, for example, book room-621. The MessagePayload class provides two helper functions for processing commands sent in the messages:

  • MessagePayload.command(): String? returns the command specified by the user

  • MessagePayload.commandArguments(): String? returns the command arguments specified by the user

// For example, a user sends 'do 1234' to chat val cmd = payload.command() // do val cmdargs = payload.commandArguments() // 1234

Processing message attachments

To help you work with different types of message attachments, Space SDK provides the following classes:

  • interface AttachmentIn : Attachment

  • interface LinkPreview : Attachment

  • interface MediaAttachment : AttachmentIn

  • DeletedAttachment( deletedIdentity: PropertyValue<String> ) : Attachment

  • FileAttachment( id: PropertyValue<String>, sizeBytes: PropertyValue<Long>, filename: PropertyValue<String> ) : AttachmentIn

  • ImageAttachment( id: PropertyValue<String>, name: PropertyValue<String?>, width: PropertyValue<Int>, height: PropertyValue<Int>, previewBytes: PropertyValue<String?>, variants: PropertyValue<List<ImageAttachmentVariant>?> ) : MediaAttachment

  • ProfileLinkPreview( profile: PropertyValue<TD_MemberProfile> ) : Attachment

  • UnfurlAttachment( unfurl: PropertyValue<Unfurl>, id: PropertyValue<String> ) : Attachment

  • VideoAttachment( id: PropertyValue<String>, name: PropertyValue<String?>, width: PropertyValue<Int?>, height: PropertyValue<Int?>, sizeBytes: PropertyValue<Long>, previewBytes: PropertyValue<String?> ) : MediaAttachment

Example

// our custom command that can also 'run' something class Command( val name: String, val description: String, val run: suspend (payload: MessagePayload) -> Unit ) { // convert to CommandDetail fun toCommand() = CommandDetail(name, description) } // list of available commands val commands = listOf( Command( "help", "Show this help", ) { payload -> commandHelp(payload) }, Command( "do", "Do something", ) { payload -> commandDo(payload) } ) // convert the list of 'Command' // to the list of 'CommandDetail' fun commandListAllCommands() = Commands( commands.map { it.toCommand() } ) fun main() { embeddedServer(Netty, port = 8080) { routing { post("/api/from-space") { val body = call.receiveText() val payload = readPayload(body) // filter payload by class when (payload) { is MessagePayload -> { // find the command val command = commands.find { it.name == payload.command() } if (command == null) { commandHelp() } else { launch { command.run(payload) } } call.respond(HttpStatusCode.OK, "") } // here goes handling of other payloads else -> call.respond(HttpStatusCode.BadRequest, "Unsupported payload type") } } } }.start(wait = true) } suspend fun commandHelp() { // send help message } suspend fun commandDo(payload: MessagePayload) { val args = payload.commandArguments() // do something with args }

For the full example, refer to the 'How to Create a Chatbot' tutorial: Kotlin, .NET.

Last modified: 06 August 2021