-1

i am trying to create a bot in google chat and i would like to enter slash commands. it's my first time creating a bot in google chat and i admit i am definitely a beginner. what i managed to do is publish the bot and make it reply to messages, then go added slash commands in the bot configuration panel but i can't integrate its functionality into the bot code. Could someone give me an example? Do you know where I can find useful information? I only found this guide https://developers.google.com/hangouts/chat/how-tos/slash-commands thank you all! edit:

function onMessage(event) {
var name = "";
  if (event.space.type == "DM") {
    name = "Hai";
  } else {
    name = event.user.displayName + " ha";
  }
  if (message.slashCommand) {
  switch (message.slashCommand.commandId) {
    case 1:  // /prova
      return  { "text": "slash" };
  }
}

  if (event.message.text =="prova") {
    var message = "Prova ok"
    } else {
  var message = name + " detto \"" + event.message.text + "\"";
  }
  return { "text": message }; 
  }
  
  
/*
 Aggiunta bot a conversazione
 */
function onAddToSpace(event) {
  var message = "";

  if (event.space.singleUserBotDm) {
    message = "Bot aggiunto a messaggio diretto, " + event.user.displayName + "!";
  } else {
    message = "Bot aggiunto in " +
        (event.space.displayName ? event.space.displayName : "this chat");
  }

  if (event.message) {
    // Ingaggio su menzione
    message = message + " and you said: \"" + event.message.text + "\"";
  }

  return { "text": message };
}

/*
 Messaggio di rimozione bot da conversazione
 */
function onRemoveFromSpace(event) {
  console.info("Bot rimosso da ",
      (event.space.name ? event.space.name : "this chat"));
}
zbrady
  • 1
  • 1
  • Hi there! In the linked docs there is a step by step example on how to add slash commands in bots. Could you please clarify which part of the guide blocks your progress? Please share your current code too so we all can take a look. – Jacques-Guzel Heron Dec 31 '20 at 08:39
  • Hello, I just added the "if" condition to my project, but in this way the bot crashes. Consider that I am really a beginner and I would like to learn better how to manage my bot, I'm looking for valid guides everywhere but I can't find any. Generally I find some projects and studying them I learn something new but I find practically nothing about google chat bots and even less about slash commands. I post the code of my bot anyway: – zbrady Jan 02 '21 at 11:53
  • If you are learning how to manage a bot I strongly recommend you to follow the [quickstart bot](https://developers.google.com/hangouts/chat/quickstart/apps-script-bot). I see your script, but I don't understand the question at hand. Could you please clarify to me where you need help? – Jacques-Guzel Heron Jan 06 '21 at 15:03

1 Answers1

0

first of all thanks for the help you are offering me. I'm following the guide you posted to me https://developers.google.com/hangouts/chat/quickstart/apps-script-bot. I posted the bot and it replies to messages. what i can't do is program my bot by implementing slash commands. I tried to figure it out from the official guide I posted earlier but I really can't: if I send a slash command to my bot it crashes. Unfortunately, I don't find a lot of documentation about it on the web. The ideal would be to have an example bot that has slash commands.

zbrady
  • 1
  • 1
  • not sure if you configured your slash commands in here https://console.developers.google.com/apis/api/chat.googleapis.com/overview?project= | --> Configuration – ilikeopensource May 31 '21 at 07:34