Function for collecting 3 answers from Telegram and posting to Google Forms

Hi,
I'm trying to send tree questions via Telegram, collect the answers and post them to Google Forms.
I have a working bot and the posting part figured out, the part that am struggling with is the function.
It should work like this:
The user sends a message, and by default that's the answer to the first question, function stores it in a variable, then asks YES or NO for the second question (as question 2 and 3 are optional )if you responds with YES it asks the second question and stores it in a second variable, and sends again YES or NO for third question and stores it ...
When it's done with the questions asks YES or NO to post. When done flush variables.

This is the code for YES no NO

var opts = {
  reply_markup: JSON.stringify({
    keyboard: [
      ['/YES'],
      ['/NO']],
      'resize_keyboard' : true, 
      'one_time_keyboard' : true
  })
};

msg.error = false;
// Dialogaufbau
msg.payload.content = 'Utrošen materijal?'; // QUESTION
msg.payload.options = opts;

msg.payload.chatId = msg.payload.chatId;
msg.payload.messageId = 99;
msg.payload.sentMessageId = 99;
msg.payload.type = "message";

Work in progress flow

[{"id":"d76bd423.63fc68","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"6f374a8b.b68764","type":"telegram receiver","z":"d76bd423.63fc68","name":"","bot":"93994684.5d2958","saveDataDir":"","filterCommands":false,"x":210,"y":280,"wires":[["54d4a7d9.bbf5d8"],[]]},{"id":"54d4a7d9.bbf5d8","type":"function","z":"d76bd423.63fc68","name":"Sorting chatID","func":"var ID = msg.payload.chatId;\nvar Miljan = xxxxxxxxxx;\nvar Lepi = xxxxxxxx;\nvar Milos = xxxxxxxxx;\nvar Dejan = xxxxxxxxx;\n\nif (ID == Miljan) {\n    \n    return [msg,null,null,null]\n}\nif (ID == Lepi) {\n    \n    return [null,msg,null,null]\n}\nif (ID == Dejan) {\n    \n    return [null,null,msg,null]\n}\nif (ID == Milos) {\n    \n    return [null,null,null,msg]\n}\n\nelse\n\nreturn null;","outputs":4,"noerr":0,"initialize":"","finalize":"","x":420,"y":280,"wires":[["9d95a85b.e3aae8"],[],[],[]],"outputLabels":["Miljan","Lepi","Dejan","Milos"]},{"id":"cd81b3ec.f705","type":"telegram sender","z":"d76bd423.63fc68","name":"","bot":"93994684.5d2958","haserroroutput":false,"outputs":1,"x":950,"y":220,"wires":[[]]},{"id":"7dd4e3c9.85a1dc","type":"http request","z":"d76bd423.63fc68","name":"Post to Google Forms","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://docs.google.com/forms/d/e/xxxxxxxxxCzaKRqQ/formResponse?entry.1765987963={{payload.unos}}","tls":"","persist":false,"proxy":"","authType":"","x":960,"y":300,"wires":[[]]},{"id":"9d95a85b.e3aae8","type":"function","z":"d76bd423.63fc68","name":"user 1 function","func":"\n\n\n// work in progres\nvar opts = {\n  reply_markup: JSON.stringify({\n    keyboard: [\n      ['/YES'],\n      ['/NO']],\n      'resize_keyboard' : true, \n      'one_time_keyboard' : true\n  })\n};\n\nmsg.error = false;\n// Dialogaufbau\nmsg.payload.content = 'Utrošen materijal?'; // QUESTION\nmsg.payload.options = opts;\n\nmsg.payload.chatId = msg.payload.chatId;\nmsg.payload.messageId = 99;\nmsg.payload.sentMessageId = 99;\nmsg.payload.type = \"message\";\n\nreturn [ msg ];\n\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":660,"y":220,"wires":[["cd81b3ec.f705"],["7dd4e3c9.85a1dc"]]},{"id":"93994684.5d2958","type":"telegram bot","botname":"NatalySpa","usernames":"","chatids":"1026277503,1457589930,5020608523,5234937789,","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":true}]

Have you tried the built in demos? (there are quite a few)

CTRL-I → examples → node-red-contrib-telegrambot

PS, Some tips to help you get going...

  1. Use debug nodes (set to show complete message) before AND after every node to determine what is going IN and what is coming OUT.
  2. Use the Copy Path button to grab the path of a msg property (this button appears under your mouse when you hover over a debug item) to avoid typos in functions

I Got it working like this

var op1 = flow.get("op1v"); //broj pitanja
//dugmici
var msg1 = {};

var opts = {
  reply_markup: JSON.stringify({
    keyboard: [
      ['-DA-'],
      ['-NE-']],
      'resize_keyboard' : true, 
      'one_time_keyboard' : true
  })
};
//-------


//predaja izveštaja

if (op1 == 3 & msg.payload.content == "-NE-") {
    flow.set("op1v", 0);
    flow.set("odg11", "");
    flow.set("odg12", "");
    flow.set("odg13", "");
    
    msg.payload.content = 'Izveštaj obustavljen.'; // QUESTION 3
    msg.payload.chatId = msg.payload.chatId;
    msg.payload.messageId = 1;
    msg.payload.type = "message";
    return [ msg, null ];
    
    
}



//treće pitanje ako ima odgovora
if (op1 == 3 & msg.payload.content == "-DA-") {
    flow.set("op1v", 0);
    msg.payload = {
         "chatId": msg.payload.chatId,
         "messageId": 1,
         "type": "message",
         "content": "Izveštaj predat."
    };
    
    msg1.payload = {
    "operater": "Miljan Miletic",
    "odradjeno": flow.get("odg11"),
    "materijal": flow.get("odg12"),
    "kvar": flow.get("odg13"),
    }
    
    flow.set("odg11", "");
    flow.set("odg12", "");
    flow.set("odg13", "");
    
    return [ msg, msg1 ];
    
}





//trece pitanje ako nema odgovora
if (op1 == 2 & msg.payload.content == "-NE-") {
    flow.set("op1v", 3);
    flow.set("odg13", "");
    
    
    msg.error = false;
    // Dialog aufbau
    msg.payload.content = 'Predaj izveštaj?'; // QUESTION 3
    msg.payload.options = opts;
    
    msg.payload.chatId = msg.payload.chatId;
    msg.payload.messageId = 99;
    msg.payload.sentMessageId = 99;
    msg.payload.type = "message";
    return [ msg, null ];
    
    
}



//treće pitanje ako ima odgovora
if (op1 == 2 & msg.payload.content == "-DA-") {
    
    msg.payload = {
         "chatId": msg.payload.chatId,
         "messageId": 1,
         "type": "message",
         "content": "Opis kvara ili ostećenja?"}
    return [ msg, null ];
    
}





// drugo pitanje ako neam odgovora
if (op1 == 1 & msg.payload.content == "-NE-") {
    flow.set("op1v", 2);
    flow.set("odg12", "");
    
    
    msg.error = false;
    // Dialog aufbau
    msg.payload.content = 'Krvar ili Oštećenja?'; // QUESTION 3
    msg.payload.options = opts;
    
    msg.payload.chatId = msg.payload.chatId;
    msg.payload.messageId = 99;
    msg.payload.sentMessageId = 99;
    msg.payload.type = "message";
    return [ msg, null ];
    
    
}



//drugo pitanje ako ima odgovora
if (op1 == 1 & msg.payload.content == "-DA-") {
    
    msg.payload = {
         "chatId": msg.payload.chatId,
         "messageId": 1,
         "type": "message",
         "content": "Šta je utrošeno?"}
    return [ msg, null ];
    
}








//provera prvog pitanja odradjeno
if (op1 == 0) {
    flow.set("odg11", msg.payload.content); // upamti prvi odgovor
    flow.set("op1v", 1);
    
    msg.error = false;
    // Dialog aufbau
    msg.payload.content = 'Utrošen materijal?'; // QUESTION 2
    msg.payload.options = opts;
    
    msg.payload.chatId = msg.payload.chatId;
    msg.payload.messageId = 99;
    msg.payload.sentMessageId = 99;
    msg.payload.type = "message";
    return [ msg, null ];

} 
// utrošeni materijal
if (op1 == 1) {
    flow.set("op1v", 2);
    flow.set("odg12", msg.payload.content);
    
    
    msg.error = false;
    // Dialog aufbau
    msg.payload.content = 'Krvar ili Oštećenja?'; // QUESTION 3
    msg.payload.options = opts;
    
    msg.payload.chatId = msg.payload.chatId;
    msg.payload.messageId = 99;
    msg.payload.sentMessageId = 99;
    msg.payload.type = "message";
    return [ msg, null ];
}
//kvar ili oštećenje

if (op1 == 2) {
    flow.set("op1v", 3);
    flow.set("odg13", msg.payload.content);
    
    
    msg.error = false;
    // Dialog aufbau
    msg.payload.content = 'Predaj izveštaj?'; // QUESTION 3
    msg.payload.options = opts;
    
    msg.payload.chatId = msg.payload.chatId;
    msg.payload.messageId = 99;
    msg.payload.sentMessageId = 99;
    msg.payload.type = "message";
    return [ msg, null ];
}


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.