M
M
MarkusEfr2018-09-27 16:04:39
Java
MarkusEfr, 2018-09-27 16:04:39

Telegram bot does not respond to buttons, how to force it?

I am doing a training project "Trading telegram bot". At this point, it responds to /start and /help, and should now handle pressing the InlineKeyboardButton with setCallbackData specified. Then getData() checks the update request and issues a message to it. But for some reason the latter does not. There are no errors in the code, maybe someone knows what the problem is and how to make it respond to clicking?

package app;

import java.util.ArrayList;
import java.util.List;

import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

public class Roric extends TelegramLongPollingBot{

  public static void main(String[] args) {
    ApiContextInitializer.init(); 
    TelegramBotsApi botapi = new TelegramBotsApi();
    try {
      botapi.registerBot(new Roric());
    } catch (TelegramApiException e) {
      e.printStackTrace();
    }
  }

  @Override
  public String getBotUsername() {
    return "Roric_bot";
  }

  @Override
  public void onUpdateReceived(Update u) {
    Message msg = u.getMessage();
    String txt = msg.getText();
    
    if (txt.equals("/start")) {
          // Set variables
          long chat_id = u.getMessage().getChatId();
            
          SendMessage message = new SendMessage() // Create a message object object
                  .setChatId(chat_id)
                  .setText("Чего желаете ?! Для помощи /help");
          try {
              execute(message); // Sending our message object to user
          } catch (TelegramApiException e) {
              e.printStackTrace();
          }  
          
          }
    else if  (txt.equals("/help")) {
      long chat_id = u.getMessage().getChatId();
      SendMessage message = new SendMessage()
          .setChatId(chat_id)
                  .setText("Чем именно помочь ?" + "\n");
      InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
            List<List<InlineKeyboardButton>> rowsInline = new ArrayList<>();
            List<InlineKeyboardButton> rowInline = new ArrayList<>();
            rowInline.add(new InlineKeyboardButton().setText("Cникерс").setCallbackData("/snik"));
            rowInline.add(new InlineKeyboardButton().setText("Марс").setCallbackData("/mars"));
            rowInline.add(new InlineKeyboardButton().setText("Bounty").setCallbackData("/boun"));
            rowInline.add(new InlineKeyboardButton().setText("Twix").setCallbackData("/twix"));
            // Set the keyboard to the markup
            rowsInline.add(rowInline);
            // Add it to the message
            markupInline.setKeyboard(rowsInline);
            message.setReplyMarkup(markupInline);
        try {
          execute(message);
        } catch (TelegramApiException e) {
          e.printStackTrace();
        }
      }   
    else if (u.hasCallbackQuery()) {
            // Set variables
            String call_data = u.getCallbackQuery().getData();
            long message_id = u.getCallbackQuery().getMessage().getMessageId();
            long chat_id = u.getCallbackQuery().getMessage().getChatId();

            if (call_data.equals("/snik")) {
                String answer = "Есть сникерс, закидуй бабло.";
                EditMessageText new_message = new EditMessageText()
                        .setChatId(chat_id)
                        .setMessageId(toIntExact(message_id))
                        .setText(answer);
                try {
                    execute(new_message); 
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }
        }
  }   		
     
  private Integer toIntExact(long message_id) {
    return (int) message_id;
  }

  public String getBotToken() {
    return "*****my token*****";
  } }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor, 2018-09-27
@RATlius

Your problem is complex and not obvious. It is not described in those tutorials, according to which you most likely learn this Api.
I had to write a new Bot myself to understand this.
After Telegram's difficulties with the RKN, they are still trying to block it.
Connect to internet via vpn and your bot will work

A
Andrey, 2019-02-13
@Allepta

It looks like you are not specifying the ReplyMarkup parameters correctly or not at all. Requests to the telegram api are sent in json format, so just try to output this request to a string and check the syntax.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question