Skip to content

Commit

Permalink
README.md, OptionalNullPtr<>, potential ub in samples
Browse files Browse the repository at this point in the history
  • Loading branch information
uralm1 committed Nov 22, 2024
1 parent 42567f5 commit d336b7a
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main() {
bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
});
try {
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
TgBot::TgLongPoll longPoll(bot);
while (true) {
printf("Long poll started\n");
Expand Down
10 changes: 5 additions & 5 deletions include/tgbot/Optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

namespace TgBot {

// Optional via boost::optional
template<typename T>
using Optional = boost::optional<T>;
using Optional = boost::optional<T>;

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

template<typename T>
using Required = T;
using Required = T;

}

Expand Down
8 changes: 2 additions & 6 deletions include/tgbot/TgTypeParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -899,19 +899,15 @@ class TGBOT_API TgTypeParser {

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

template<typename T>
inline void appendToJson(std::string& json, const std::string& varName, const Optional<T>& value) const {
if (!value) {
return;
}
json += '"';
json += varName;
json += R"(":)";
json += *value;
json += ',';
appendToJson(json, varName, *value);
}

template<typename T>
Expand Down
8 changes: 4 additions & 4 deletions include/tgbot/types/InlineKeyboardButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class InlineKeyboardButton {
* The Web App will be able to send an arbitrary message on behalf of the user using the method Api::answerWebAppQuery.
* Available only in private chats between a user and the bot.
*/
OptionalPtr<WebAppInfo::Ptr> webApp;
OptionalNullPtr<WebAppInfo::Ptr> webApp;

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

/**
* @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.
Expand All @@ -74,14 +74,14 @@ class InlineKeyboardButton {
/**
* @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
*/
OptionalPtr<SwitchInlineQueryChosenChat::Ptr> switchInlineQueryChosenChat;
OptionalNullPtr<SwitchInlineQueryChosenChat::Ptr> switchInlineQueryChosenChat;

/**
* @brief Optional. Description of the game that will be launched when the user presses the button.
*
* NOTE: This type of button must always be the first button in the first row.
*/
OptionalPtr<CallbackGame::Ptr> callbackGame;
OptionalNullPtr<CallbackGame::Ptr> callbackGame;

/**
* @brief Optional. Specify True, to send a [Pay button](https://core.telegram.org/bots/api#payments).
Expand Down
2 changes: 1 addition & 1 deletion include/tgbot/types/MessageEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MessageEntity {
/**
* @brief Optional. For Type::TextMention only, the mentioned user
*/
OptionalPtr<User::Ptr> user;
OptionalNullPtr<User::Ptr> user;

/**
* @brief Optional. For Type::Pre only, the programming language of the entity text
Expand Down
2 changes: 1 addition & 1 deletion samples/echobot-curl-client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main() {
});

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

TgLongPoll longPoll(bot);
Expand Down
2 changes: 1 addition & 1 deletion samples/echobot-setmycommands/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main() {
});

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

TgLongPoll longPoll(bot);
Expand Down
2 changes: 1 addition & 1 deletion samples/echobot-submodule/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main() {
});

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

TgLongPoll longPoll(bot);
Expand Down
2 changes: 1 addition & 1 deletion samples/echobot-webhook-server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main() {
});

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

TgWebhookTcpServer webhookServer(8080, bot);

Expand Down
5 changes: 4 additions & 1 deletion samples/echobot/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ int main() {
});

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

TgLongPoll longPoll(bot);
Expand Down
2 changes: 1 addition & 1 deletion samples/inline-keyboard/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main() {
});

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

TgLongPoll longPoll(bot);
Expand Down
2 changes: 1 addition & 1 deletion samples/photo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main() {
});

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

TgLongPoll longPoll(bot);
Expand Down
2 changes: 1 addition & 1 deletion samples/receive-file/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main() {
});

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

TgLongPoll longPoll(bot);
Expand Down
2 changes: 1 addition & 1 deletion samples/received-text-processing/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main() {
});

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

while (true) {
Expand Down
2 changes: 1 addition & 1 deletion samples/reply-keyboard/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main() {
});

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

TgLongPoll longPoll(bot);
Expand Down

0 comments on commit d336b7a

Please sign in to comment.