#java #json #post #telegram-bot #telegram-api
Вопрос:
Я пытаюсь отправлять запросы на публикацию через API Telegram, но это не очень хорошо сочетается со встроенной клавиатурой. Хотя текст сообщения отправлен , клавиатура не отображается.
Это моя функция :
public static void ApiToOpponent(InlineKeyboardMarkup keyboardMarkup, long chat_id, String text) throws IOException {
HttpClient httpclient = HttpClients.createDefault();
List<List<InlineKeyboardButton>> keyboard = keyboardMarkup.getKeyboard();
URL url = new URL ("https://api.telegram.org/bot1778589497:AAE2CGovQ67.......bk/sendMessage");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
JSONObject params = new JSONObject();
params.put("chat_id",Long.toString(chat_id));
params.put("text",text);
JSONArray field_of_buttons = new JSONArray();
JSONArray row_of_buttons;
for( List<InlineKeyboardButton> row : keyboard) {
row_of_buttons = new JSONArray();
for(InlineKeyboardButton button : row){
JSONObject button_json = new JSONObject();
JSONArray param_aray = new JSONArray();
JSONObject button_param = new JSONObject();
button_param.put("text",button.getText());
button_param.put("callback_data",button.getCallbackData());
param_aray.put(button_param);
button_json.put("InlineKeyboardButton",param_aray);
row_of_buttons.put(button_json);
}
field_of_buttons.put(row_of_buttons);
}
JSONObject last_json = new JSONObject();
last_json.put("InlineKeyboardMarkup",field_of_buttons);
params.put("reply_markup",last_json);
try(OutputStream os = con.getOutputStream()) {
byte[] input = params.toString().getBytes("utf-8");
os.write(input, 0, input.length);
}
try(BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
}
Json из запроса является:
{"text":"Your opponent has been found. Make a move","reply_markup":{"InlineKeyboardMarkup":[[{"InlineKeyboardButton":[{"text":"⬜","callback_data":"Tic_tac_move:0,0,,X"}]},{"InlineKeyboardButton":[{"text":"⬜","callback_data":"Tic_tac_move:0,1,,X"}]},{"InlineKeyboardButton":[{"text":"⬜","callback_data":"Tic_tac_move:0,2,,X"}]}],[{"InlineKeyboardButton":[{"text":"⬜","callback_data":"Tic_tac_move:1,0,,X"}]},{"InlineKeyboardButton":[{"text":"❌","callback_data":"Tic_tac_move:1,1,,X"}]},{"InlineKeyboardButton":[{"text":"⬜","callback_data":"Tic_tac_move:1,2,,X"}]}],[{"InlineKeyboardButton":[{"text":"⬜","callback_data":"Tic_tac_move:2,0,,X"}]},{"InlineKeyboardButton":[{"text":"⬜","callback_data":"Tic_tac_move:2,1,,X"}]},{"InlineKeyboardButton":[{"text":":heavy_large_circle:","callback_data":"Tic_tac_move:2,2,,X"}]}]]},"chat_id":"369439.."}
И ответ на запрос таков {"ok":true,"result":{"message_id":2420,"from":{"id":1778589497,"is_bot":true,"first_name":"Minesweeper","username":"Minesweep..."},"chat":{"id":3694....,"first_name":"Qwzr","username":"Qwa...","type":"private"},"date":1619903986,"text":"Your opponent has been found. Make a move"}}
Буду чрезвычайно рад любому ответу : )
Ответ №1:
В этом случае правильный запрос json:
{«текст»:»Ваш оппонент найден. Сделайте move»,»reply_markup»:{«inline_keyboard»:[[{«text»:»⬜»,»callback_data»:»Tic_tac_move:0,0,,X»},{«text»:»⬜»,»callback_data»:»Tic_tac_move:0,1,,X»},{«text»:»⬜»,»callback_data»:»Tic_tac_move:0,2,,X»}],[{«text»:»⬜»,»callback_data»:»Tic_tac_move:1,0,,X»},{«text»:»❌»,»callback_data»:»Tic_tac_move:1,1,,X»},{«text»:»❌»,»callback_data»:»Tic_tac_move:1,2,,X»}],[{«text»:»⬜»,»callback_data»:»Tic_tac_move:2,0,,X»},{«text»:»⬜»,»callback_data»:»Tic_tac_move:2,1,,X»},{«text»:»⭕»,»callback_data»:»Tic_tac_move:2,2,,X»}]]},»chat_id»:»177114…»}