Search code examples
google-hangouts

Bot only works in Direct Message


I've built a Dice Rolling bot in Google Apps Scripts and deployed it as a manifest. Under functionality I checked both "Bot works in Rooms" and "Bot works in direct messages".

I can get the bot to respond in a DM. When I add it to a room I get the onAddToSpace() message. However I don't get a response, instead I get this message.

Not Responding: Only visible to you.

enter image description here

What do I need to do, to enable the bot to work in rooms?

Here is my code:

/**
* Responds to a MESSAGE event in Hangouts Chat.
*
* @param {Object} event the event object from Hangouts Chat
*/
function onMessage(event) {
  var name = "";

  if (event.space.type == "DM") {
    name = "You";
  } else {
    name = event.sender.displayName;
  }
  var roll = diceRoll(event.message.text)
  if (roll[1] == ''){
    if (event.space.type == 'DM'){
      var message = name + " rolled  \"" + event.message.text + "\" and got " + roll[0];
    }
    else {
      var message = name + " rolls  \"" + event.message.text + "\" and gets " + roll[0];
    }
  }
  else {
    if (event.space.type == 'DM') {
      var message = name + " rolled  \"" + event.message.text + "\" and got " + roll[0] + ". [" + roll[1] + "]";
    }
    else{
      var message = name + " rolls  \"" + event.message.text + "\" and gets " + roll[0] + ". [" + roll[1] + "]";
    }
  }

  return { "text": message };
}

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}

function add(a, b) {
  return a + b;
}

function diceRoll(message){
  var fixedMessage = message.toLowerCase()
  fixedMessage = fixedMessage.split('d')
  var numberOfDice = parseInt(fixedMessage[0])
  var sizeOfDice = parseInt(fixedMessage[1])
  if (numberOfDice == 1){
    return [Math.floor(getRandomInt(1, sizeOfDice)), '']
  }
  else {
    var diceRolls = []
    for(i=0; i<numberOfDice; i++){
      diceRolls.push(Math.floor(getRandomInt(1, sizeOfDice)))
    }
    var sum = Math.floor(diceRolls.reduce(add, 0))
    return [sum, diceRolls]
  }
}

function test(){
  Logger.log(diceRoll('2d20'))
  Logger.log(diceRoll('1d20'))
}

/**
* Responds to an ADDED_TO_SPACE event in Hangouts Chat.
*
* @param {Object} event the event object from Hangouts Chat
*/
function onAddToSpace(event) {
  Logger.log(event)
  var message = "";

  if (event.space.type == "DM") {
    message = "Thank you for adding DiceRoll to a DM, " + event.user.displayName + "!";
  } else {
    message = "Thank you for adding DiceRoll to " + event.space.displayName;
  }

  return { "text": message };
}

/**
* Responds to a REMOVED_FROM_SPACE event in Hangouts Chat.
*
* @param {Object} event the event object from Hangouts Chat
*/
function onRemoveFromSpace(event) {
  Logger.log(event)
  console.info("Bot removed from ", event.space.name);
}

Solution

  • Ok, I figured out the not responding issue. This was because my diplayName variable was looking at event.sender.displayName and the proper path was event.message.sender.displayName! I found what was breaking by going to the Stack Driver logging located under View > Stackdriver Logging in the Google Scripts console. Hope this helps for anyone else who runs into similar issues. enter image description here