Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

tdlib

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), {});
}

}

like image 916
Sargis Avatar asked May 03 '20 19:05

Sargis


1 Answers

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
  }
}
like image 165
Azeem Avatar answered Sep 28 '22 10:09

Azeem