简体   繁体   English

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

[英]Java based Telegram Bot Api: How to send emojis?

I'm doing a Java based Telegram Bot API, but I don't know how to send emojis from Java.我正在做一个基于 Java 的 Telegram Bot API,但我不知道如何从 Java 发送表情符号。

I've tried to send emojis as unicode using the emoji-java library, but it doesn't seem to work.我尝试使用emoji-java库将表情符号作为 unicode 发送,但它似乎不起作用。

So, how can I send emojis from Java to Telegram?那么,如何将表情符号从 Java 发送到 Telegram?

Thank you.谢谢你。

Here is the code I've been using:这是我一直在使用的代码:

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

The list of emojis can be downloaded from here表情符号列表可以从这里下载

Here is a sample bot which sends emojis as message.这是一个示例机器人,它将表情符号作为消息发送。 And also it has buttons with 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
    }

}

Dependencies to add to maven pom.xml file:添加到 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>

Here is how bot looks like:这是机器人的样子:

在此处输入图片说明

  1. type any emoji in telegram;在电报中输入任何表情符号;

  2. select text area where emoji is;选择表情符号所在的文本区域;

  3. copy it and paste into Intellij Idea.将其复制并粘贴到 Intellij Idea 中。 The IDE will automatically generate code for it. IDE 将自动为其生成代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM