Sunday, 22 August 2021

How do I notify the user of an error in a Card?

I have a very basic card built that will query a url when a user clicks a button. However, sometimes the url that gets queries is unavailable or returns an error. I'd like to notify the user that there was an error (via a toast notification or something similar). I have:

function myfunction(){
    var response = 
      UrlFetchApp.fetch('https://example.com/available',options).getContentText();
    var j = JSON.parse(response);
    if ('error' in j){
      var buttonAction = CardService.newAction().setFunctionName('notifyUser').setParameters({'notifyText': 'an error occured!'});
      var button = CardService.newTextButton().setText('Notify').setOnClickAction(buttonAction);
      var section = CardService.newCardSection().addWidget(button);
      var card = CardService.newCardBuilder().addSection(section);
      card.build();
    }
...
...
}

...
...
...
function notifyUser(e) {
  var parameters = e.parameters;
  var notificationText = parameters['notifyText'];
  return CardService.newActionResponseBuilder()
      .setNotification(CardService.newNotification()
      .setText(notificationText)
      .setType(CardService.NotificationType.INFO))
      .build();
}

I know the above is wrong. I simply copy and pasted the example shown here (I was planning on getting rid of the button and just showing the notification part). How do I show just the notification on error?

EDIT:

In this example they show a notification but I am unable to add it to my existing card:

function createCatCard(text, isHomepage, withError) {
  var imageUrl = 'https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png';
  var image = CardService.newImage().setImageUrl(imageUrl).setAltText('Cat')
  var action = CardService.newAction().setFunctionName('onChangeCat').setParameters({text: text, isHomepage: isHomepage.toString()});
  var button = CardService.newTextButton().setText('Change cat').setOnClickAction(action).setTextButtonStyle(CardService.TextButtonStyle.FILLED);
  var buttonSet = CardService.newButtonSet().addButton(button);

  var section = CardService.newCardSection().addWidget(image).addWidget(buttonSet);
  var notification = CardService.newNotification().setText('hi there fjsdfkasdjf');

  var card = CardService.newCardBuilder().addSection(section).setFixedFooter(CardService.newFixedFooter().setPrimaryButton(CardService.newTextButton().setText('Cat').setOpenLink(CardService.newOpenLink().setUrl('https://example.com')))).newActionResponseBuilder().setNotification(notification);

}

The error I get with the updated example is TypeError: Cannot find function newActionResponseBuilder in object CardBuilder. [line: 111, function: createCatCard, file: Common]



from How do I notify the user of an error in a Card?

No comments:

Post a Comment