[英]How to build a Telegram bot to suggest you pizza using java?
I created a Main java file and I added an instruction for the bot: say "I love Pizza" on a public channel I created. 我创建了一个Main java文件,并为该机器人添加了一条指令:在我创建的公共频道上说“我爱披萨”。
public class Main {
public static void main(String[] args) {
//create a new Telegram bot object to start talking with Telegram
TelegramBot bot = TelegramBotAdapter.build(“HERE YOUR API KEY”);
bot.sendMessage(“@pizzaciaopizza”, “I love Pizza”);
}
}
This worked. 这工作了。 Good start.
好的开始。 Thankfully my bot loves pizza.
幸运的是,我的机器人喜欢披萨。 I wanted to enable my bot to answer a command like "/recommendPizza" and to answer something.So how can one do this?
我想让我的机器人能够回答“ / recommendPizza”之类的命令并回答一些问题。那怎么办呢?
Any help? 有什么帮助吗?
You seem to be using https://github.com/pengrad/java-telegram-bot-api , right? 您似乎正在使用https://github.com/pengrad/java-telegram-bot-api ,对吗?
I have previously used https://github.com/rubenlagus/TelegramBots . 我以前使用过https://github.com/rubenlagus/TelegramBots 。 It provides a simple listener API to receive updates:
它提供了一个简单的侦听器API来接收更新:
public class PizzaBot {
private static final Logger LOG = Logger.getLogger(PizzaBot.class.getName());
public static void main(String... args) throws Exception {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
telegramBotsApi.registerBot(new TelegramLongPollingBot() {
@Override
public void onUpdateReceived(Update update) {
Message message = update.getMessage();
Long chatId = message.getChatId();
String input = message.getText();
if ("/recommendPizza".equals(input)) {
SendMessage request = new SendMessage();
request.setChatId(chatId.toString());
request.setText("Have a calzone!");
try {
sendMessage(request);
} catch (TelegramApiException e) {
LOG.log(Level.SEVERE, "Could not send message", e);
}
}
}
@Override
public String getBotUsername() {
return "YOUR_BOT_USERNAME";
}
@Override
public String getBotToken() {
return "YOUR_BOT_TOKEN";
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.