简体   繁体   中英

Bad Request: can't parse reply keyboard markup JSON object

I want to create button in my telegram bot by javascript and I working in script google.

But it didn't fetch.

My code:

function a() {
  const opts = {
    reply_to_message_id: '690534265',
    reply_markup: {
      resize_keyboard: true,
      one_time_keyboard: true,
      keyboard: [ ['test'], ['test1'] ]
    }
  };

  MainKeyBoard(opts, 'afs', '690534265');
}


function MainKeyBoard(tasti, toxt, chid) {
  var url = link + '/sendMessage?parse_mode=Markdown' +
    '&chat_id=' + chid +
    '&text=' + toxt +
    '&reply_markup=' + tasti;
  var respose = UrlFetchApp.fetch(url);
}

This is my error:

   Request failed for https://api.telegram.org/bot745193421:<token>/sendMessage?parse_mode=Markdown&chat_id=690534265&text=afs&reply_markup=[object%20Object] returned code 400. Truncated server response: {"ok":false,"error_code":400,"description":"Bad Request: can't parse reply keyboard markup JSON object"} (use muteHttpExceptions option to examine full response)

I tried to add JSON.parse but error :

SyntaxError: Unexpected token: o

Since opts is an object when you are trying to concatenate it will use the toString() method which will just return [object Object] that you can see in the request.

In order to solve this issue serialize your object using JSON.stringify in your MainKeyboard function.

function MainKeyBoard(tasti, toxt, chid) {
  var url = link + '/sendMessage?parse_mode=Markdown&chat_id=' + chid +
    '&text=' + toxt + '&reply_markup=' + JSON.stringify(tasti);
  var response = UrlFetchApp.fetch(url);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM