2017-03-24 67 views
2

例JSON作秀inline_keyboard在電報機器人電報BOT:例如JSON,inline_keyboard

https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating

enter image description here

{ 
     "chat_id": "123456", 
     "text": "Hi", 
     "reply_markup": { 
      "inline_keyboard": [[ 
       { 
        "text": "A", 
        "callback_data": "A1"    
       }, 
       { 
        "text": "B", 
        "callback_data": "C1"    
       }] 
      ] 
     } 
    } 
+0

這似乎並不是一個真正的問題。 [這裏有一個問題鏈接,可能會幫助你](https://stackoverflow.com/help/how-to-ask) –

回答

1

嗯,我想我明白你的意思,jeissonp。看來你是如何使用Node.js寫電報機器人,這就是你如何提供一個內嵌鍵盤的用戶:

創建鍵盤:

const opts = { 
"reply_markup": { 
      "inline_keyboard": [[ 
       { 
        "text": "A", 
        "callback_data": "A1"    
       }, 
       { 
        "text": "B", 
        "callback_data": "C1"    
       }] 
      ] 
     } 
} 

,然後發送一個消息與OPTS:

bot.sendMessage(chatID, "Message text", opts); 

希望它有幫助!

4

我只是很難試圖讓它在我的API上工作,我發現了這個問題。您需要JSON.stringify()的內容reply_markup將鍵盤對象和內容首先轉換爲字符串。

下面是一個例子。

bot.onCommand = function (chat, from, message_id, text, command, commandData) { 
    if (command === "test") { 
     var keyboard = { 
      "inline_keyboard": [ 
       [ 
        {"text": "Yes", "url": "http://www.google.com/"}, 
        {"text": "No", "url": "http://www.google.com/"} 
       ] 
      ] 
     }; 

     var data = { 
      "reply_to_message_id": message_id, 
      "reply_markup": JSON.stringify(keyboard) 
     }; 


     bot.sendText(chat.id, "test", data, function (isSuccess) { 
      console.log(isSuccess); 
     }); 

     return; 
    } 
} 

我寫了這個,希望能夠減少混淆。

的輸出將是:

(test ) 
[Yes] [No] 

的circler括號是消息的方括號是按鈕。在這個例子中都打開了一個到Google的鏈接。