Java 基于 Telegram Bot Api:如何发送表情符号?
Java based Telegram Bot Api: How to send emojis?
我正在做一个基于 Java 的电报机器人 API,但我不知道如何从 Java.
发送表情符号
我尝试使用 emoji-java 库将表情符号作为 unicode 发送,但它似乎不起作用。
那么,如何将表情符号从 Java 发送到 Telegram?
谢谢。
这是我一直在使用的代码:
String ballEmoji = "\u26BD";
String bananaEmoji = "\uD83C\uDF4C";
String koreanFlagEmoji = "\uD83C\uDDF0\uD83C\uDDF7";
表情符号列表可以从here
下载
这是一个发送表情符号作为消息的示例机器人。而且它还有带有表情符号的按钮。
import com.vdurmont.emoji.EmojiParser;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import java.util.ArrayList;
import java.util.List;
public class SimpleEmojiExample extends TelegramLongPollingBot {
private String smile_emoji = EmojiParser.parseToUnicode(":smiley: some text");
private String share_number_emoji = EmojiParser.parseToUnicode(":phone: share your number");
private String money_emoji = EmojiParser.parseToUnicode(":moneybag:");
public static void main(String[] args) {
ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi();
try {
botsApi.registerBot(new SimpleEmojiExample());
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
public void setButtons(SendMessage sendMessage) {
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
sendMessage.setReplyMarkup(replyKeyboardMarkup);
replyKeyboardMarkup.setSelective(true);
replyKeyboardMarkup.setResizeKeyboard(true);
replyKeyboardMarkup.setOneTimeKeyboad(false);
List<KeyboardRow> keyboard = new ArrayList<KeyboardRow>();
KeyboardRow keyboardFirstRow = new KeyboardRow();
keyboardFirstRow.add(smile_emoji);
keyboardFirstRow.add(smile_emoji);
KeyboardRow keyboardSecondRow = new KeyboardRow();
KeyboardButton shareNumBtn = new KeyboardButton(share_number_emoji);
shareNumBtn.setRequestContact(true);
shareNumBtn.setRequestLocation(false);
keyboardSecondRow.add(shareNumBtn);
keyboard.add(keyboardFirstRow);
keyboard.add(keyboardSecondRow);
replyKeyboardMarkup.setKeyboard(keyboard);
}
public void onUpdateReceived(Update update) {
long chat_id = update.getMessage().getChatId();
SendMessage message = new SendMessage();
message.setChatId(chat_id);
// We check if the update has a message and the message has text
if (update.hasMessage() && update.getMessage().hasText()) {
if (update.getMessage().getText().equals("/start")) {
String message_text = "Welcome to our bot! " + smile_emoji;
message.setText(message_text);
} else if (update.getMessage().getText().equals(smile_emoji)) {
message.setText("some text as response " + money_emoji);
} else {
message.setText("Order is not recognized");
}
} else if (update.getMessage().getContact() != null) {//contact is shared
message.setText("You have shared your number: " + update.getMessage().getContact().getPhoneNumber());
}
setButtons(message);
try {
sendMessage(message); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
public String getBotUsername() {
return "yourBotName";//add your own
}
public String getBotToken() {
return "yourTokenFromBotFather";//add your own
}
}
要添加到 Maven pom.xml 文件的依赖项:
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.4.4.5</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
这是机器人的样子:
在电报中输入任何表情符号;
select表情符号所在的文本区域;
复制并粘贴到 Intellij Idea 中。 IDE 会自动为它生成代码。
我正在做一个基于 Java 的电报机器人 API,但我不知道如何从 Java.
发送表情符号我尝试使用 emoji-java 库将表情符号作为 unicode 发送,但它似乎不起作用。
那么,如何将表情符号从 Java 发送到 Telegram?
谢谢。
这是我一直在使用的代码:
String ballEmoji = "\u26BD";
String bananaEmoji = "\uD83C\uDF4C";
String koreanFlagEmoji = "\uD83C\uDDF0\uD83C\uDDF7";
表情符号列表可以从here
下载这是一个发送表情符号作为消息的示例机器人。而且它还有带有表情符号的按钮。
import com.vdurmont.emoji.EmojiParser;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import java.util.ArrayList;
import java.util.List;
public class SimpleEmojiExample extends TelegramLongPollingBot {
private String smile_emoji = EmojiParser.parseToUnicode(":smiley: some text");
private String share_number_emoji = EmojiParser.parseToUnicode(":phone: share your number");
private String money_emoji = EmojiParser.parseToUnicode(":moneybag:");
public static void main(String[] args) {
ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi();
try {
botsApi.registerBot(new SimpleEmojiExample());
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
public void setButtons(SendMessage sendMessage) {
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
sendMessage.setReplyMarkup(replyKeyboardMarkup);
replyKeyboardMarkup.setSelective(true);
replyKeyboardMarkup.setResizeKeyboard(true);
replyKeyboardMarkup.setOneTimeKeyboad(false);
List<KeyboardRow> keyboard = new ArrayList<KeyboardRow>();
KeyboardRow keyboardFirstRow = new KeyboardRow();
keyboardFirstRow.add(smile_emoji);
keyboardFirstRow.add(smile_emoji);
KeyboardRow keyboardSecondRow = new KeyboardRow();
KeyboardButton shareNumBtn = new KeyboardButton(share_number_emoji);
shareNumBtn.setRequestContact(true);
shareNumBtn.setRequestLocation(false);
keyboardSecondRow.add(shareNumBtn);
keyboard.add(keyboardFirstRow);
keyboard.add(keyboardSecondRow);
replyKeyboardMarkup.setKeyboard(keyboard);
}
public void onUpdateReceived(Update update) {
long chat_id = update.getMessage().getChatId();
SendMessage message = new SendMessage();
message.setChatId(chat_id);
// We check if the update has a message and the message has text
if (update.hasMessage() && update.getMessage().hasText()) {
if (update.getMessage().getText().equals("/start")) {
String message_text = "Welcome to our bot! " + smile_emoji;
message.setText(message_text);
} else if (update.getMessage().getText().equals(smile_emoji)) {
message.setText("some text as response " + money_emoji);
} else {
message.setText("Order is not recognized");
}
} else if (update.getMessage().getContact() != null) {//contact is shared
message.setText("You have shared your number: " + update.getMessage().getContact().getPhoneNumber());
}
setButtons(message);
try {
sendMessage(message); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
public String getBotUsername() {
return "yourBotName";//add your own
}
public String getBotToken() {
return "yourTokenFromBotFather";//add your own
}
}
要添加到 Maven pom.xml 文件的依赖项:
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.4.4.5</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
这是机器人的样子:
在电报中输入任何表情符号;
select表情符号所在的文本区域;
复制并粘贴到 Intellij Idea 中。 IDE 会自动为它生成代码。