summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleg Morozenkov <reo7sp@users.noreply.github.com>2017-03-21 18:50:45 +0300
committerGitHub <noreply@github.com>2017-03-21 18:50:45 +0300
commit4d9e96d9c91e1faeefb67de2164c026f84be020e (patch)
tree60af55841a59d7af13bb492d248e0744478e8056
parent1c7d4c17062644d28f485b0ff6cbc213e975ac29 (diff)
parent156c087f3eeeeffb949ae6a572dd7b52815eb0e5 (diff)
Merge pull request #43 from kukin-konstantin/master
fix errors and new function
-rw-r--r--include/tgbot/Api.h4
-rw-r--r--include/tgbot/tools/FileTools.h7
-rw-r--r--src/Api.cpp15
-rw-r--r--src/EventHandler.cpp1
-rw-r--r--src/TgTypeParser.cpp2
-rw-r--r--src/tools/FileTools.cpp10
-rw-r--r--src/tools/StringTools.cpp2
7 files changed, 35 insertions, 6 deletions
diff --git a/include/tgbot/Api.h b/include/tgbot/Api.h
index b2f6911..0710f17 100644
--- a/include/tgbot/Api.h
+++ b/include/tgbot/Api.h
@@ -323,7 +323,7 @@ public:
* @param fileId File identifier to get info about
* @return A File object.
*/
- File::Ptr getFile(int32_t fileId) const;
+ File::Ptr getFile(const std::string &fileId) const;
/**
* Use this method for your bot to leave a group, supergroup or channel.
@@ -475,6 +475,8 @@ public:
*/
bool unbanChatMember(int64_t chatId, int32_t userId) const;
+ std::string downloadFile(const std::string& filePath, const std::vector<HttpReqArg>& args = std::vector<HttpReqArg>()) const;
+
private:
boost::property_tree::ptree sendRequest(const std::string& method, const std::vector<HttpReqArg>& args = std::vector<HttpReqArg>()) const;
diff --git a/include/tgbot/tools/FileTools.h b/include/tgbot/tools/FileTools.h
index 7329a8e..97fb52c 100644
--- a/include/tgbot/tools/FileTools.h
+++ b/include/tgbot/tools/FileTools.h
@@ -19,6 +19,13 @@ namespace FileTools {
*/
std::string read(const std::string& filePath);
+/**
+* Save file to disk.
+* @param filePath Path to a file
+* @return bool success
+*/
+bool write(const std::string& content, const std::string& filePath);
+
};
diff --git a/src/Api.cpp b/src/Api.cpp
index 984deb5..b7a53c1 100644
--- a/src/Api.cpp
+++ b/src/Api.cpp
@@ -310,7 +310,7 @@ Message::Ptr Api::sendVoice(int64_t chatId, const InputFile::Ptr voice, const st
if (disableNotification){
args.push_back(HttpReqArg("disable_notification", disableNotification));
}
- return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVideo", args));
+ return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoice", args));
}
Message::Ptr Api::sendVoice(int64_t chatId, const std::string& voiceId, const std::string &caption, int duration, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const {
@@ -410,7 +410,7 @@ UserProfilePhotos::Ptr Api::getUserProfilePhotos(int32_t userId, int32_t offset,
return TgTypeParser::getInstance().parseJsonAndGetUserProfilePhotos(sendRequest("getUserProfilePhotos", args));
}
-File::Ptr Api::getFile(int32_t fileId) const
+File::Ptr Api::getFile(const std::string &fileId) const
{
vector<HttpReqArg> args;
args.push_back(HttpReqArg("file_id", fileId));
@@ -681,4 +681,15 @@ ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) con
}
}
+std::string Api::downloadFile(const std::string& filePath, const std::vector<HttpReqArg>& args) const {
+ string url = "https://api.telegram.org/file/bot";
+ url += _token;
+ url += "/";
+ url += filePath;
+
+ string serverResponse = HttpClient::getInstance().makeRequest(url, args);
+
+ return serverResponse;
+}
+
}
diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp
index 8529400..d679683 100644
--- a/src/EventHandler.cpp
+++ b/src/EventHandler.cpp
@@ -3,6 +3,7 @@
//
#include "tgbot/EventHandler.h"
+#include <algorithm>
namespace TgBot {
diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp
index eeaba5e..cd30b02 100644
--- a/src/TgTypeParser.cpp
+++ b/src/TgTypeParser.cpp
@@ -270,7 +270,7 @@ Sticker::Ptr TgTypeParser::parseJsonAndGetSticker(const ptree& data) const {
result->width = data.get<int32_t>("width");
result->height = data.get<int32_t>("height");
result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb");
- result->emoji = data.get<string>("emoji");
+ result->emoji = data.get("emoji", "");
result->fileSize = data.get("file_size", 0);
return result;
}
diff --git a/src/tools/FileTools.cpp b/src/tools/FileTools.cpp
index e92e96d..14c322c 100644
--- a/src/tools/FileTools.cpp
+++ b/src/tools/FileTools.cpp
@@ -22,4 +22,14 @@ std::string read(const std::string& filePath) {
throw errno;
}
+bool write(const std::string& content, const std::string& filePath) {
+ ofstream out(filePath, ios::out | ios::binary);
+ if (out) {
+ out << content;
+ out.close();
+ return true;
+ }
+ return false;
+}
+
};
diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp
index ab7da46..0e68dcc 100644
--- a/src/tools/StringTools.cpp
+++ b/src/tools/StringTools.cpp
@@ -90,12 +90,10 @@ string generateRandomString(size_t length) {
string urlEncode(const string& value, const std::string& additionalLegitChars) {
static const string legitPunctuation = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~:";
std::stringstream ss;
- std::string t;
for (auto const &c : value) {
if ((legitPunctuation.find(c) == std::string::npos)
&& (additionalLegitChars.find(c)==std::string::npos)) {
ss << '%' << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)(unsigned char)c;
- t = ss.str();
} else {
ss << c;
}