gpt4 book ai didi

java - 长拉机器人 Telegram 因测试而失败

转载 作者:行者123 更新时间:2023-12-02 11:15:34 24 4
gpt4 key购买 nike

所以,伙计们,我正在通过 Java、 Telegram Bot API 制作一个 Telegram 长轮询机器人。我做了一个整数来进行测试,在好的答案后做+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 都不起作用。我的逻辑哪里有问题?

最佳答案

让我们忘记 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 "...";
}
}

关于java - 长拉机器人 Telegram 因测试而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com