diff --git a/include/telebotxx/Attachment.hpp b/include/telebotxx/Attachment.hpp index 4abdb42..86a8a54 100644 --- a/include/telebotxx/Attachment.hpp +++ b/include/telebotxx/Attachment.hpp @@ -6,212 +6,213 @@ #include #include -namespace telebotxx +namespace telebotxx { + +class Attachment { - class Attachment +public: + enum class Type { - public: - enum class Type - { - Audio, - Document, - Game, - PhotoSizeArray, - Sticker, - Video, - Voice, - Contact, - Location, - Venue - }; - - Attachment(Type type); - Attachment(const Attachment&); - Attachment(Attachment&&); - virtual ~Attachment() = 0; - - Type getType() const; - - void swap(Attachment& other) noexcept; - - private: - Type attachmentType_; + Audio, + Document, + Game, + PhotoSizeArray, + Sticker, + Video, + Voice, + Contact, + Location, + Venue }; - using AttachmentPtr = std::shared_ptr; + Attachment(Type type); + Attachment(const Attachment&); + Attachment(Attachment&&); + virtual ~Attachment() = 0; - class PhotoSize - { - public: - PhotoSize(); - PhotoSize(const PhotoSize&); - PhotoSize(PhotoSize&&); - ~PhotoSize(); + Type getType() const; - const std::string& getFileId() const; - void setFileId(const std::string& fileId); + void swap(Attachment& other) noexcept; - int getWidth() const; - void setWidth(int width); +private: + Type attachmentType_; +}; - int getHeight() const; - void setHeight(int height); +using AttachmentPtr = std::shared_ptr; - int getFileSize() const; - void setFileSize(int fileSize); +class PhotoSize +{ +public: + PhotoSize(); + PhotoSize(const PhotoSize&); + PhotoSize(PhotoSize&&); + ~PhotoSize(); - void swap(PhotoSize& other) noexcept; + const std::string& getFileId() const; + void setFileId(const std::string& fileId); - const PhotoSize& operator=(PhotoSize other) noexcept; + int getWidth() const; + void setWidth(int width); - private: - std::string fileId_; - int width_; - int height_; - int fileSize_; - }; + int getHeight() const; + void setHeight(int height); - using PhotoSizePtr = std::shared_ptr; + int getFileSize() const; + void setFileSize(int fileSize); - class PhotoSizeArray : public Attachment - { - public: - PhotoSizeArray(); - PhotoSizeArray(const PhotoSizeArray&); - PhotoSizeArray(PhotoSizeArray&&); - ~PhotoSizeArray(); + void swap(PhotoSize& other) noexcept; - const std::vector& getArray() const; - void setArray(const std::vector& array); + const PhotoSize& operator=(PhotoSize other) noexcept; - void swap(PhotoSizeArray& other) noexcept; +private: + std::string fileId_; + int width_; + int height_; + int fileSize_; +}; - const PhotoSizeArray& operator=(PhotoSizeArray other) noexcept; +using PhotoSizePtr = std::shared_ptr; - private: - std::vector array_; - }; +class PhotoSizeArray : public Attachment +{ +public: + PhotoSizeArray(); + PhotoSizeArray(const PhotoSizeArray&); + PhotoSizeArray(PhotoSizeArray&&); + ~PhotoSizeArray(); - using PhotoSizeArrayPtr = std::shared_ptr; + const std::vector& getArray() const; + void setArray(const std::vector& array); - class Audio : public Attachment - { - public: - Audio(); - Audio(const Audio&); - Audio(Audio&&); - ~Audio(); + void swap(PhotoSizeArray& other) noexcept; - const std::string& getFileId() const; - void setFileId(const std::string& fileId); + const PhotoSizeArray& operator=(PhotoSizeArray other) noexcept; - int getDuration() const; - void setDuration(int duration); +private: + std::vector array_; +}; - const std::string& getPerformer() const; - void setPerformer(const std::string& performer); +using PhotoSizeArrayPtr = std::shared_ptr; - const std::string& getTitle() const; - void setTitle(const std::string& title); +class Audio : public Attachment +{ +public: + Audio(); + Audio(const Audio&); + Audio(Audio&&); + ~Audio(); - const std::string& getMimeType() const; - void setMimeType(const std::string& mimeType); + const std::string& getFileId() const; + void setFileId(const std::string& fileId); - int getFileSize() const; - void setFileSize(int fileSize); + int getDuration() const; + void setDuration(int duration); - void swap(Audio& other) noexcept; + const std::string& getPerformer() const; + void setPerformer(const std::string& performer); - const Audio& operator=(Audio other) noexcept; + const std::string& getTitle() const; + void setTitle(const std::string& title); - private: - std::string fileId_; - int duration_; - std::string performer_; - std::string title_; - std::string mimeType_; - int fileSize_; - }; + const std::string& getMimeType() const; + void setMimeType(const std::string& mimeType); - class Document : public Attachment - { - public: - Document(); - Document(const Document&); - Document(Document&&); - ~Document(); + int getFileSize() const; + void setFileSize(int fileSize); - const std::string& getFileId() const; - void setFileId(const std::string& fileId); + void swap(Audio& other) noexcept; - const PhotoSizePtr getThumb() const; - void setThumb(const PhotoSizePtr& thumb); + const Audio& operator=(Audio other) noexcept; - const std::string& getFileName() const; - void setFileName(const std::string& fileName); +private: + std::string fileId_; + int duration_; + std::string performer_; + std::string title_; + std::string mimeType_; + int fileSize_; +}; - const std::string& getMimeType() const; - void setMimeType(const std::string& mimeType); +class Document : public Attachment +{ +public: + Document(); + Document(const Document&); + Document(Document&&); + ~Document(); - int getFileSize() const; - void setFileSize(int fileSize); + const std::string& getFileId() const; + void setFileId(const std::string& fileId); - void swap(Document& other) noexcept; + const PhotoSizePtr getThumb() const; + void setThumb(const PhotoSizePtr& thumb); - const Document& operator=(Document other) noexcept; + const std::string& getFileName() const; + void setFileName(const std::string& fileName); - private: - std::string fileId_; - PhotoSizePtr thumb_; - std::string fileName_; - std::string mimeType_; - int fileSize_; - }; + const std::string& getMimeType() const; + void setMimeType(const std::string& mimeType); - class Sticker : public Attachment - { - public: - Sticker(); - Sticker(const Sticker&); - Sticker(Sticker&&); - ~Sticker(); + int getFileSize() const; + void setFileSize(int fileSize); - const std::string& getFileId() const; - void setFileId(const std::string& fileId); + void swap(Document& other) noexcept; - int getWidth() const; - void setWidth(int width); + const Document& operator=(Document other) noexcept; - int getHeight() const; - void setHeight(int height); +private: + std::string fileId_; + PhotoSizePtr thumb_; + std::string fileName_; + std::string mimeType_; + int fileSize_; +}; - const PhotoSizePtr getThumb() const; - void setThumb(const PhotoSizePtr& thumb); +class Sticker : public Attachment +{ +public: + Sticker(); + Sticker(const Sticker&); + Sticker(Sticker&&); + ~Sticker(); - const std::string& getEmoji() const; - void setEmoji(const std::string& emoji); + const std::string& getFileId() const; + void setFileId(const std::string& fileId); - int getFileSize() const; - void setFileSize(int fileSize); + int getWidth() const; + void setWidth(int width); - void swap(Sticker& other) noexcept; + int getHeight() const; + void setHeight(int height); - const Sticker& operator=(Sticker other) noexcept; + const PhotoSizePtr getThumb() const; + void setThumb(const PhotoSizePtr& thumb); - private: - std::string fileId_; - int width_; - int height_; - PhotoSizePtr thumb_; - std::string emoji_; - int fileSize_; - }; + const std::string& getEmoji() const; + void setEmoji(const std::string& emoji); + + int getFileSize() const; + void setFileSize(int fileSize); + + void swap(Sticker& other) noexcept; + + const Sticker& operator=(Sticker other) noexcept; + +private: + std::string fileId_; + int width_; + int height_; + PhotoSizePtr thumb_; + std::string emoji_; + int fileSize_; +}; + +void swap(PhotoSize& lhs, PhotoSize& rhs); +void swap(PhotoSizeArray& lhs, PhotoSizeArray& rhs); +void swap(Audio& lhs, Audio& rhs); +void swap(Document& lhs, Document& rhs); +void swap(Sticker& lhs, Sticker& rhs); - void swap(PhotoSize& lhs, PhotoSize& rhs); - void swap(PhotoSizeArray& lhs, PhotoSizeArray& rhs); - void swap(Audio& lhs, Audio& rhs); - void swap(Document& lhs, Document& rhs); - void swap(Sticker& lhs, Sticker& rhs); } #endif // TELEBOTXX_ATTACHMENT_HPP diff --git a/include/telebotxx/BotApi.hpp b/include/telebotxx/BotApi.hpp index 01431e1..c21a939 100644 --- a/include/telebotxx/BotApi.hpp +++ b/include/telebotxx/BotApi.hpp @@ -1,5 +1,5 @@ -#ifndef TELEBOTXX_BOTAPI_H -#define TELEBOTXX_BOTAPI_H +#ifndef TELEBOTXX_BOTAPI_HPP +#define TELEBOTXX_BOTAPI_HPP #include "User.hpp" #include "Message.hpp" @@ -10,85 +10,86 @@ #include #include -namespace telebotxx +namespace telebotxx { + +template +void setRequestOption(RequestType& request, T t) { - template - void setRequestOption(RequestType& request, T t) - { - request.setOption(t); - } - - template - void setRequestOption(RequestType& request, T&& t, Ts&&... ts) - { - setRequestOption(request, std::forward(t)); - setRequestOption(request, std::forward(ts)...); - }; - - class BotApi - { - public: - - /// \param [in] token bot's secret token - BotApi(const std::string& token); - - ~BotApi(); - - /// \brief Get basic information about the bot - /// \return User object with information about the bot - User getMe(); - - /// \brief Send text message - /// \param chatId chat identifier - /// \param text text - /// \return Message object, recieved from the server - Message sendMessage(ChatId&& chatId, Text&& text); - - /// \brief Send text message - /// \param chatId chat identifier - /// \param text text - /// \param args parameters - /// \return Message object, recieved from the server - template - Message sendMessage(ChatId&& chatId, Text&& text, Ts&&... args) - { - SendMessageRequest request(getTelegramMainUrl(), std::forward(chatId), std::forward(text)); - setRequestOption(request, std::forward(args)...); - return request.execute(); - } - - /// \brief Send image - /// \param [in] chatId chat identifier - /// \param [in] photo photo - /// \return Message object, recieved from the server - Message sendPhoto(ChatId&& chatId, Photo&& photo); - - /// \brief Send image - /// \param [in] chatId chat identifier - /// \param [in] photo photo - /// \param [in] args parameters - /// \return Message object, recieved from the server - template - Message sendPhoto(ChatId&& chatId, Photo&& photo, Ts&&... args) - { - SendPhotoRequest request(getTelegramMainUrl(), std::forward(chatId), std::forward(photo)); - setRequestOption(request, std::forward(args)...); - return request.execute(); - } - - /// \brief Get updates using long polling - /// \param offset identifier of the first update to be returned - /// \param limit maximum number of updates to be retrieved - /// \param timeout timeout in seconds for long polling - /// \return Updates (vector of Update) - Updates getUpdates(int offset = 0, unsigned short limit = 0, unsigned timeout = 0); - - private: - std::string getTelegramMainUrl() const; - - class Impl; - std::unique_ptr impl_; - }; + request.setOption(t); } -#endif // TELEBOTXX_BOTAPI_H +template +void setRequestOption(RequestType& request, T&& t, Ts&&... ts) +{ + setRequestOption(request, std::forward(t)); + setRequestOption(request, std::forward(ts)...); +}; + +class BotApi +{ +public: + + /// \param [in] token bot's secret token + BotApi(const std::string& token); + + ~BotApi(); + + /// \brief Get basic information about the bot + /// \return User object with information about the bot + User getMe(); + + /// \brief Send text message + /// \param chatId chat identifier + /// \param text text + /// \return Message object, recieved from the server + Message sendMessage(ChatId&& chatId, Text&& text); + + /// \brief Send text message + /// \param chatId chat identifier + /// \param text text + /// \param args parameters + /// \return Message object, recieved from the server + template + Message sendMessage(ChatId&& chatId, Text&& text, Ts&&... args) + { + SendMessageRequest request(getTelegramMainUrl(), std::forward(chatId), std::forward(text)); + setRequestOption(request, std::forward(args)...); + return request.execute(); + } + + /// \brief Send image + /// \param [in] chatId chat identifier + /// \param [in] photo photo + /// \return Message object, recieved from the server + Message sendPhoto(ChatId&& chatId, Photo&& photo); + + /// \brief Send image + /// \param [in] chatId chat identifier + /// \param [in] photo photo + /// \param [in] args parameters + /// \return Message object, recieved from the server + template + Message sendPhoto(ChatId&& chatId, Photo&& photo, Ts&&... args) + { + SendPhotoRequest request(getTelegramMainUrl(), std::forward(chatId), std::forward(photo)); + setRequestOption(request, std::forward(args)...); + return request.execute(); + } + + /// \brief Get updates using long polling + /// \param offset identifier of the first update to be returned + /// \param limit maximum number of updates to be retrieved + /// \param timeout timeout in seconds for long polling + /// \return Updates (vector of Update) + Updates getUpdates(int offset = 0, unsigned short limit = 0, unsigned timeout = 0); + +private: + std::string getTelegramMainUrl() const; + + class Impl; + std::unique_ptr impl_; +}; + +} + +#endif // TELEBOTXX_BOTAPI_HPP diff --git a/include/telebotxx/Chat.hpp b/include/telebotxx/Chat.hpp index 91f14ae..4939b7e 100644 --- a/include/telebotxx/Chat.hpp +++ b/include/telebotxx/Chat.hpp @@ -5,64 +5,65 @@ #include #include -namespace telebotxx +namespace telebotxx { + +class Chat { - class Chat +public: + enum class Type { - public: - enum class Type - { - Private, - Group, - Supergroup, - Channel - }; - - Chat(); - Chat(const Chat&); - Chat(Chat&&); - ~Chat(); - - std::int64_t getId() const; - void setId(std::int64_t id); - - Type getType() const; - void setType(Type type); - - const std::string& getTitle() const; - void setTitle(const std::string& title); - - const std::string& getUsername() const; - void setUsername(const std::string& username); - - const std::string& getFirstName() const; - void setFirstName(const std::string& firstName); - - const std::string& getLastName() const; - void setLastName(const std::string& lastName); - - bool isAllAdmins() const; - void setAllAdmins(bool allAdmins); - - void swap(Chat& other) noexcept; - - const Chat& operator=(Chat other) noexcept; - - private: - std::int64_t id_; - Type type_; - std::string title_; - std::string username_; - std::string firstName_; - std::string lastName_; - bool allAdmins_; + Private, + Group, + Supergroup, + Channel }; - using ChatPtr = std::shared_ptr; + Chat(); + Chat(const Chat&); + Chat(Chat&&); + ~Chat(); - void swap(Chat& lhs, Chat& rhs); + std::int64_t getId() const; + void setId(std::int64_t id); + + Type getType() const; + void setType(Type type); + + const std::string& getTitle() const; + void setTitle(const std::string& title); + + const std::string& getUsername() const; + void setUsername(const std::string& username); + + const std::string& getFirstName() const; + void setFirstName(const std::string& firstName); + + const std::string& getLastName() const; + void setLastName(const std::string& lastName); + + bool isAllAdmins() const; + void setAllAdmins(bool allAdmins); + + void swap(Chat& other) noexcept; + + const Chat& operator=(Chat other) noexcept; + +private: + std::int64_t id_; + Type type_; + std::string title_; + std::string username_; + std::string firstName_; + std::string lastName_; + bool allAdmins_; +}; + +using ChatPtr = std::shared_ptr; + +void swap(Chat& lhs, Chat& rhs); + +Chat::Type chatTypeFromString(const std::string& str); - Chat::Type chatTypeFromString(const std::string& str); } #endif // TELEBOTXX_CHAT_HPP diff --git a/include/telebotxx/Exception.hpp b/include/telebotxx/Exception.hpp index 2f72ff8..091491f 100644 --- a/include/telebotxx/Exception.hpp +++ b/include/telebotxx/Exception.hpp @@ -3,33 +3,33 @@ #include -namespace telebotxx +namespace telebotxx { + +class ParseError : public std::invalid_argument { - class ParseError : public std::invalid_argument +public: + ParseError(const std::string& message) + : std::invalid_argument(message) { - public: - ParseError(const std::string& message) - : std::invalid_argument(message) - { - } - }; + } +}; - class ApiError : public std::runtime_error +class ApiError : public std::runtime_error +{ +public: + ApiError(int code, const std::string& message) + : std::runtime_error(message), code_(code) { - public: - ApiError(int code, const std::string& message) - : std::runtime_error(message), code_(code) - { - } + } - int getCode() const - { - return code_; - } + int getCode() const + { + return code_; + } - protected: - int code_; - }; +protected: + int code_; +}; } diff --git a/include/telebotxx/Logging.hpp b/include/telebotxx/Logging.hpp index 2866077..033e0aa 100644 --- a/include/telebotxx/Logging.hpp +++ b/include/telebotxx/Logging.hpp @@ -1,12 +1,13 @@ #ifndef TELEBOTXX_LOGGING_HPP #define TELEBOTXX_LOGGING_HPP -namespace telebotxx -{ - extern bool debugMode; +namespace telebotxx { + +extern bool debugMode; + +/// \brief Enable/disable debug output +void setDebugMode(bool enabled); - /// \brief Enable/disable debug output - void setDebugMode(bool enabled); } #endif // TELEBOTXX_LOGGING_HPP diff --git a/include/telebotxx/Message.hpp b/include/telebotxx/Message.hpp index a20ccb6..fdd235a 100644 --- a/include/telebotxx/Message.hpp +++ b/include/telebotxx/Message.hpp @@ -9,177 +9,178 @@ #include #include -namespace telebotxx +namespace telebotxx { + +class MessageEntity { - class MessageEntity +public: + enum class Type { - public: - enum class Type - { - Mention, - Hashtag, - BotCommand, - Url, - Email, - Bold, - Italic, - Code, - Pre, - TextLink, - TextMention - }; - - MessageEntity(); - MessageEntity(const MessageEntity&); - MessageEntity(MessageEntity&&); - ~MessageEntity(); - - Type getType() const; - void setType(Type type); - - int getOffset() const; - void setOffset(int offset); - - size_t getLength() const; - void setLength(size_t length); - - const std::string& getUrl() const; - void setUrl(const std::string& url); - - const User& getUser() const; - void setUser(const User& user); - - void swap(MessageEntity& other) noexcept; - - const MessageEntity& operator=(MessageEntity other); - - private: - Type type_; - int offset_; - std::size_t length_; - std::string url_; - User user_; + Mention, + Hashtag, + BotCommand, + Url, + Email, + Bold, + Italic, + Code, + Pre, + TextLink, + TextMention }; - MessageEntity::Type messageEntityTypeFromString(const std::string& str); + MessageEntity(); + MessageEntity(const MessageEntity&); + MessageEntity(MessageEntity&&); + ~MessageEntity(); - using MessageEntities = std::vector; + Type getType() const; + void setType(Type type); - class Message; - using MessagePtr = std::shared_ptr; + int getOffset() const; + void setOffset(int offset); - class Message - { - public: - Message(); - Message(const Message&); - Message(Message&&); - ~Message(); + size_t getLength() const; + void setLength(size_t length); - int getId() const; - void setId(int id); + const std::string& getUrl() const; + void setUrl(const std::string& url); - const UserPtr getFrom() const; - void setFrom(UserPtr from); + const User& getUser() const; + void setUser(const User& user); - time_t getDate() const; - void setDate(time_t date); + void swap(MessageEntity& other) noexcept; - const Chat& getChat() const; - void setChat(const Chat& chat); + const MessageEntity& operator=(MessageEntity other); - const UserPtr getForwardFrom() const; - void setForwardFrom(UserPtr forwardFrom); +private: + Type type_; + int offset_; + std::size_t length_; + std::string url_; + User user_; +}; - const ChatPtr getForwardFromChat() const; - void setForwardFromChat(ChatPtr forwardFromChat); +MessageEntity::Type messageEntityTypeFromString(const std::string& str); - time_t getForwardDate() const; - void setForwardDate(time_t forwardDate); +using MessageEntities = std::vector; - const MessagePtr getReplyToMessage() const; - void setReplyToMessage(MessagePtr replyToMessage); +class Message; +using MessagePtr = std::shared_ptr; - time_t getEditDate() const; - void setEditDate(time_t editDate); +class Message +{ +public: + Message(); + Message(const Message&); + Message(Message&&); + ~Message(); - const std::string& getText() const; - void setText(const std::string& text); + int getId() const; + void setId(int id); - const MessageEntities& getEntities() const; - void setEntities(MessageEntities&& entities); + const UserPtr getFrom() const; + void setFrom(UserPtr from); - const AttachmentPtr getAttachment() const; - void setAttachment(AttachmentPtr attachment); + time_t getDate() const; + void setDate(time_t date); - const std::string& getCaption() const; - void setCaption(const std::string& caption); + const Chat& getChat() const; + void setChat(const Chat& chat); - const UserPtr getNewChatMember() const; - void setNewChatMember(UserPtr newChatMember); + const UserPtr getForwardFrom() const; + void setForwardFrom(UserPtr forwardFrom); - const UserPtr getLeftChatMember() const; - void setLeftChatMember(UserPtr leftChatMember); + const ChatPtr getForwardFromChat() const; + void setForwardFromChat(ChatPtr forwardFromChat); - const std::string& getNewChatTitle() const; - void setNewChatTitle(const std::string& newChatTitle); + time_t getForwardDate() const; + void setForwardDate(time_t forwardDate); - const PhotoSizeArrayPtr getNewChatPhoto() const; - void setNewChatPhoto(PhotoSizeArrayPtr newChatPhoto); + const MessagePtr getReplyToMessage() const; + void setReplyToMessage(MessagePtr replyToMessage); - bool isDeleteChatPhoto() const; - void setDeleteChatPhoto(bool deleteChatPhoto); + time_t getEditDate() const; + void setEditDate(time_t editDate); - bool isGroupChatCreated() const; - void setGroupChatCreated(bool groupChatCreated); + const std::string& getText() const; + void setText(const std::string& text); - bool isSuperGroupChatCreated() const; - void setSuperGroupChatCreated(bool superGroupChatCreated); + const MessageEntities& getEntities() const; + void setEntities(MessageEntities&& entities); - bool isChannelChatCreated() const; - void setChannelChatCreated(bool channelChatCreated); + const AttachmentPtr getAttachment() const; + void setAttachment(AttachmentPtr attachment); - std::int64_t getMigrateToChatId() const; - void setMigrateToChatId(std::int64_t migrateToChatId); + const std::string& getCaption() const; + void setCaption(const std::string& caption); - std::int64_t getMigrateFromChatId() const; - void setMigrateFromChatId(std::int64_t migrateFromChatId); + const UserPtr getNewChatMember() const; + void setNewChatMember(UserPtr newChatMember); - const MessagePtr getPinnedMessage() const; - void setPinnedMessage(MessagePtr pinnedMessage); + const UserPtr getLeftChatMember() const; + void setLeftChatMember(UserPtr leftChatMember); - void swap(Message& other) noexcept; + const std::string& getNewChatTitle() const; + void setNewChatTitle(const std::string& newChatTitle); - const Message& operator=(Message other) noexcept; + const PhotoSizeArrayPtr getNewChatPhoto() const; + void setNewChatPhoto(PhotoSizeArrayPtr newChatPhoto); - private: - int id_; - UserPtr from_; - std::time_t date_; - Chat chat_; - UserPtr forwardFrom_; - ChatPtr forwardFromChat_; - std::time_t forwardDate_; - MessagePtr replyToMessage_; - std::time_t editDate_; - std::string text_; - MessageEntities entities_; - AttachmentPtr attachment_; - std::string caption_; - UserPtr newChatMember_; - UserPtr leftChatMember_; - std::string newChatTitle_; - PhotoSizeArrayPtr newChatPhoto_; - bool deleteChatPhoto_; - bool groupChatCreated_; - bool superGroupChatCreated_; - bool channelChatCreated_; - std::int64_t migrateToChatId_; - std::int64_t migrateFromChatId_; - MessagePtr pinnedMessage_; - }; + bool isDeleteChatPhoto() const; + void setDeleteChatPhoto(bool deleteChatPhoto); + + bool isGroupChatCreated() const; + void setGroupChatCreated(bool groupChatCreated); + + bool isSuperGroupChatCreated() const; + void setSuperGroupChatCreated(bool superGroupChatCreated); + + bool isChannelChatCreated() const; + void setChannelChatCreated(bool channelChatCreated); + + std::int64_t getMigrateToChatId() const; + void setMigrateToChatId(std::int64_t migrateToChatId); + + std::int64_t getMigrateFromChatId() const; + void setMigrateFromChatId(std::int64_t migrateFromChatId); + + const MessagePtr getPinnedMessage() const; + void setPinnedMessage(MessagePtr pinnedMessage); + + void swap(Message& other) noexcept; + + const Message& operator=(Message other) noexcept; + +private: + int id_; + UserPtr from_; + std::time_t date_; + Chat chat_; + UserPtr forwardFrom_; + ChatPtr forwardFromChat_; + std::time_t forwardDate_; + MessagePtr replyToMessage_; + std::time_t editDate_; + std::string text_; + MessageEntities entities_; + AttachmentPtr attachment_; + std::string caption_; + UserPtr newChatMember_; + UserPtr leftChatMember_; + std::string newChatTitle_; + PhotoSizeArrayPtr newChatPhoto_; + bool deleteChatPhoto_; + bool groupChatCreated_; + bool superGroupChatCreated_; + bool channelChatCreated_; + std::int64_t migrateToChatId_; + std::int64_t migrateFromChatId_; + MessagePtr pinnedMessage_; +}; + +void swap(Message& lhs, Message& rhs); - void swap(Message& lhs, Message& rhs); } #endif // TELEBOTXX_MESSAGE_HPP diff --git a/include/telebotxx/RequestOptions.hpp b/include/telebotxx/RequestOptions.hpp index ec5afd2..0e5e617 100644 --- a/include/telebotxx/RequestOptions.hpp +++ b/include/telebotxx/RequestOptions.hpp @@ -4,128 +4,129 @@ #include #include -namespace telebotxx +namespace telebotxx { + +class ChatId { - class ChatId +public: + ChatId(int); + ChatId(const std::string&); + ChatId(const ChatId&); + ChatId(ChatId&&); + ~ChatId(); + + enum class Type { Id, Username }; + Type getType() const; + const int getId() const; + const std::string getUsername() const; +private: + Type type_; + union { - public: - ChatId(int); - ChatId(const std::string&); - ChatId(const ChatId&); - ChatId(ChatId&&); - ~ChatId(); - - enum class Type { Id, Username }; - Type getType() const; - const int getId() const; - const std::string getUsername() const; - private: - Type type_; - union - { - int id_; - std::string username_; - }; - }; - - using Text = std::string; - using Caption = std::string; - - enum class ParseMode - { - Plain, - Markdown, - Html - }; - - class DisableWebPagePreview - { - public: - DisableWebPagePreview(bool disabled = true); - bool value() const; - private: - bool disable_; - }; - - class DisableNotification - { - public: - DisableNotification(bool disabled = true); - bool value() const; - private: - bool disable_; - }; - - class ReplyTo - { - public: - explicit ReplyTo(int id); - int value() const; - private: int id_; + std::string username_; }; +}; - class Buffer +using Text = std::string; +using Caption = std::string; + +enum class ParseMode +{ + Plain, + Markdown, + Html +}; + +class DisableWebPagePreview +{ +public: + DisableWebPagePreview(bool disabled = true); + bool value() const; +private: + bool disable_; +}; + +class DisableNotification +{ +public: + DisableNotification(bool disabled = true); + bool value() const; +private: + bool disable_; +}; + +class ReplyTo +{ +public: + explicit ReplyTo(int id); + int value() const; +private: + int id_; +}; + +class Buffer +{ +public: + Buffer(const char *buffer, std::size_t size, const std::string& filename); + explicit Buffer(const std::vector& data, const std::string& filename); + const char *data() const; + const std::size_t size() const; + const std::string filename() const; +private: + const char *data_; + std::size_t size_; + std::string filename_; +}; + +class File +{ +public: + explicit File(const std::string& filename); + const std::string& getFilename() const; +private: + std::string filename_; +}; + +class Url +{ +public: + explicit Url(const std::string& url); + const std::string& getUrl() const; +private: + std::string url_; +}; + +class Photo +{ +public: + explicit Photo(int id); + explicit Photo(const Buffer&); + explicit Photo(const File&); + explicit Photo(const Url&); + Photo(const Photo&); + Photo(Photo&&); + ~Photo(); + + enum class Type { Id, Buffer, File, Url }; + Type getType() const; + + int getId() const; + const Buffer& getBuffer() const; + const File& getFile() const; + const Url& getUrl() const; + +private: + Type type_; + union { - public: - Buffer(const char* buffer, std::size_t size, const std::string& filename); - explicit Buffer(const std::vector& data, const std::string& filename); - const char* data() const; - const std::size_t size() const; - const std::string filename() const; - private: - const char* data_; - std::size_t size_; - std::string filename_; + int id_; + Buffer buffer_; + File file_; + Url url_; }; +}; - class File - { - public: - explicit File(const std::string& filename); - const std::string& getFilename() const; - private: - std::string filename_; - }; - - class Url - { - public: - explicit Url(const std::string& url); - const std::string& getUrl() const; - private: - std::string url_; - }; - - class Photo - { - public: - explicit Photo(int id); - explicit Photo(const Buffer&); - explicit Photo(const File&); - explicit Photo(const Url&); - Photo(const Photo&); - Photo(Photo&&); - ~Photo(); - - enum class Type { Id, Buffer, File, Url}; - Type getType() const; - - int getId() const; - const Buffer& getBuffer() const; - const File& getFile() const; - const Url& getUrl() const; - - private: - Type type_; - union - { - int id_; - Buffer buffer_; - File file_; - Url url_; - }; - }; } #endif // TELEBOTXX_REQUEST_OPTIONS_HPP diff --git a/include/telebotxx/SendMessageRequest.hpp b/include/telebotxx/SendMessageRequest.hpp index 7ffb861..d9334e0 100644 --- a/include/telebotxx/SendMessageRequest.hpp +++ b/include/telebotxx/SendMessageRequest.hpp @@ -4,35 +4,34 @@ #include #include -#include - #include #include -namespace telebotxx +namespace telebotxx { + +class SendMessageRequest { - class SendMessageRequest - { - public: - SendMessageRequest(const std::string& telegramMainUrl, const ChatId& chat, const Text& text); - ~SendMessageRequest(); +public: + SendMessageRequest(const std::string& telegramMainUrl, const ChatId& chat, const Text& text); + ~SendMessageRequest(); - void setParseMode(ParseMode mode); - void setDisableWebPagePreview(const DisableWebPagePreview& disableWebPagePreview); - void setDisableNotification(const DisableNotification& disableNotification); - void setReplyToMessageId(const ReplyTo& replyToMessageId); + void setParseMode(ParseMode mode); + void setDisableWebPagePreview(const DisableWebPagePreview& disableWebPagePreview); + void setDisableNotification(const DisableNotification& disableNotification); + void setReplyToMessageId(const ReplyTo& replyToMessageId); - void setOption(ParseMode mode); - void setOption(const DisableWebPagePreview& disableWebPagePreview); - void setOption(const DisableNotification& disableNotification); - void setOption(const ReplyTo& replyToMessageId); + void setOption(ParseMode mode); + void setOption(const DisableWebPagePreview& disableWebPagePreview); + void setOption(const DisableNotification& disableNotification); + void setOption(const ReplyTo& replyToMessageId); - Message execute(); + Message execute(); + +private: + class Impl; + std::unique_ptr impl_; +}; - private: - class Impl; - std::unique_ptr impl_; - }; } #endif // TELEBOTXX_SEND_MESSAGE_REQUEST_HPP diff --git a/include/telebotxx/SendPhotoRequest.hpp b/include/telebotxx/SendPhotoRequest.hpp index bfff348..c07e3e9 100644 --- a/include/telebotxx/SendPhotoRequest.hpp +++ b/include/telebotxx/SendPhotoRequest.hpp @@ -4,32 +4,32 @@ #include #include -#include - #include #include -namespace telebotxx +namespace telebotxx { + +class SendPhotoRequest { - class SendPhotoRequest - { - public: - SendPhotoRequest(const std::string& telegramMainUrl, const ChatId& chat, const Photo& photo); - ~SendPhotoRequest(); +public: + SendPhotoRequest(const std::string& telegramMainUrl, const ChatId& chat, const Photo& photo); + ~SendPhotoRequest(); - void setCaption(const Caption& caption); - void setDisableNotification(const DisableNotification& disableNotification); - void setReplyToMessageId(const ReplyTo& replyToMessageId); - void setOption(const Caption& caption); - void setOption(const DisableNotification& disableNotification); - void setOption(const ReplyTo& replyToMessageId); + void setCaption(const Caption& caption); + void setDisableNotification(const DisableNotification& disableNotification); + void setReplyToMessageId(const ReplyTo& replyToMessageId); - Message execute(); + void setOption(const Caption& caption); + void setOption(const DisableNotification& disableNotification); + void setOption(const ReplyTo& replyToMessageId); + + Message execute(); + +private: + class Impl; + std::unique_ptr impl_; +}; - private: - class Impl; - std::unique_ptr impl_; - }; } #endif // TELEBOTXX_SEND_PHOTO_REQUEST_HPP diff --git a/include/telebotxx/Update.hpp b/include/telebotxx/Update.hpp index f1b7d9d..d671da2 100644 --- a/include/telebotxx/Update.hpp +++ b/include/telebotxx/Update.hpp @@ -6,68 +6,68 @@ #include #include -namespace telebotxx +namespace telebotxx { + +class Update { - class Update +public: + enum class Type { - public: - enum class Type - { - Message, - EditedMessage, - InlineQuery, - ChosenInlineResult, - CallbackQuery - }; - - Update(int id, Type type); - Update(const Update&); - Update(Update&&); - virtual ~Update() = 0; - - int getId() const; - Type getType() const; - - void swap(Update& other) noexcept; - - private: - int id_; - Type updateType_; + Message, + EditedMessage, + InlineQuery, + ChosenInlineResult, + CallbackQuery }; - using UpdatePtr = std::shared_ptr; - using Updates = std::vector; + Update(int id, Type type); + Update(const Update&); + Update(Update&&); + virtual ~Update() = 0; - class MessageUpdate : public Update - { - public: - MessageUpdate(int id, const Message& message); - MessageUpdate(const MessageUpdate&); - MessageUpdate(MessageUpdate&&); - ~MessageUpdate(); + int getId() const; + Type getType() const; - const Message& getMessage() const; + void swap(Update& other) noexcept; - void swap(MessageUpdate& other) noexcept; +private: + int id_; + Type updateType_; +}; - const MessageUpdate& operator=(MessageUpdate other); +using UpdatePtr = std::shared_ptr; +using Updates = std::vector; - private: - Message message_; - }; +class MessageUpdate : public Update +{ +public: + MessageUpdate(int id, const Message& message); + MessageUpdate(const MessageUpdate&); + MessageUpdate(MessageUpdate&&); + ~MessageUpdate(); - class EditedMessageUpdate: public MessageUpdate - { - public: - EditedMessageUpdate(int id, const Message& message); - EditedMessageUpdate(const EditedMessageUpdate&); - EditedMessageUpdate(EditedMessageUpdate&&); - ~EditedMessageUpdate(); + const Message& getMessage() const; - void swap(EditedMessageUpdate& other) noexcept; + void swap(MessageUpdate& other) noexcept; - const EditedMessageUpdate& operator=(EditedMessageUpdate other); - }; + const MessageUpdate& operator=(MessageUpdate other); + +private: + Message message_; +}; + +class EditedMessageUpdate : public MessageUpdate +{ +public: + EditedMessageUpdate(int id, const Message& message); + EditedMessageUpdate(const EditedMessageUpdate&); + EditedMessageUpdate(EditedMessageUpdate&&); + ~EditedMessageUpdate(); + + void swap(EditedMessageUpdate& other) noexcept; + + const EditedMessageUpdate& operator=(EditedMessageUpdate other); +}; } diff --git a/include/telebotxx/User.hpp b/include/telebotxx/User.hpp index 375f3b9..a36ad83 100644 --- a/include/telebotxx/User.hpp +++ b/include/telebotxx/User.hpp @@ -4,57 +4,58 @@ #include #include -namespace telebotxx +namespace telebotxx { + +class User { - class User - { - public: - User(); - User(const User&); - User(User&&); - ~User(); +public: + User(); + User(const User&); + User(User&&); + ~User(); - /// \brief Get id - int getId() const; + /// \brief Get id + int getId() const; - /// \brief Set id - void setId(int id); + /// \brief Set id + void setId(int id); - /// \brief Get first name - const std::string& getFirstName() const; + /// \brief Get first name + const std::string& getFirstName() const; - /// \brief Set first name - void setFirstName(const std::string& firstName); + /// \brief Set first name + void setFirstName(const std::string& firstName); - /// \brief Get last name - const std::string& getLastName() const; + /// \brief Get last name + const std::string& getLastName() const; - /// \brief Set last name - void setLastName(const std::string& lastName); + /// \brief Set last name + void setLastName(const std::string& lastName); - /// \brief Get username - const std::string& getUsername() const; + /// \brief Get username + const std::string& getUsername() const; - /// \brief Set username - void setUsername(const std::string& username); + /// \brief Set username + void setUsername(const std::string& username); - /// \brief Get string representation of user - const std::string toString() const; + /// \brief Get string representation of user + const std::string toString() const; - void swap(User&) noexcept; + void swap(User&) noexcept; - const User& operator=(User other); + const User& operator=(User other); - private: - int id_; - std::string firstName_; - std::string lastName_; - std::string username_; - }; +private: + int id_; + std::string firstName_; + std::string lastName_; + std::string username_; +}; - using UserPtr = std::shared_ptr; +using UserPtr = std::shared_ptr; + +std::ostream& operator<<(std::ostream& os, const User& user); - std::ostream& operator<<(std::ostream& os, const User& user); } #endif // TELEBOTXX_USER_H diff --git a/src/Attachment.cpp b/src/Attachment.cpp index be42972..66b391b 100644 --- a/src/Attachment.cpp +++ b/src/Attachment.cpp @@ -1,14 +1,16 @@ #include -using namespace telebotxx; +namespace telebotxx { Attachment::Attachment(Type type) - : attachmentType_(type) + : attachmentType_(type) { } Attachment::Attachment(const Attachment&) = default; + Attachment::Attachment(Attachment&&) = default; + Attachment::~Attachment() = default; Attachment::Type Attachment::getType() const @@ -24,14 +26,16 @@ void Attachment::swap(Attachment& other) noexcept //////////////////////////////////////////////////////////////// PhotoSize::PhotoSize() - : width_(-1), - height_(-1), - fileSize_(-1) + : width_(-1), + height_(-1), + fileSize_(-1) { } PhotoSize::PhotoSize(const PhotoSize&) = default; + PhotoSize::PhotoSize(PhotoSize&&) = default; + PhotoSize::~PhotoSize() = default; const std::string& PhotoSize::getFileId() const @@ -97,7 +101,9 @@ PhotoSizeArray::PhotoSizeArray() } PhotoSizeArray::PhotoSizeArray(const PhotoSizeArray&) = default; + PhotoSizeArray::PhotoSizeArray(PhotoSizeArray&&) = default; + PhotoSizeArray::~PhotoSizeArray() = default; const std::vector& PhotoSizeArray::getArray() const @@ -125,14 +131,16 @@ const PhotoSizeArray& PhotoSizeArray::operator=(PhotoSizeArray other) noexcept //////////////////////////////////////////////////////////////// Audio::Audio() - : Attachment(Type::Audio), - duration_(-1), - fileSize_(-1) + : Attachment(Type::Audio), + duration_(-1), + fileSize_(-1) { } Audio::Audio(const Audio&) = default; + Audio::Audio(Audio&&) = default; + Audio::~Audio() = default; const std::string& Audio::getFileId() const @@ -216,13 +224,15 @@ const Audio& Audio::operator=(Audio other) noexcept //////////////////////////////////////////////////////////////// Document::Document() - : Attachment(Type::Document), - fileSize_(-1) + : Attachment(Type::Document), + fileSize_(-1) { } Document::Document(const Document&) = default; + Document::Document(Document&&) = default; + Document::~Document() = default; const std::string& Document::getFileId() const @@ -295,13 +305,15 @@ const Document& Document::operator=(Document other) noexcept //////////////////////////////////////////////////////////////// Sticker::Sticker() - : Attachment(Type::Sticker), - fileSize_(-1) + : Attachment(Type::Sticker), + fileSize_(-1) { } Sticker::Sticker(const Sticker&) = default; + Sticker::Sticker(Sticker&&) = default; + Sticker::~Sticker() = default; const std::string& Sticker::getFileId() const @@ -384,27 +396,29 @@ const Sticker& Sticker::operator=(Sticker other) noexcept //////////////////////////////////////////////////////////////// -void telebotxx::swap(PhotoSize& lhs, PhotoSize& rhs) +void swap(PhotoSize& lhs, PhotoSize& rhs) { lhs.swap(rhs); } -void telebotxx::swap(PhotoSizeArray& lhs, PhotoSizeArray& rhs) +void swap(PhotoSizeArray& lhs, PhotoSizeArray& rhs) { lhs.swap(rhs); } -void telebotxx::swap(Audio& lhs, Audio& rhs) +void swap(Audio& lhs, Audio& rhs) { lhs.swap(rhs); } -void telebotxx::swap(Document& lhs, Document& rhs) +void swap(Document& lhs, Document& rhs) { lhs.swap(rhs); } -void telebotxx::swap(Sticker& lhs, Sticker& rhs) +void swap(Sticker& lhs, Sticker& rhs) { lhs.swap(rhs); } + +} diff --git a/src/BotApi.cpp b/src/BotApi.cpp index 050e146..03823d7 100644 --- a/src/BotApi.cpp +++ b/src/BotApi.cpp @@ -10,7 +10,7 @@ #include -using namespace telebotxx; +namespace telebotxx { class BotApi::Impl { @@ -41,7 +41,7 @@ public: { auto r = cpr::Post(cpr::Url{telegramMainUrl_ + "/sendPhoto"}, cpr::Multipart{{"chat_id", chat}, - {"photo", cpr::File{filename}}, + {"photo", cpr::File{filename}}, {"caption", caption} } ); @@ -181,3 +181,5 @@ std::string BotApi::getTelegramMainUrl() const { return impl_->getTelegramMainUrl(); } + +} diff --git a/src/Chat.cpp b/src/Chat.cpp index ebfdcb7..f2753c6 100644 --- a/src/Chat.cpp +++ b/src/Chat.cpp @@ -1,7 +1,7 @@ #include #include -using namespace telebotxx; +namespace telebotxx { Chat::Chat() : id_(-1), @@ -11,7 +11,9 @@ Chat::Chat() } Chat::Chat(const Chat&) = default; + Chat::Chat(Chat&&) = default; + Chat::~Chat() = default; std::int64_t Chat::getId() const @@ -102,12 +104,12 @@ const Chat& Chat::operator=(Chat other) noexcept return *this; } -void telebotxx::swap(Chat& lhs, Chat& rhs) +void swap(Chat& lhs, Chat& rhs) { lhs.swap(rhs); } -Chat::Type telebotxx::chatTypeFromString(const std::string& str) +Chat::Type chatTypeFromString(const std::string& str) { if (str == "private") return Chat::Type::Private; @@ -120,3 +122,5 @@ Chat::Type telebotxx::chatTypeFromString(const std::string& str) else throw std::invalid_argument("Unknown chat type"); } + +} diff --git a/src/JsonObjects.cpp b/src/JsonObjects.cpp index 992b9bf..ff6c99f 100644 --- a/src/JsonObjects.cpp +++ b/src/JsonObjects.cpp @@ -3,313 +3,314 @@ #include -namespace telebotxx +namespace telebotxx { +namespace impl { + +template bool is(const rapidjson::Value& obj); +template<> bool is(const rapidjson::Value& obj) { return obj.IsInt(); } +template<> bool is(const rapidjson::Value& obj) { return obj.IsInt64(); } +template<> bool is(const rapidjson::Value& obj) { return obj.IsBool(); } +template<> bool is(const rapidjson::Value& obj) { return obj.IsString(); } + +template const T get(const rapidjson::Value& obj); +template<> const int get(const rapidjson::Value& obj) { return obj.GetInt(); } +template<> const std::int64_t get(const rapidjson::Value& obj) { return obj.GetInt64(); } +template<> const bool get(const rapidjson::Value& obj) { return obj.GetBool(); } +template<> const std::string get(const rapidjson::Value& obj) { return obj.GetString(); } + +template const T null(); +template<> const int null() { return 0; } +template<> const std::int64_t null() { return 0; } +template<> const bool null() { return false; } +template<> const std::string null() { return ""; } + +} + +template +const T parse(const rapidjson::Value& obj, const char* name, bool required) { - namespace impl + if (obj.HasMember(name)) { - template bool is(const rapidjson::Value& obj); - template<> bool is(const rapidjson::Value& obj) { return obj.IsInt(); } - template<> bool is(const rapidjson::Value& obj) { return obj.IsInt64(); } - template<> bool is(const rapidjson::Value& obj) { return obj.IsBool(); } - template<> bool is(const rapidjson::Value& obj) { return obj.IsString(); } - - template const T get(const rapidjson::Value& obj); - template<> const int get(const rapidjson::Value& obj) { return obj.GetInt(); } - template<> const std::int64_t get(const rapidjson::Value& obj) { return obj.GetInt64(); } - template<> const bool get(const rapidjson::Value& obj) { return obj.GetBool(); } - template<> const std::string get(const rapidjson::Value& obj) { return obj.GetString(); } - - template const T null(); - template<> const int null() { return 0; } - template<> const std::int64_t null() { return 0; } - template<> const bool null() { return false; } - template<> const std::string null() { return ""; } - } - - template - const T parse(const rapidjson::Value& obj, const char* name, bool required) - { - if (obj.HasMember(name)) - { - if (impl::is(obj[name])) - return impl::get(obj[name]); - else - throw ParseError(std::string("Field '")+ name + "' has invalid type"); - } - else if (required) - throw ParseError(std::string("Field '")+ name + "' not found"); + if (impl::is(obj[name])) + return impl::get(obj[name]); else - return impl::null(); + throw ParseError(std::string("Field '") + name + "' has invalid type"); } + else if (required) + throw ParseError(std::string("Field '") + name + "' not found"); + else + return impl::null(); +} - const rapidjson::Value& parseObject(const rapidjson::Value& parent, const char* name, bool required, bool& found) +const rapidjson::Value& parseObject(const rapidjson::Value& parent, const char* name, bool required, bool& found) +{ + if (parent.HasMember(name)) { - if (parent.HasMember(name)) + if (parent[name].IsObject()) { - if (parent[name].IsObject()) - { - found = true; - return parent[name]; - } - else - throw ParseError(std::string("Field '")+ name + "' has invalid type"); - } - else if (required) - throw ParseError(std::string("Field '")+ name + "' not found"); - else - { - found = false; - return parent; - } - } - - const rapidjson::Value& parseArray(const rapidjson::Value& parent, const char* name, bool required, bool& found) - { - if (parent.HasMember(name)) - { - if (parent[name].IsArray()) - { - found = true; - return parent[name]; - } - else - throw ParseError(std::string("Field '")+ name + "' has invalid type"); - } - else if (required) - throw ParseError(std::string("Field '")+ name + "' not found"); - else - { - found = false; - return parent; - } - } - - std::unique_ptr parsePhotoSize(const rapidjson::Value& obj) - { - auto photo = std::make_unique(); - photo->setFileId(parse(obj, "file_id", REQUIRED)); - photo->setWidth(parse(obj, "width", REQUIRED)); - photo->setHeight(parse(obj, "height", REQUIRED)); - photo->setFileSize(parse(obj, "file_size", OPTIONAL)); - return photo; - } - - std::unique_ptr parsePhotoSize(const rapidjson::Value& parent, const char* name, bool required) - { - bool found; - auto& obj = parseObject(parent, name, required, found); - if (found) - { - return parsePhotoSize(obj); + found = true; + return parent[name]; } else - return nullptr; + throw ParseError(std::string("Field '") + name + "' has invalid type"); } - - std::unique_ptr parsePhotoSizeArray(const rapidjson::Value& parent, const char* name, bool required) + else if (required) + throw ParseError(std::string("Field '") + name + "' not found"); + else { - bool found; - auto& obj = parseArray(parent, name, required, found); - if (found) - { - std::vector photos; - for (auto& elem : obj.GetArray()) - { - auto photo = parsePhotoSize(elem); - photos.push_back(std::move(*photo)); - } - auto result = std::make_unique(); - result->setArray(photos); - return result; - } - else - return nullptr; - } - - std::unique_ptr