I am writing a google app script that works with telegram. The user will key in their unique 4 digit code and it will run against my array. i have most of the stuff working. I’m currently stuck getting my code to match the users input.
from the JSON below. i am only able to extract the first result in my syntax.
for example extracting the chatid
var contents = JSON.parse(e.postData.contents)
var id = contents.message.from.id
But what i am trying to extract is the "Welcome" from the text portion of the 2nd response.
{
"ok": true,
"result": [
{
"update_id": 11111111,
"message": {
"message_id": 412,
"from": {
"id": 22222222,
"is_bot": false,
"first_name": "don",
"username": "king",
"language_code": "en"
},
"chat": {
"id": 333333333,
"first_name": "Don",
"username": "king",
"type": "private"
},
"date": 1600865179,
"text": "Test"
}
},
{
"update_id": 111111111,
"message": {
"message_id": 413,
"from": {
"id": 1111111111,
"is_bot": false,
"first_name": "Don",
"username": "king",
"language_code": "en"
},
"chat": {
"id": 44444444,
"first_name": "don",
"username": "king",
"type": "private"
},
"date": 1600865186,
"text": "Welcome"
}
}
]
}
This is the main part of my script that has difficulty. I was able to log and confirm it works and generates the values and array i need. However when i send an input to telegram it stops at "Please Wait".
function doPost(e){
var contents = JSON.parse(e.postData.contents);
var id = contents.message.from.id;
sendMessage(id, "Please wait");
var userInput = contents.result[1].message.text;
var ssId = "XXXX";
var sheet = SpreadsheetApp.openById(ssId).getSheetByName("Sheet1");
var idArr = sheet.getRange("A2:A").getValues();
idArr = idArr.map(row => row[0]);
var idIndex = idArr.indexOf(userInput);
if(userinput == idArr){
let rowNumber = idIndex + 3;
let colNumber = 4; //col D
var dateNow = new Date;
sheet.getRange(rowNumber, colNumber).setValue(dateNow);
sendMessage(id, "You have successfully registered");
}
else{
sendMessage(id, "user not found");
}
}
2
Answers
Does this work for you?
The result inside that JSON is an array so you could try loop it to get the data. try this….