【发布时间】:2018-07-09 20:31:35
【问题描述】:
我正在使用电报机器人 api,我想在确定的时间内向用户发送消息,但机器人需要接收“消息”才能发送内容,我的问题是: 是否可以发送模拟用户交互的更新?
我的意思是这样的: 这里我创建更新来模拟用户交互(sendUpdate) 是一种自定义方法例如这实际上不起作用
public void sendUpdate() {
//sending the update to simulate user interaction
Update upd = new Update();
//method that telegram bot api uses to reply when you send a message to the bot
onUpdateReceived(upd);
}
@Override
//Here I want to recipt my update to simulate the user interaction, and send a message witout user input
public void onUpdateReceived(Update update) {
System.out.println(update);
LOGGER.setLevel(Level.ALL);
LOGGER.addHandler(new ConsoleHandler());
LOGGER.info("2");
if (update.hasMessage() && update.getMessage().hasText()) {
// Set variables
String message_text = "Message";
long chat_id = update.getMessage().getChatId();
SendMessage message = new SendMessage()
.setChatId(chat_id)
.setText(message_text);
try {
this.sendMessage(message);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
为什么说需要接收消息才能发送消息?你为什么不能打电话给
this.sendMessage(message); -
因为我很笨哈哈 :( ,你是对的,我不知道只使用 this.sendMessage (message) 会起作用,我以为我必须使用 onUpdateReceived 方法,谢谢非常喜欢!
标签: telegram telegram-bot