TDLib: how to send bold text in the message? (С++)
Asked Answered
R

1

8

Using the official TDLib C++ example, I'm trying to send a message with formatted markdown text.

Here's my code:

auto send_message = td_api::make_object<td_api::sendMessage>();
send_message->chat_id_ = -1001424068198;
auto message_content = td_api::make_object<td_api::inputMessageText>();

std::string text = "Hello! **how are u?**";
message_content->text_ = td_api::make_object<td_api::formattedText>();     
message_content->text_->text_ = std::move(text);      
send_message->input_message_content_ = std::move(message_content);
send_query(std::move(send_message), {});

I expect to see "Hello! how are u?" but the message comes as it is written in the code, without markdown formatting applied.

I spent hours on google trying to figure out how to force TDLib to parse it.

UPDATE: SOLVED!

Thanks Azeem for help!

Using this example, the following code should send the parsed message (tested in VS 2019)

void sendMsg(INT64 chatID, INT64 ReplyTo, const char* textMsg) {
const std::string text = textMsg;
auto textParseMarkdown = td_api::make_object<td_api::textParseModeMarkdown>(2);
auto parseTextEntities = td_api::make_object<td_api::parseTextEntities>(text, std::move(textParseMarkdown));
td::Client::Request parseRequest{ 123, std::move(parseTextEntities) };
auto parseResponse = td::Client::execute(std::move(parseRequest));

if (parseResponse.object->get_id()  == td_api::formattedText::ID) {
    auto formattedText = td_api::make_object<td_api::formattedText>();
    formattedText = td_api::move_object_as<td_api::formattedText>(parseResponse.object);

    auto send_message = td_api::make_object<td_api::sendMessage>();
    send_message->chat_id_ = chatID;
    auto message_content = td_api::make_object<td_api::inputMessageText>();

    message_content->text_ = std::move(formattedText);
    send_message->input_message_content_ = std::move(message_content);
    send_message->reply_to_message_id_ = ReplyTo;
    send_query(std::move(send_message), {});
}

}

Rubble answered 3/5, 2020 at 19:27 Comment(10)
Have you tried core.telegram.org/tdlib/docs/… with core.telegram.org/tdlib/docs/…Playpen
@AlanBirtles I see thanks! But could you please point me out how to use it in the code? A bit messed with those TDLib classes:)Rubble
I think it's all in the documentation. I haven't used this library myselfPlaypen
@Sargis: Can you try this code snippet? You can explore the content of an object using td_api::to_string(object).Electromagnetic
@Electromagnetic thanks for you answer! it doesnt compile: Error C2039 'release': is not a member of 'td::Client::Response'Rubble
@Sargis: What is release? Can you please add your build commands (compile and link) in your question along with the headers?Electromagnetic
@Electromagnetic I test with official C++ example link move_object_as defined in the td_api.h as return object_ptr<ToType>(static_cast<ToType *>(from.release())); The Response actually does not contain Release. Could you please checkRubble
@Sargis: In my code snippet, response is not of object_ptr type. Its type is Response. You need to change that accordingly.Electromagnetic
@Sargis: It would be something like this: auto parseResponse = td_api::make_object<td::Client::Response>( td::Client::execute( std::move( parseRequest ) ) ); Please check on your side and change other usages accordingly.Electromagnetic
@Electromagnetic thanks a lot!! finally I got it. The right way is formattedText = td_api::move_object_as<td_api::formattedText>(parseResponse.object); so its parseResponse.object instead of parseResponse This library is so hard to understand:))Rubble
E
3

You can use td_api::textParseModeMarkdown, td_api::parseTextEntities and td::Client::execute() like this:

using namespace td;

const std::string text = "*bold* _italic_ `code`";

auto textParseMarkdown = td_api::make_object<td_api::textParseModeMarkdown>( 2 );
auto parseTextEntities = td_api::make_object<td_api::parseTextEntities>( text, std::move( textParseMarkdown ) );

td::Client::Request parseRequest { 123, std::move( parseTextEntities ) };
auto parseResponse = td::Client::execute( std::move( parseRequest ) );

auto formattedText = td_api::make_object<td_api::formattedText>();

if ( parseResponse.object->get_id() == td_api::formattedText::ID )
{
    formattedText = td_api::move_object_as<td_api::formattedText>( parseResponse.object );
}
else
{
    std::vector<td_api::object_ptr<td_api::textEntity>> entities;
    formattedText = td_api::make_object<td_api::formattedText>( text, std::move(entities) );
}

std::cout << td_api::to_string( formattedText ) << '\n';

For debugging purposes, you can use td_api::to_string() to dump the contents of an object. For example, dumping parseTextEntities like this:

std::cout << td_api::to_string( parseTextEntities ) << '\n';

would give this:

parseTextEntities {
  text = "*bold* _italic_ `code`"
  parse_mode = textParseModeMarkdown {
    version = 2
  }
}
Electromagnetic answered 8/5, 2020 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.