summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt18
-rw-r--r--include/tgbot/tools/StringTools.h3
-rw-r--r--samples/echobot/src/main.cpp6
-rw-r--r--src/Api.cpp (renamed from src/tgbot/Api.cpp)32
-rw-r--r--src/TgException.cpp (renamed from src/tgbot/TgException.cpp)2
-rw-r--r--src/TgTypeParser.cpp (renamed from src/tgbot/TgTypeParser.cpp)2
-rw-r--r--src/net/HttpClient.cpp (renamed from src/tgbot/net/HttpClient.cpp)6
-rw-r--r--src/net/HttpParser.cpp (renamed from src/tgbot/net/HttpParser.cpp)2
-rw-r--r--src/net/TgLongPoll.cpp (renamed from src/tgbot/net/TgLongPoll.cpp)2
-rw-r--r--src/net/Url.cpp (renamed from src/tgbot/net/Url.cpp)9
-rw-r--r--src/tools/StringTools.cpp (renamed from src/tgbot/tools/StringTools.cpp)6
11 files changed, 46 insertions, 42 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ba9ae76..8fd3321 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,16 +7,16 @@ option(ENABLE_SAMPLES "Set to ON to enable building of samples" OFF)
### sources
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
-include_directories("${PROJECT_SOURCE_DIR}/include")
+include_directories(include)
set(SRC_LIST
- src/tgbot/Api.cpp
- src/tgbot/TgTypeParser.cpp
- src/tgbot/TgException.cpp
- src/tgbot/net/Url.cpp
- src/tgbot/net/HttpClient.cpp
- src/tgbot/net/HttpParser.cpp
- src/tgbot/net/TgLongPoll.cpp
- src/tgbot/tools/StringTools.cpp
+ src/Api.cpp
+ src/TgTypeParser.cpp
+ src/TgException.cpp
+ src/net/Url.cpp
+ src/net/HttpClient.cpp
+ src/net/HttpParser.cpp
+ src/net/TgLongPoll.cpp
+ src/tools/StringTools.cpp
)
### libs
diff --git a/include/tgbot/tools/StringTools.h b/include/tgbot/tools/StringTools.h
index 0a01b0d..a8b4af1 100644
--- a/include/tgbot/tools/StringTools.h
+++ b/include/tgbot/tools/StringTools.h
@@ -63,9 +63,10 @@ std::string generateRandomString(size_t length);
/**
* Performs url encode.
* @param value Source url string
+ * @param additionalLegitChars Optional. String of chars which will be not encoded in source url string.
* @return Encoded url string
*/
-std::string urlEncode(const std::string& value);
+std::string urlEncode(const std::string& value, const std::string additionalLegitChars = "");
/**
* Performs url decode.
diff --git a/samples/echobot/src/main.cpp b/samples/echobot/src/main.cpp
index 7b8d880..61f4019 100644
--- a/samples/echobot/src/main.cpp
+++ b/samples/echobot/src/main.cpp
@@ -26,8 +26,6 @@
#include <tgbot/Bot.h>
#include <tgbot/net/TgLongPoll.h>
-#include <tgbot/TgException.h>
-#include <tgbot/tools/StringTools.h>
using namespace std;
using namespace TgBot;
@@ -52,7 +50,6 @@ int main() {
bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
});
- printf("try {\n");
try {
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
@@ -61,10 +58,9 @@ int main() {
printf("Long poll started\n");
longPoll.start();
}
- } catch (TgException& e) {
+ } catch (exception& e) {
printf("error: %s\n", e.what());
}
- printf("}\n");
return 0;
}
diff --git a/src/tgbot/Api.cpp b/src/Api.cpp
index 913401b..0c04081 100644
--- a/src/tgbot/Api.cpp
+++ b/src/Api.cpp
@@ -20,7 +20,7 @@
* SOFTWARE.
*/
-#include "Api.h"
+#include "tgbot/Api.h"
#include "tgbot/TgTypeParser.h"
#include "tgbot/TgException.h"
@@ -94,7 +94,7 @@ Message::Ptr Api::sendPhoto(int32_t chatId, const string& photo, const string& c
return TgTypeParser::getInstance().parseMessage(sendRequest("sendPhoto", args).find("result")->second);
}
-Message::Ptr Api::sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t duration = 0, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
+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));
@@ -110,7 +110,7 @@ Message::Ptr Api::sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t
return TgTypeParser::getInstance().parseMessage(sendRequest("sendAudio", args).find("result")->second);
}
-Message::Ptr Api::sendAudio(int32_t chatId, const string& audio, int32_t duration = 0, int32_t replyToMessageId, const GenericReply::Ptr& replyMarkup) const {
+Message::Ptr Api::sendAudio(int32_t chatId, const string& 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));
@@ -259,17 +259,21 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st
std::string url = "https://api.telegram.org/bot";
url += _token;
url += "/";
- url += method;
- try {
- ptree result = TgTypeParser::getInstance().parseJson(HttpClient::getInstance().makeRequest(url, args));
- if (result.get<bool>("ok")) {
- return result;
- } else {
- throw TgException(result.get("description", ""));
- }
- } catch (boost::property_tree::ptree_error& e) {
- return ptree();
- }
+ url += method;
+ string serverResponse = HttpClient::getInstance().makeRequest(url, args);
+ if (serverResponse.find("<html>") != serverResponse.npos) {
+ throw TgException("Bad request");
+ }
+ ptree result = TgTypeParser::getInstance().parseJson(serverResponse);
+ try {
+ if (result.get<bool>("ok")) {
+ return result;
+ } else {
+ throw TgException(result.get("description", ""));
+ }
+ } catch (boost::property_tree::ptree_error& e) {
+ throw TgException("");
+ }
}
}
diff --git a/src/tgbot/TgException.cpp b/src/TgException.cpp
index 2c8263e..0c118ec 100644
--- a/src/tgbot/TgException.cpp
+++ b/src/TgException.cpp
@@ -20,7 +20,7 @@
* SOFTWARE.
*/
-#include "TgException.h"
+#include "tgbot/TgException.h"
namespace TgBot {
diff --git a/src/tgbot/TgTypeParser.cpp b/src/TgTypeParser.cpp
index 70588da..9a1f93a 100644
--- a/src/tgbot/TgTypeParser.cpp
+++ b/src/TgTypeParser.cpp
@@ -20,7 +20,7 @@
* SOFTWARE.
*/
-#include "TgTypeParser.h"
+#include "tgbot/TgTypeParser.h"
using namespace std;
using namespace boost::property_tree;
diff --git a/src/tgbot/net/HttpClient.cpp b/src/net/HttpClient.cpp
index fefeac5..5027b09 100644
--- a/src/tgbot/net/HttpClient.cpp
+++ b/src/net/HttpClient.cpp
@@ -20,7 +20,7 @@
* SOFTWARE.
*/
-#include "HttpClient.h"
+#include "tgbot/net/HttpClient.h"
#include <boost/asio/ssl.hpp>
@@ -46,9 +46,6 @@ string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) {
connect(socket.lowest_layer(), resolver.resolve(query));
-// boost::asio::socket_base::keep_alive keepAliveOption(true);
-// socket.lowest_layer().set_option(keepAliveOption);
-
socket.set_verify_mode(ssl::verify_none);
socket.set_verify_callback(ssl::rfc2818_verification(url.host));
socket.handshake(ssl::stream<tcp::socket>::client);
@@ -62,7 +59,6 @@ string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) {
while (!error) {
size_t bytes = read(socket, buffer(buff), error);
response += string(buff, bytes);
- printf("%s", string(buff, bytes).c_str());
}
return HttpParser::getInstance().parseResponse(response);
diff --git a/src/tgbot/net/HttpParser.cpp b/src/net/HttpParser.cpp
index 070c630..ae9de62 100644
--- a/src/tgbot/net/HttpParser.cpp
+++ b/src/net/HttpParser.cpp
@@ -20,7 +20,7 @@
* SOFTWARE.
*/
-#include "HttpParser.h"
+#include "tgbot/net/HttpParser.h"
#include <boost/algorithm/string.hpp>
diff --git a/src/tgbot/net/TgLongPoll.cpp b/src/net/TgLongPoll.cpp
index 6359ec6..f420e92 100644
--- a/src/tgbot/net/TgLongPoll.cpp
+++ b/src/net/TgLongPoll.cpp
@@ -20,7 +20,7 @@
* SOFTWARE.
*/
-#include "TgLongPoll.h"
+#include "tgbot/net/TgLongPoll.h"
namespace TgBot {
diff --git a/src/tgbot/net/Url.cpp b/src/net/Url.cpp
index 7424849..c773f3b 100644
--- a/src/tgbot/net/Url.cpp
+++ b/src/net/Url.cpp
@@ -20,7 +20,9 @@
* SOFTWARE.
*/
-#include "Url.h"
+#include "tgbot/net/Url.h"
+
+#include "tgbot/tools/StringTools.h"
using namespace std;
@@ -73,6 +75,11 @@ Url::Url(const string& url) {
fragment += c;
}
}
+
+ host = StringTools::urlEncode(host, ".");
+ path = StringTools::urlEncode(path, "/");
+ query = StringTools::urlEncode(query, "&");
+ fragment = StringTools::urlEncode(fragment);
}
}
diff --git a/src/tgbot/tools/StringTools.cpp b/src/tools/StringTools.cpp
index de3bbb8..188e71c 100644
--- a/src/tgbot/tools/StringTools.cpp
+++ b/src/tools/StringTools.cpp
@@ -20,7 +20,7 @@
* SOFTWARE.
*/
-#include "StringTools.h"
+#include "tgbot/tools/StringTools.h"
#include <stdlib.h>
#include <iomanip>
@@ -78,13 +78,13 @@ string generateRandomString(size_t length) {
return result;
}
-string urlEncode(const string& value) {
+string urlEncode(const string& value, const std::string additionalLegitChars) {
static const string legitPunctuation = "-_.~";
ostringstream result;
result.fill('0');
result << hex;
for (const char& c : value) {
- if (isalnum(c) || legitPunctuation.find(c) != legitPunctuation.npos) {
+ if (isalnum(c) || legitPunctuation.find(c) != legitPunctuation.npos || additionalLegitChars.find(c) != additionalLegitChars.npos) {
result << c;
} else {
result << '%' << setw(2) << int((unsigned char) c);