pengrad / java-telegram-bot-api

Telegram Bot API for Java

Home Page:https://core.telegram.org/bots

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Styled text with message entities

kosik opened this issue · comments

Hello! Telegram has the facility to style text with message entities: https://core.telegram.org/api/entities

Some of the entities are available as HTML markup: bold, underline, code and others. The total number of Telegram styled objects are 158. Most of them are not available by HTML markup.

As I see, we have com.pengrad.telegrambot.model.MessageEntity class. It seems like with the class should be possible to apply styled text in messages. In fact i did not managed do this. No markup applied. Below is code sample. Could you please advice how to succeed on it:

As per official documentation:
>> A simple, but not very efficient way of computing the entity length is converting the text to UTF-16, and then taking the byte length divided by 2 (=number of UTF-16 code units).


final String message = "00000000000";

final SendMessage request = new SendMessage(chatId, message)
        .parseMode(ParseMode.HTML).disableWebPagePreview(true)
        .disableNotification(true);

final MessageEntity[] msgEntities = new MessageEntity[]{
        new MessageEntity(MessageEntity.Type.phone_number, 0, getOffsetLength(message)) };

request.entities(msgEntities);

telegramBot.execute(request);

private int getOffsetLength(final String msg){
    try {
        final String encoded = new String(msg.getBytes("UTF-8"), "UTF-16");
        final int result = encoded.getBytes().length / 2;
        return result;

    } catch (final Exception e){}
    return 0;
}

Thank you in advance for the help

It must be because of the "00000000000" phone number, it doesn't work for me either.
But the number from the telegram doc works fine "+1-212-555-0123".
https://core.telegram.org/bots/api#messageentity

And also "+12125550123".
It should probably start with a "+"