2

This here is my function that hears for anything incoming from Telegram. It does work well with any message and files or whatever but it doesn't work when i press a but of an inline_keyboard, Why?

How can i create a function that hears for a inline_keyboard button pressed?

function doPost(e) {

  var data = JSON.parse(e.postData.contents);

  sendText('personal_chat_id', JSON.stringify(data));

}

In case you need it here under is my function that can send a message with an inline_keyboard.

function lol(){
  var keyboard ={
    inline_keyboard: [
      [
        {
          text: "A",
          callback_data: 123
        },
        {
          text: "B",
          callback_data: 234
        }
      ],
      [
        {
          text: "C",
          callback_data: 345
        },
        {
          text: "D",
          callback_data: 456
        }
      ]
    ]
  }
  sendText('personal_chat_id', "lol", keyboard)
}

function sendText(chatId,text,keyBoard){

  keyBoard = keyBoard || 0;

  if(keyBoard.inline_keyboard || keyBoard.keyboard){
    var data = {
      method: "post",
      payload: {
        method: "sendMessage",
        chat_id: String(chatId),
        text: text,
        parse_mode: "HTML",
        reply_markup: JSON.stringify(keyBoard)
      }
    }
    }

  var response = UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
  return response.getContentText()
}
Paolo177
  • 366
  • 6
  • 18

1 Answers1

0

You need to do it inside the doPost(e) function. Take the "e" parameter and verify what kind of type it is. If it is "contents.callback_query" is coming from the inline keyboard response and if it is "contents.message" it's text typed by the user. And dont forget declare sendText() and sendKeyboard() functions.

function doPost(e) {

var contents = JSON.parse(e.postData.contents);

var keyboard = {
"inline_keyboard": [
  [{
    "text": "Players",
    "callback_data": "player"
  }],
  [{
    "text": "Match",
    "callback_data": "match"
  }],
  [{
    "text": "Results",
    "callback_data": "result"
  }]
]}

if (contents.callback_query) {
var id = contents.callback_query.message.chat.id;
var text = contents.callback_query.data;
switch (text) {
  case 'Player':
    var answer = 'Ingresa tu nombre y apellido';
    sendText(id, answer);
    break;
  case 'match':
    var answer = 'Ingresá tu nombre y apellido';
    sendText(id, answer); 
    break;
  case 'result':
    var keyboard = {
      'inline_keyboard': [
        [{
          'text': 'Youtube',
          'url': 'https://youtube.com'
        },{
          'text': 'Google',
          'url': 'https://google.com'
        }]
      ]
    };
    sendTextWithButtons(id, 'Algunos enlaces', JSON.stringify(keyboard));
    break;
  default:
    var answer = 'Default answer';
    sendText(id, answer);
    break;
}
  var chat_id = contents.callback_query.from.id;
  var user = contents.callback_query.message.chat.first_name;
  var cb_data = contents.callback_query.data;
  sendText(chat_id, CariDataDariIDSheet(cb_data)); 

}else if (contents.message) {

  var chat_id = contents.message.from.id;
  var user = contents.message.chat.first_name;
  var answer = "your answer";
  sendKeyboard(chat_id, answer, keyboard);
 }
}
Cheloespinoza
  • 57
  • 2
  • 8