diff options
-rw-r--r-- | include/tgbot/TgTypeParser.h | 3 | ||||
-rw-r--r-- | include/tgbot/types/Chat.h | 75 | ||||
-rw-r--r-- | include/tgbot/types/Message.h | 5 | ||||
-rw-r--r-- | src/Api.cpp | 6 | ||||
-rw-r--r-- | src/TgTypeParser.cpp | 33 |
5 files changed, 118 insertions, 4 deletions
diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 07eb1b6..2da9685 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -29,6 +29,7 @@ #include <boost/property_tree/json_parser.hpp> #include "tgbot/types/User.h" +#include "tgbot/types/Chat.h" #include "tgbot/types/GroupChat.h" #include "tgbot/types/Message.h" #include "tgbot/types/PhotoSize.h" @@ -58,6 +59,8 @@ public: static TgTypeParser& getInstance(); + Chat::Ptr parseJsonAndGetChat(const boost::property_tree::ptree& data) const; + std::string parseChat(const Chat::Ptr& object) const; User::Ptr parseJsonAndGetUser(const boost::property_tree::ptree& data) const; std::string parseUser(const User::Ptr& object) const; GroupChat::Ptr parseJsonAndGetGroupChat(const boost::property_tree::ptree& data) const; diff --git a/include/tgbot/types/Chat.h b/include/tgbot/types/Chat.h new file mode 100644 index 0000000..d05155c --- /dev/null +++ b/include/tgbot/types/Chat.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef TGBOT_CPP_CHAT_H +#define TGBOT_CPP_CHAT_H + +#include <string> +#include <memory> + +#include "tgbot/types/GenericChat.h" + +namespace TgBot { + +/** + * This object represents a Telegram Chat + * @ingroup types + */ +class Chat : public GenericChat { + +public: + typedef std::shared_ptr<Chat> Ptr; + + /** + * Type of chat : can be either + * "private", "group", "supergroup, + * or "channel" + */ + std::string type; + + /** + * Optional. Title for channels and group chat + */ + std::string title; + + /** + * Optional. Username for + * private chats and channels + */ + std::string username; + + /** + * Optional. First name of the + * other party in private chat + */ + std::string firstName; + + /** + * Optional. Last name of the + * other party in private chat + */ + std::string lastName; +}; + +} + +#endif //TGBOT_CPP_CHAT_H diff --git a/include/tgbot/types/Message.h b/include/tgbot/types/Message.h index b8dfcc6..4185052 100644 --- a/include/tgbot/types/Message.h +++ b/include/tgbot/types/Message.h @@ -27,6 +27,7 @@ #include <vector> #include <memory> +#include "tgbot/types/Chat.h" #include "tgbot/types/User.h" #include "tgbot/types/GenericChat.h" #include "tgbot/types/Message.h" @@ -65,9 +66,9 @@ public: int32_t date; /** - * Conversation the message belongs to — user in case of a private message, GroupChat in case of a group. + * Chat. */ - GenericChat::Ptr chat; + Chat::Ptr chat; /** * Optional. For forwarded messages, sender of the original message. diff --git a/src/Api.cpp b/src/Api.cpp index 9c8259f..2e3af45 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -256,14 +256,20 @@ void Api::setWebhook(const string& url) const { } ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) const { + string url = "https://api.telegram.org/bot"; url += _token; url += "/"; url += method; + + std::cout << "URL: " << url << std::endl; string serverResponse = HttpClient::getInstance().makeRequest(url, args); if (serverResponse.find("<html>") != serverResponse.npos) { throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token."); } + + std::cout << "Server Response: " << serverResponse << std::endl; + ptree result = TgTypeParser::getInstance().parseJson(serverResponse); try { if (result.get<bool>("ok", false)) { diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 6586ec6..df80ca7 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -32,6 +32,35 @@ TgTypeParser& TgTypeParser::getInstance() { return result; } +Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { + Chat::Ptr result(new Chat); + result->id = data.get<int32_t>("id"); + result->type = data.get<string>("type"); + result->title = data.get("title", ""); + result->username = data.get("username", ""); + result->firstName = data.get<string>("first_name"); + result->lastName = data.get("last_name", ""); + + return result; +} + +string TgTypeParser::parseChat(const Chat::Ptr& object) const { + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "type", object->type); + appendToJson(result, "title", object->title); + appendToJson(result, "username", object->username); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + result.erase(result.length() - 1); + result += '}'; + return result; +} + User::Ptr TgTypeParser::parseJsonAndGetUser(const ptree& data) const { User::Ptr result(new User); result->id = data.get<int32_t>("id"); @@ -81,7 +110,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { result->messageId = data.get<int32_t>("message_id"); result->from = parseJsonAndGetUser(data.find("from")->second); result->date = data.get<int32_t>("date"); - result->chat = parseJsonAndGetGenericChat(data.find("chat")->second); + result->chat = tryParseJson<Chat>(&TgTypeParser::parseJsonAndGetChat, data, "chat"); result->forwardFrom = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); result->forwardDate = data.get("forward_date", 0); result->replyToMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); @@ -112,7 +141,7 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const { appendToJson(result, "message_id", object->messageId); appendToJson(result, "from", parseUser(object->from)); appendToJson(result, "date", object->date); - appendToJson(result, "chat", parseGenericChat(object->chat)); + appendToJson(result, "chat", parseChat(object->chat)); appendToJson(result, "forward_from", parseUser(object->forwardFrom)); appendToJson(result, "forward_date", object->forwardDate); appendToJson(result, "reply_to_message", parseMessage(object->replyToMessage)); |