Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I've tried to send emojis as unicode using the emoji-java library, but it doesn't seem to work.

So, how can I send emojis from Java to Telegram?

Thank you.

like image 909
D. Fuentes Avatar asked Dec 23 '15 10:12

D. Fuentes


2 Answers

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

like image 127
Harun Avatar answered Oct 13 '22 05:10

Harun


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:

<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:

enter image description here

like image 22
valijon Avatar answered Oct 13 '22 07:10

valijon