Java based Telegram Bot Api: How to send emojis?
Asked Answered
L

3

8

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.

Loralyn answered 23/12, 2015 at 10:14 Comment(1)
If you can make a simple example showing how you used the emoji-java library, that would be helpful.Semiaquatic
B
6

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

Blouson answered 14/6, 2017 at 6:52 Comment(0)
B
8

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

Blimey answered 20/2, 2016 at 13:30 Comment(0)
B
6

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

Blouson answered 14/6, 2017 at 6:52 Comment(0)
M
4
  1. type any emoji in telegram;

  2. select text area where emoji is;

  3. copy it and paste into Intellij Idea. The IDE will automatically generate code for it.

Megalocardia answered 27/6, 2020 at 15:14 Comment(1)
Thank you, that was a nice trick. Copying it inside a string quotes is directly converting it to the code!!Caitiff

© 2022 - 2024 — McMap. All rights reserved.