基于Java的Telegram Bot Api:如何发送表情符号?

时间:2015-12-23 10:14:13

标签: java emoji telegram-bot

我正在使用基于Java的Telegram Bot API,但我不知道如何从Java发送emojis。

我尝试使用emoji-java库将emojis作为unicode发送,但它似乎不起作用。

那么,如何将表情符号从Java发送到Telegram?

谢谢。

3 个答案:

答案 0 :(得分:5)

以下是我一直在使用的代码:

String ballEmoji = "\u26BD";
String bananaEmoji = "\uD83C\uDF4C";
String koreanFlagEmoji = "\uD83C\uDDF0\uD83C\uDDF7";

可以从here

下载表情符号列表

答案 1 :(得分:1)

这是一个示例机器人,它将emojis作为消息发送。它还有带表情符号的按钮。

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>

以下是机器人的样子:

enter image description here

答案 2 :(得分:0)

  1. 在电报中键入任何表情符号;

  2. 选择表情符号所在的文本区域;

  3. 将其复制并粘贴到Intellij Idea中。 IDE将自动为其生成代码。