summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleg Morozenkov <omorozenkov@gmail.com>2015-09-15 18:18:49 +0300
committerOleg Morozenkov <omorozenkov@gmail.com>2015-09-15 18:18:49 +0300
commit6a3c8387da208ac2ffa4757dfafe1600a693b24f (patch)
treea8cfbfd46643784ede5ddfc0ce6dcd5f39d533d2
parent370df9853702bf9666b8dfa2e4254b9e236b2789 (diff)
Fix formatting + some minor improvements
-rw-r--r--include/tgbot/Api.h20
-rw-r--r--include/tgbot/net/HttpReqArg.h12
-rw-r--r--include/tgbot/net/TgWebhookLocalServer.h6
-rw-r--r--include/tgbot/types/GenericChat.h2
-rw-r--r--include/tgbot/types/InputFile.h12
-rw-r--r--samples/echobot/src/main.cpp52
-rw-r--r--src/Api.cpp29
-rw-r--r--src/TgTypeParser.cpp12
-rw-r--r--src/net/HttpParser.cpp22
-rw-r--r--test/utils.h2
10 files changed, 79 insertions, 90 deletions
diff --git a/include/tgbot/Api.h b/include/tgbot/Api.h
index 1881015..0d32252 100644
--- a/include/tgbot/Api.h
+++ b/include/tgbot/Api.h
@@ -91,13 +91,13 @@ public:
/**
* Use this method to send photos.
* @param chatId Unique identifier for the message recipient — User or GroupChat id.
- * @param photo Photo to send. Id of the photo that is already on the Telegram servers.
+ * @param photoId Id of the photo that is already on the Telegram servers.
* @param caption Optional. Photo caption.
* @param replyToMessageId Optional. If the message is a reply, ID of the original message.
* @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
* @return On success, the sent message is returned.
*/
- Message::Ptr sendPhoto(int32_t chatId, const std::string& photoId, const std::string& caption = "", int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
+ Message::Ptr sendPhoto(int32_t chatId, const std::string& photoId, const std::string& caption = "", int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
/**
* Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Document).
@@ -113,13 +113,13 @@ public:
/**
* Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Document).
* @param chatId Unique identifier for the message recipient — User or GroupChat id.
- * @param audio Id of the audio that is already on the Telegram servers.
+ * @param audioId Id of the audio that is already on the Telegram servers.
* @param duration Duration of sent audio in seconds.
* @param replyToMessageId Optional. If the message is a reply, ID of the original message.
* @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
* @return On success, the sent message is returned.
*/
- Message::Ptr sendAudio(int32_t chatId, const std::string& audioId, int32_t duration = 0, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
+ Message::Ptr sendAudio(int32_t chatId, const std::string& audioId, int32_t duration = 0, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
/**
* Use this method to send general files.
@@ -134,12 +134,12 @@ public:
/**
* Use this method to send general files.
* @param chatId Unique identifier for the message recipient — User or GroupChat id.
- * @param document Id of the document that is already on the Telegram servers.
+ * @param documentId Id of the document that is already on the Telegram servers.
* @param replyToMessageId Optional. If the message is a reply, ID of the original message.
* @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
* @return On success, the sent message is returned.
*/
- Message::Ptr sendDocument(int32_t chatId, const std::string& documentId, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
+ Message::Ptr sendDocument(int32_t chatId, const std::string& documentId, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
/**
* Use this method to send .webp stickers.
@@ -154,12 +154,12 @@ public:
/**
* Use this method to send .webp stickers.
* @param chatId Unique identifier for the message recipient — User or GroupChat id.
- * @param sticker Id of the sticker that is already on the Telegram servers.
+ * @param stickerId Id of the sticker that is already on the Telegram servers.
* @param replyToMessageId Optional. If the message is a reply, ID of the original message.
* @param replyMarkup Optional. Additional interface options. A object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
* @return On success, the sent message is returned.
*/
- Message::Ptr sendSticker(int32_t chatId, const std::string& stickerId, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
+ Message::Ptr sendSticker(int32_t chatId, const std::string& stickerId, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
/**
* Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document).
@@ -174,12 +174,12 @@ public:
/**
* Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document).
* @param chatId Unique identifier for the message recipient — User or GroupChat id.
- * @param video Id of the video that is already on the Telegram servers.
+ * @param videoId Id of the video that is already on the Telegram servers.
* @param replyToMessageId Optional. If the message is a reply, ID of the original message.
* @param replyMarkup Optional. Additional interface options. A object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
* @return On success, the sent message is returned.
*/
- Message::Ptr sendVideo(int32_t chatId, const std::string& videoId, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
+ Message::Ptr sendVideo(int32_t chatId, const std::string& videoId, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const;
/**
* Use this method to send point on the map.
diff --git a/include/tgbot/net/HttpReqArg.h b/include/tgbot/net/HttpReqArg.h
index 683c2b0..f0e4492 100644
--- a/include/tgbot/net/HttpReqArg.h
+++ b/include/tgbot/net/HttpReqArg.h
@@ -37,8 +37,8 @@ class HttpReqArg {
public:
template<typename T>
- HttpReqArg(const std::string& name, const T& value, bool isFile = false, const std::string& mimeType = "text/plain", std::string fileName = "") :
- name(name), value(boost::lexical_cast<std::string>(value)), isFile(isFile), mimeType(mimeType), fileName(fileName)
+ HttpReqArg(const std::string& name, const T& value, bool isFile = false, const std::string& mimeType = "text/plain", const std::string& fileName = "") :
+ name(name), value(boost::lexical_cast<std::string>(value)), isFile(isFile), mimeType(mimeType), fileName(fileName)
{
}
@@ -62,10 +62,10 @@ public:
*/
std::string mimeType = "text/plain";
- /**
- * Should be set if an argument value hold some file contents
- */
- std::string fileName;
+ /**
+ * Should be set if an argument value hold some file contents
+ */
+ std::string fileName;
};
}
diff --git a/include/tgbot/net/TgWebhookLocalServer.h b/include/tgbot/net/TgWebhookLocalServer.h
index 770c66c..67268f5 100644
--- a/include/tgbot/net/TgWebhookLocalServer.h
+++ b/include/tgbot/net/TgWebhookLocalServer.h
@@ -23,7 +23,7 @@
#ifndef TGBOT_TGWEBHOOKTCPSERVER_H
#define TGBOT_TGWEBHOOKTCPSERVER_H
-# ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
+#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
#include "tgbot/net/TgWebhookServer.h"
@@ -48,5 +48,7 @@ public:
};
}
-# endif
+
+#endif //BOOST_ASIO_HAS_LOCAL_SOCKETS
+
#endif //TGBOT_TGWEBHOOKTCPSERVER_H
diff --git a/include/tgbot/types/GenericChat.h b/include/tgbot/types/GenericChat.h
index f1fe6a1..2d91281 100644
--- a/include/tgbot/types/GenericChat.h
+++ b/include/tgbot/types/GenericChat.h
@@ -39,7 +39,7 @@ public:
virtual ~GenericChat() { }
/**
- * Unique identifier for this user, bot or group chat
+ * Unique identifier for this user, bot or group chat
*/
int32_t id;
};
diff --git a/include/tgbot/types/InputFile.h b/include/tgbot/types/InputFile.h
index 1228885..f2403e3 100644
--- a/include/tgbot/types/InputFile.h
+++ b/include/tgbot/types/InputFile.h
@@ -36,10 +36,6 @@ namespace TgBot {
class InputFile {
public:
- InputFile() {
- fileName = "set_file_name.ext";
- }
-
typedef std::shared_ptr<InputFile> Ptr;
/**
@@ -52,10 +48,10 @@ public:
*/
std::string mimeType;
- /**
- * File name.
- */
- std::string fileName;
+ /**
+ * File name.
+ */
+ std::string fileName;
};
}
diff --git a/samples/echobot/src/main.cpp b/samples/echobot/src/main.cpp
index f4a1a31..05806d2 100644
--- a/samples/echobot/src/main.cpp
+++ b/samples/echobot/src/main.cpp
@@ -32,34 +32,34 @@ using namespace TgBot;
bool sigintGot = false;
int main() {
- signal(SIGINT, [](int s) {
- printf("SIGINT got");
- sigintGot = true;
- });
+ signal(SIGINT, [](int s) {
+ printf("SIGINT got");
+ sigintGot = true;
+ });
- Bot bot("PLACE YOUR TOKEN HERE");
- bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
- bot.getApi().sendMessage(message->chat->id, "Hi!");
- });
- bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
- printf("User wrote %s\n", message->text.c_str());
- if (StringTools::startsWith(message->text, "/start")) {
- return;
- }
- bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
- });
+ Bot bot("PLACE YOUR TOKEN HERE");
+ bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
+ bot.getApi().sendMessage(message->chat->id, "Hi!");
+ });
+ bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
+ printf("User wrote %s\n", message->text.c_str());
+ if (StringTools::startsWith(message->text, "/start")) {
+ return;
+ }
+ bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
+ });
- try {
- printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
+ try {
+ printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
- TgLongPoll longPoll(bot);
- while (!sigintGot) {
- printf("Long poll started\n");
- longPoll.start();
- }
- } catch (exception& e) {
- printf("error: %s\n", e.what());
- }
+ TgLongPoll longPoll(bot);
+ while (!sigintGot) {
+ printf("Long poll started\n");
+ longPoll.start();
+ }
+ } catch (exception& e) {
+ printf("error: %s\n", e.what());
+ }
- return 0;
+ return 0;
}
diff --git a/src/Api.cpp b/src/Api.cpp
index 237cf88..4db54e9 100644
--- a/src/Api.cpp
+++ b/src/Api.cpp
@@ -65,7 +65,7 @@ Message::Ptr Api::forwardMessage(int32_t chatId, int32_t fromChatId, int32_t mes
Message::Ptr Api::sendPhoto(int32_t chatId, const InputFile::Ptr& photo, const string& caption, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("photo", photo->data, true, photo->mimeType, photo->fileName));
+ args.push_back(HttpReqArg("photo", photo->data, true, photo->mimeType, photo->fileName));
if (!caption.empty()) {
args.push_back(HttpReqArg("caption", caption));
}
@@ -81,7 +81,7 @@ Message::Ptr Api::sendPhoto(int32_t chatId, const InputFile::Ptr& photo, const s
Message::Ptr Api::sendPhoto(int32_t chatId, const string& photoId, const string& caption, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("photo", photoId));
+ args.push_back(HttpReqArg("photo", photoId));
if (!caption.empty()) {
args.push_back(HttpReqArg("caption", caption));
}
@@ -97,7 +97,7 @@ Message::Ptr Api::sendPhoto(int32_t chatId, const string& photoId, const string&
Message::Ptr Api::sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t duration, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("audio", audio->data, true, audio->mimeType, audio->fileName));
+ args.push_back(HttpReqArg("audio", audio->data, true, audio->mimeType, audio->fileName));
if (duration) {
args.push_back(HttpReqArg("duration", duration));
}
@@ -113,7 +113,7 @@ Message::Ptr Api::sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t
Message::Ptr Api::sendAudio(int32_t chatId, const string& audioId, int32_t duration, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("audio", audioId));
+ args.push_back(HttpReqArg("audio", audioId));
if (duration) {
args.push_back(HttpReqArg("duration", duration));
}
@@ -129,7 +129,7 @@ Message::Ptr Api::sendAudio(int32_t chatId, const string& audioId, int32_t durat
Message::Ptr Api::sendDocument(int32_t chatId, const InputFile::Ptr& document, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("document", document->data, true, document->mimeType, document->fileName));
+ args.push_back(HttpReqArg("document", document->data, true, document->mimeType, document->fileName));
if (replyToMessageId) {
args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId));
}
@@ -155,7 +155,7 @@ Message::Ptr Api::sendDocument(int32_t chatId, const string& document, int32_t r
Message::Ptr Api::sendSticker(int32_t chatId, const InputFile::Ptr& sticker, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("sticker", sticker->data, true, sticker->mimeType, sticker->fileName));
+ args.push_back(HttpReqArg("sticker", sticker->data, true, sticker->mimeType, sticker->fileName));
if (replyToMessageId) {
args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId));
}
@@ -168,7 +168,7 @@ Message::Ptr Api::sendSticker(int32_t chatId, const InputFile::Ptr& sticker, int
Message::Ptr Api::sendSticker(int32_t chatId, const string& stickerId, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("sticker", stickerId));
+ args.push_back(HttpReqArg("sticker", stickerId));
if (replyToMessageId) {
args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId));
}
@@ -181,7 +181,7 @@ Message::Ptr Api::sendSticker(int32_t chatId, const string& stickerId, int32_t r
Message::Ptr Api::sendVideo(int32_t chatId, const InputFile::Ptr& video, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("video", video->data, true, video->mimeType, video->fileName));
+ args.push_back(HttpReqArg("video", video->data, true, video->mimeType, video->fileName));
if (replyToMessageId) {
args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId));
}
@@ -194,7 +194,7 @@ Message::Ptr Api::sendVideo(int32_t chatId, const InputFile::Ptr& video, int32_t
Message::Ptr Api::sendVideo(int32_t chatId, const string& videoId, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
- args.push_back(HttpReqArg("video", videoId));
+ args.push_back(HttpReqArg("video", videoId));
if (replyToMessageId) {
args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId));
}
@@ -262,20 +262,17 @@ ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) con
url += method;
string serverResponse = HttpClient::getInstance().makeRequest(url, args);
if (serverResponse.find("<html>") != serverResponse.npos) {
- throw TgException("Bad request");
+ throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
}
ptree result = TgTypeParser::getInstance().parseJson(serverResponse);
try {
- if (result.get<bool>("ok")) {
- if(method != "getMe" && method != "sendMessage" && method != "sendDocument")
- return result;
- else
- return result.find("result")->second;
+ if (result.get<bool>("ok", false)) {
+ return result.get_child("result");
} else {
throw TgException(result.get("description", ""));
}
} catch (boost::property_tree::ptree_error& e) {
- throw TgException("");
+ throw TgException("tgbot-cpp library can't parse json response. " + e.what());
}
}
diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp
index d4f4340..141aebd 100644
--- a/src/TgTypeParser.cpp
+++ b/src/TgTypeParser.cpp
@@ -187,9 +187,7 @@ string TgTypeParser::parseAudio(const Audio::Ptr& object) const {
Document::Ptr TgTypeParser::parseJsonAndGetDocument(const ptree& data) const {
Document::Ptr result(new Document);
result->fileId = data.get<string>("file_id");
- if(data.find("thumb") != data.not_found()) {
- result->thumb = parseJsonAndGetPhotoSize(data.find("thumb")->second);
- }
+ result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb");
result->fileName = data.get("file_name", "");
result->mimeType = data.get("mime_type", "");
result->fileSize = data.get("file_size", 0);
@@ -217,9 +215,7 @@ Sticker::Ptr TgTypeParser::parseJsonAndGetSticker(const ptree& data) const {
result->fileId = data.get<string>("file_id");
result->width = data.get<int32_t>("width");
result->height = data.get<int32_t>("height");
- if(data.find("thumb") != data.not_found()) {
- result->thumb = parseJsonAndGetPhotoSize(data.find("thumb")->second);
- }
+ result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb");
result->fileSize = data.get("file_size", 0);
return result;
}
@@ -246,9 +242,7 @@ Video::Ptr TgTypeParser::parseJsonAndGetVideo(const ptree& data) const {
result->width = data.get<int32_t>("width");
result->height = data.get<int32_t>("height");
result->duration = data.get<int32_t>("duration");
- if(data.find("thumb") != data.not_found()) {
- result->thumb = parseJsonAndGetPhotoSize(data.find("thumb")->second);
- }
+ result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb");
result->mimeType = data.get("mime_type", "");
result->fileSize = data.get("file_size", 0);
return result;
diff --git a/src/net/HttpParser.cpp b/src/net/HttpParser.cpp
index 3ea95ec..7c4f9e2 100644
--- a/src/net/HttpParser.cpp
+++ b/src/net/HttpParser.cpp
@@ -63,16 +63,16 @@ string HttpParser::generateRequest(const Url& url, const vector<HttpReqArg>& arg
string bondary = generateMultipartBoundary(args);
if (bondary.empty()) {
result += "Content-Type: application/x-www-form-urlencoded\r\n";
- requestData = generateWwwFormUrlencoded(args);
+ requestData = generateWwwFormUrlencoded(args);
} else {
result += "Content-Type: multipart/form-data; boundary=";
result += bondary;
- result += "\r\n";
+ result += "\r\n";
requestData = generateMultipartFormData(args, bondary);
- }
+ }
- result += "Content-Length: ";
- result += lexical_cast<string>(requestData.length());
+ result += "Content-Length: ";
+ result += lexical_cast<string>(requestData.length());
result += "\r\n\r\n";
result += requestData;
}
@@ -86,11 +86,11 @@ string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, con
result += bondary;
result += "\r\nContent-Disposition: form-data; name=\"";
result += item.name;
- if(item.isFile) {
- result += "\"; filename=\"" + item.fileName;
- }
+ if (item.isFile) {
+ result += "\"; filename=\"" + item.fileName;
+ }
result += "\"\r\n";
- if (item.isFile) {
+ if (item.isFile) {
result += "Content-Type: ";
result += item.mimeType;
result += "\r\n";
@@ -99,7 +99,7 @@ string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, con
result += item.value;
result += "\r\n";
}
- result += "--" + bondary + "--";
+ result += "--" + bondary + "--";
return result;
}
@@ -107,7 +107,7 @@ string HttpParser::generateMultipartBoundary(const vector<HttpReqArg>& args) {
string result;
srand((unsigned int) time(nullptr));
for (const HttpReqArg& item : args) {
- if (item.isFile) {
+ if (item.isFile) {
while (result.empty() || item.value.find(result) != item.value.npos) {
result += StringTools::generateRandomString(4);
}
diff --git a/test/utils.h b/test/utils.h
index 485a40e..31f9e25 100644
--- a/test/utils.h
+++ b/test/utils.h
@@ -53,7 +53,7 @@ std::string diff(const std::string& test, const std::string& expected) {
template<typename T>
std::string diff(const T& test, const T& expected, std::string (*toStringFunc)(const typename T::value_type&)) {
std::string result;
- result += "\n*** BEGIN *** Count: t=";
+ result += "\n*** BEGIN *** Count: t=";
result += boost::lexical_cast<std::string>(test.size());
result += " e=";
result += boost::lexical_cast<std::string>(expected.size());