【问题标题】:long pulling bot telegram fail with making a test长拉机器人电报无法进行测试
【发布时间】:2018-05-14 04:30:38
【问题描述】:

所以,伙计们,我正在做一个电报长轮询机器人,通过 Java,通过电报机器人 API。 我为测试做了一个整数,在回答好之后做+1,而整数没有,而答案是错误的。

public void onUpdateReceived(Update update) {
    int i=0;

在更新接收的最开始。 当用户开始测试时,他会看到一个带有第一个问题和 4 个答案的标记键盘

      else if (message_text.equals("test"))
        {
            SendMessage message = new SendMessage() // Create a message object object
                    .setChatId(chat_id)
                    .setText("Test");
            // Create ReplyKeyboardMarkup object
            ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
            // Create the keyboard (list of keyboard rows)
            List<KeyboardRow> keyboard = new ArrayList<>();
            // Create a keyboard row
            KeyboardRow row = new KeyboardRow();
            // Set each button, you can also use KeyboardButton objects if you need something else than text
            row.add("1. М");
            row.add("2. end");

            // Add the first row to the keyboard
            keyboard.add(row);
            // Create another keyboard row
            row = new KeyboardRow();
            row.add("3. К");
            row.add("4. Т");
            keyboard.add(row);
            // Set the keyboard to the markup
            keyboardMarkup.setKeyboard(keyboard);
            // Add it to the message
            message.setReplyMarkup(keyboardMarkup);
            try
            {
                sendMessage(message); // Call method to send the photo
            }
            catch (TelegramApiException e)
            {
                e.printStackTrace();

            }
        }
        else if (message_text.equals("1. М"))
        {
            i=i+1;
            SendMessage message = new SendMessage() // Create a message object object
                    .setChatId(chat_id)
                    .setText("Test");
            ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
            List<KeyboardRow> keyboard = new ArrayList<>();
            KeyboardRow row = new KeyboardRow();
            row.add("1. М");
            row.add("2. end");
            keyboard.add(row);
            row = new KeyboardRow();
            row.add("3. К");
            row.add("4. Т");
            keyboard.add(row);
            keyboardMarkup.setKeyboard(keyboard);
            message.setReplyMarkup(keyboardMarkup);
            try
            {
                sendMessage(message);
            }
            catch (TelegramApiException e)
            {
                e.printStackTrace();

            }
        }
        else if (message_text.equals("2. end"))
        {
            if (i == 1) {
                SendMessage message = new SendMessage()
                        .setChatId(chat_id)
                        .setText("roflan");

                try {
                    sendMessage(message);
                } catch (TelegramApiException e) {
                    e.printStackTrace();

                }
            }
        }

如果我要写 "!=1" ,在 "2.end" 之后它总是会显示结果。无论用户回答第一个按钮多少次,i+1 都不起作用。我的逻辑哪里有问题?

【问题讨论】:

    标签: java logic telegram telegram-bot


    【解决方案1】:

    让我们忘记 Telegram API,看看你的 int i 变量发生了什么。

    每次调用onUpdateReceived() 时,int i 都会在此方法中声明并使用值0 进行初始化。

    看起来像这样

    public class Scope {
        public static void main(String[] args) {
            System.out.println(getI());
            System.out.println(getI());
        }
        private static int getI() {
            int i = 0;
            i++;
            return i;
        }
    }
    

    输出将是

    1
    1
    

    要使您的程序按预期运行,您应该在onUpdateReceived() 范围之外声明int i。最明显的方法是创建一个静态变量。

    public class Scope {
        public static void main(String[] args) {
            System.out.println(getI());
            System.out.println(getI());
            System.out.println(getI());
            System.out.println(getI());
        }
        private static int i = 0;
        private static int getI() {
            i++;
            return i;
        }
    }
    

    输出将是

    1
    2
    3
    4
    

    所以,现在你的代码应该是这样的

    public class Bot extends TelegramLongPollingBot {
    
        private static int i = 0;
    
        public void onUpdateReceived() {
            /*...*/
            else if (update.hasMessage() && update.getMessage().hasText() && update.getMessage().getText().equals("1. M")) {
                i++;
            } else if (update.hasMessage() && update.getMessage().hasText() && update.getMessage().getText().equals("1. end")) {
                System.out.println(i);
            }
            /*...*/
        }
    
        public String getBotToken() {
            return "...";
        }
    
        public String getBotUsername() {
            return "...";
        }
    }
    

    【讨论】:

    • 谢谢,那行得通。我知道 API 没有问题,所以我在 tegs 上提出了一个逻辑问题。
    • 现在,我如何为单个 chat_id 加入它,无论哪种方式,它都适用于每个人,并且每次在我的机器人运行时。
    猜你喜欢
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多