Class ChatResponseBuilder

ChatResponseBuilder

A builder for ChatResponse objects.

Only available for Google Chat apps. Not available for Google Workspace Add-ons.

const cardSection = CardService.newCardSection();
cardSection.addWidget(
    CardService.newTextParagraph().setText('This is a text paragraph widget.'),
);

const card = CardService.newCardBuilder()
                 .setName('Card name')
                 .setHeader(CardService.newCardHeader().setTitle('Card title'))
                 .addSection(cardSection)
                 .build();

const cardWithId =
    CardService.newCardWithId().setCardId('card_id').setCard(card);

const chatResponse = CardService.newChatResponseBuilder()
                         .addCardsV2(cardWithId)
                         .setText('Example text')
                         .build();

Methods

MethodReturn typeBrief description
addCardsV2(cardWithId)ChatResponseBuilderSets the card field of the message.
build()ChatResponseBuilds the current action response and validates it.
setActionResponse(actionResponse)ChatResponseBuilderSets the action response field of the message.
setText(text)ChatResponseBuilderSets the text of the Chat message.

Detailed documentation

addCardsV2(cardWithId)

Sets the card field of the message. This is used to send a card in a Google Chat message. Each card is associated with a unique id, CardWithId object should be built and be used with this method.

const cardSection = CardService.newCardSection();
cardSection.addWidget(
    CardService.newTextParagraph().setText('This is a text paragraph widget.'),
);

const card = CardService.newCardBuilder()
                 .setHeader(CardService.newCardHeader().setTitle('Card title'))
                 .addSection(cardSection)
                 .build();

const cardWithId =
    CardService.newCardWithId().setCardId('card_id').setCard(card);

const chatResponse =
    CardService.newChatResponseBuilder().addCardsV2(cardWithId).build();

Parameters

NameTypeDescription
cardWithIdCardWithIdThe CardWithId to use.

Return

ChatResponseBuilder — This object, for chaining.


build()

Builds the current action response and validates it.

Return

ChatResponse — A validated ChatResponse.


setActionResponse(actionResponse)

Sets the action response field of the message.

// Build the card.
const card = CardService.newCardBuilder()
                 .setHeader(CardService.newCardHeader().setTitle('card title'))
                 .build();

// Creates the dialog.
const dialog = CardService.newDialog().setBody(card);

// Creates the dialog action.
const dialogAction = CardService.newDialogAction().setDialog(dialog);

// Creates the action response and sets the type to DIALOG.
const actionResponse = CardService.newChatActionResponse()
                           .setDialogAction(dialogAction)
                           .setResponseType(CardService.Type.DIALOG);

// Creates the Chat response and sets the action response.
const chatResponse = CardService.newChatResponseBuilder()
                         .setActionResponse(actionResponse)
                         .build();

Parameters

NameTypeDescription
actionResponseChatActionResponseThe ChatActionResponse to use.

Return

ChatResponseBuilder — This object, for chaining.


setText(text)

Sets the text of the Chat message.

const chatResponse =
    CardService.newChatResponseBuilder().setText('Example text').build();

Parameters

NameTypeDescription
textStringThe text to use.

Return

ChatResponseBuilder — This object, for chaining.