我在Google Apps Scripts中构建了一个Dice Rolling bot,并将其部署为清单。根据功能,我检查了两个" Bot在Rooms"和#34; Bot在直接消息中工作"。
我可以让机器人在DM中做出回应。当我将它添加到房间时,我得到onAddToSpace()消息。但是,我没有收到回复,而是收到了此消息。
无响应:仅对您可见。
我需要做什么才能使机器人在房间内工作?
这是我的代码:
/**
* 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);
}