“Modern” C++/WinRT is the way to write rather powerful things in a compact and readable way, mixing everything you can think of together: classic C++ and libraries, UWP APIs including HTTP client, JSON, COM, ability to put code into console/desktop applications, async API model and C++20 coroutines.
Fragment of Telegram bot code snippet that echoes a message back, written with just bare Windows 10 SDK API set without external libraries, for example:
for(auto&& UpdateValue: UpdateArray) // https://core.telegram.org/bots/api#update
{
JsonObject Update = UpdateValue.GetObject();
const UINT64 UpdateIdentifier = static_cast<UINT64>(Update.GetNamedNumber(L"update_id"));
m_Context.m_NextUpdateIdentifier = UpdateIdentifier + 1;
if(Update.HasKey(L"message"))
{
JsonObject Message = Update.GetNamedObject(L"message");
m_Journal.Write(
{
L"Message",
static_cast<std::wstring>(Message.Stringify()),
});
const UINT64 MessageIdentifier = static_cast<UINT64>(Message.GetNamedNumber(L"message_id"));
JsonObject FromUser = Message.GetNamedObject(L"from");
const UINT64 FromUserIdentifier = static_cast<UINT64>(FromUser.GetNamedNumber(L"id"));
std::wstring FromUserUsername = static_cast<std::wstring>(FromUser.GetNamedString(L"username"));
#pragma region ACK
JsonObject Chat = Message.GetNamedObject(L"chat");
const UINT64 ChatIdentifier = static_cast<UINT64>(Chat.GetNamedNumber(L"id"));
{
std::wstring Text = Format(L"Hey, *@%ls*, I confirm message _%llu_\\. Send me a file now\\!", FromUserUsername.c_str(), MessageIdentifier);
Uri RequestUri(static_cast<winrt::hstring>(Format(L"https://api.telegram.org/bot%ls/sendMessage", m_Configuration.m_Token.c_str())));
JsonObject Request;
Request.Insert(L"chat_id", JsonValue::CreateNumberValue(static_cast<DOUBLE>(ChatIdentifier)));
Request.Insert(L"text", JsonValue::CreateStringValue(static_cast<winrt::hstring>(Text)));
Request.Insert(L"parse_mode", JsonValue::CreateStringValue(L"MarkdownV2"));
m_Journal.Write(
{
L"sendMessage",
L"Request",
static_cast<std::wstring>(Request.Stringify()),
});
HttpStringContent Content(Request.Stringify(), UnicodeEncoding::Utf8);
Content.Headers().ContentType(Headers::HttpMediaTypeHeaderValue(L"application/json"));
HttpResponseMessage ResponseMessage = Client.PostAsync(RequestUri, Content).get();
JsonObject Response = JsonObject::Parse(ResponseMessage.Content().ReadAsStringAsync().get());
m_Journal.Write(
{
L"sendMessage",
L"Response",
static_cast<std::wstring>(Response.Stringify()),
});
__D(Response.GetNamedBoolean(L"ok"), E_UNNAMED);
}
#pragma endregion
}
Please count me as a fan of this.