skip to Main Content

I’m so new with GAS and trying to write a Telegram bot with text commands and keyboard but it didn’t give me the result when I pressed keyboard. (text commands work).

What did I do wrong? Thanks for reading and helping me with it.

Here my code:

function doPost(e) {
  var contents = JSON.parse(e.postData.contents);
  var keyBoard = {
    "inline_keyboard": [
      [{
        "text": "Edit",
        'callback_data': 'edit'
      }]
    ]
  };  
  var text = contents.message.text;
  var id = contents.message.from.id;
  var user = contents.message.from.username;
  var Filter = CTask.getRange('I:I').getValues();
  var NV = CTask.getRange('J:J').getValues();
  var tasks = [];
  var STT = [];
  var list = [];
  for (var x = 1 ; (x < lastrow) && (Filter[x] == "Giao việc"); x++) {
    tasks.push(NV[x]);
    STT.push(x + ")" + " ");
    tasks.sort();
  };
  for (var y = 0 ; y < tasks.length; y++) { 
    list.push(STT[y]+tasks[y]);
    var taskslist = list.join("n");
  };  
  if (contents.callback_query) {
    var id_callback = contents.callback_query.from.id;
    var data = contents.callback_query.data;    
    if (data == 'edit') {
      sendText(id_callback,"P is your allocated budget for the week" );
    }     
  } else if (contents.message) {
    if((text.substring(0,5) != "/task") && (text.substring(0,5) != "/list")) {      
      sendText(id, '@'+user + " Bạn đã gọi sai cú pháp");
    } else if (text.substring(0,5) == "/task") { 
      CTask.appendRow([new Date(), user, text]);
    }  
    // Lệnh xem list
    else if ((text.substring(0,5) == "/list") && (text.length == 5)) {      
      sendText(id,  decodeURI( "Các nghiệp vụ cần thực hiện: %0A"+ taskslist),keyBoard);
    }    
  }
}

2

Answers


  1. Based on the keyboard you have created in your script, the callback data appears to be ‘budget’. (Is that right?)

    But you seem to be checking

    if (data == 'edit') {
    

    According to the docs, callback_query.data is the “Data associated with the callback button.”

    Login or Signup to reply.
  2. In my opinion your code falls because you have 3 variables (id, text, user) at the top of your function. They should be moved to the section where you handle messages.

    Those variables on the top drops your code when you send callback.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search