Skip to content

Commit d336b7a

Browse files
committed
README.md, OptionalNullPtr<>, potential ub in samples
1 parent 42567f5 commit d336b7a

File tree

15 files changed

+26
-27
lines changed

15 files changed

+26
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int main() {
3737
bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
3838
});
3939
try {
40-
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
40+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
4141
TgBot::TgLongPoll longPoll(bot);
4242
while (true) {
4343
printf("Long poll started\n");

include/tgbot/Optional.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
namespace TgBot {
77

8+
// Optional via boost::optional
89
template<typename T>
9-
using Optional = boost::optional<T>;
10+
using Optional = boost::optional<T>;
1011

11-
// use for: OptionalPtr<std::shared/unique_ptr<TYPE>>
12-
// for pointers, we assume optional value == nullptr (or not owned, etc)
12+
// Optional is nullptr (for std::shared/unique_ptr<> etc)
1313
template<typename T>
14-
using OptionalPtr = T;
14+
using OptionalNullPtr = T;
1515

1616
template<typename T>
17-
using Required = T;
17+
using Required = T;
1818

1919
}
2020

include/tgbot/TgTypeParser.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,19 +899,15 @@ class TGBOT_API TgTypeParser {
899899

900900
private:
901901
inline void removeLastComma(std::string& input) const {
902-
if (!input.empty() && input.back() == ',') input.erase(input.length() - 1);
902+
if (!input.empty() && input.back() == ',') input.pop_back();
903903
}
904904

905905
template<typename T>
906906
inline void appendToJson(std::string& json, const std::string& varName, const Optional<T>& value) const {
907907
if (!value) {
908908
return;
909909
}
910-
json += '"';
911-
json += varName;
912-
json += R"(":)";
913-
json += *value;
914-
json += ',';
910+
appendToJson(json, varName, *value);
915911
}
916912

917913
template<typename T>

include/tgbot/types/InlineKeyboardButton.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ class InlineKeyboardButton {
4747
* The Web App will be able to send an arbitrary message on behalf of the user using the method Api::answerWebAppQuery.
4848
* Available only in private chats between a user and the bot.
4949
*/
50-
OptionalPtr<WebAppInfo::Ptr> webApp;
50+
OptionalNullPtr<WebAppInfo::Ptr> webApp;
5151

5252
/**
5353
* @brief Optional. An HTTPS URL used to automatically authorize the user.
5454
*
5555
* Can be used as a replacement for the [Telegram Login Widget](https://core.telegram.org/widgets/login).
5656
*/
57-
OptionalPtr<LoginUrl::Ptr> loginUrl;
57+
OptionalNullPtr<LoginUrl::Ptr> loginUrl;
5858

5959
/**
6060
* @brief Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field.
@@ -74,14 +74,14 @@ class InlineKeyboardButton {
7474
/**
7575
* @brief Optional. If set, pressing the button will prompt the user to select one of their chats of the specified type, open that chat and insert the bot's username and the specified inline query in the input field
7676
*/
77-
OptionalPtr<SwitchInlineQueryChosenChat::Ptr> switchInlineQueryChosenChat;
77+
OptionalNullPtr<SwitchInlineQueryChosenChat::Ptr> switchInlineQueryChosenChat;
7878

7979
/**
8080
* @brief Optional. Description of the game that will be launched when the user presses the button.
8181
*
8282
* NOTE: This type of button must always be the first button in the first row.
8383
*/
84-
OptionalPtr<CallbackGame::Ptr> callbackGame;
84+
OptionalNullPtr<CallbackGame::Ptr> callbackGame;
8585

8686
/**
8787
* @brief Optional. Specify True, to send a [Pay button](https://core.telegram.org/bots/api#payments).

include/tgbot/types/MessageEntity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MessageEntity {
5555
/**
5656
* @brief Optional. For Type::TextMention only, the mentioned user
5757
*/
58-
OptionalPtr<User::Ptr> user;
58+
OptionalNullPtr<User::Ptr> user;
5959

6060
/**
6161
* @brief Optional. For Type::Pre only, the programming language of the entity text

samples/echobot-curl-client/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main() {
3333
});
3434

3535
try {
36-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
36+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
3737
bot.getApi().deleteWebhook();
3838

3939
TgLongPoll longPoll(bot);

samples/echobot-setmycommands/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int main() {
5959
});
6060

6161
try {
62-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
62+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
6363
bot.getApi().deleteWebhook();
6464

6565
TgLongPoll longPoll(bot);

samples/echobot-submodule/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int main() {
3131
});
3232

3333
try {
34-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
34+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
3535
bot.getApi().deleteWebhook();
3636

3737
TgLongPoll longPoll(bot);

samples/echobot-webhook-server/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main() {
3333
});
3434

3535
try {
36-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
36+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
3737

3838
TgWebhookTcpServer webhookServer(8080, bot);
3939

samples/echobot/src/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ int main() {
3131
});
3232

3333
try {
34-
printf("Bot username: %s\n", bot.getApi().getMe()->username.value_or(string{"unknown"}).c_str());
34+
auto user = bot.getApi().getMe();
35+
printf("Bot name: %s, username: %s\n",
36+
user->firstName.c_str(),
37+
user->username.value_or(string{"unknown"}).c_str());
3538
bot.getApi().deleteWebhook();
3639

3740
TgLongPoll longPoll(bot);

0 commit comments

Comments
 (0)