Bot仅适用于Direct Message

时间:2018-03-19 20:41:30

标签: hangouts-chat

我在Google Apps Scripts中构建了一个Dice Rolling bot,并将其部署为清单。根据功能,我检查了两个" Bot在Rooms"和#34; Bot在直接消息中工作"。

我可以让机器人在DM中做出回应。当我将它添加到房间时,我得到onAddToSpace()消息。但是,我没有收到回复,而是收到了此消息。

无响应:仅对您可见。

enter image description here

我需要做什么才能使机器人在房间内工作?

这是我的代码:

/**
* 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);
}

1 个答案:

答案 0 :(得分:1)

好的,我想出了没有回复的问题。这是因为我的diplayName变量正在查看event.sender.displayName,而正确的路径是event。 message .sender.displayName!我通过转到位于查看&gt;下的堆栈驱动程序日志记录找到了什么。 Google Scripts控制台中的 Stackdriver Logging 。希望这对遇到类似问题的其他人有帮助。 enter image description here