#pragma once #include #include #include #include #include #include #include #include #include #include "userstatus.h" #include "user.h" namespace td_api = td::td_api; using Object = td_api::object_ptr; template using objptr = td_api::object_ptr; using MessageCallback = std::function)>; using StatusCallback = std::function; using DraftCallback = std::function; using ActionCallback = std::function; using DeleteCallback = std::function; using UserUpdateCallback = std::function; using EditMessageCallback = std::function)>; using IndexDoneCallback = std::function; using FileUpdateCallback = std::function)>; using NewChatCallback = std::function)>; using ChatFiltersCallback = std::function)>; class TGClient { public: static const uint64_t STATICHANDLERCOUNT = 128; static void(TGClient::* STATICHANDLERS [STATICHANDLERCOUNT])(objptr); TGClient(std::function initDoneCallback = {}); void setAuthData(std::function getAuthCodeCallback); void loop(); void stop(); void registerNewMessageHandler(MessageCallback mclb); void registerStatusUpdateHandler(StatusCallback sclb); void registerDraftHandler(DraftCallback dclb); void registerActionHandler(ActionCallback clb); void registerDeleteHandler(DeleteCallback dclb); void registerUserUpdateHandler(UserUpdateCallback uuclb); void registerEditMessageHandler(EditMessageCallback emclb); void registerIndexDoneHandle(IndexDoneCallback idclb); void registerFileUpdateCallback(FileUpdateCallback fuclb); void registerNewChatCallback(NewChatCallback ncclb); void registerChatFiltersCallback(ChatFiltersCallback cfclb); //exposed apicalls void openChat(int64_t chatid); void closeChat(int64_t chatid); void deleteMessage(int64_t chatid, int64_t messageid, bool forall = true); void screenShottaken(int64_t chatid); void reply(const objptr& rplyto, const std::string& awnser); void reply(int64_t chat, int64_t msg, const std::string& awnser); void editMessage(const objptr& toEdit, const std::string& newText); void editMessage(const objptr& toEdit, const std::string& newText, std::vector>& entities); void editMessageCaption(const objptr& toEdit, const std::string& newText, std::vector>& entities); void sendCallbackQuery(int64_t chat, int64_t messageid, const std::string& payload = ""); // mute for is in seconds (more than 604800 => forever) void muteChat(int64_t chatid, int32_t mutefor = 1<<30); void addChatToChatListFilter(int64_t chatid, int32_t chatfilterid); //index chat (editMessageCallback is called for every Message in the chat) void indexChat(int64_t chatid); void getAllTextMessages(int64_t chatid, std::function)>); void downloadFile(int32_t file_id); void sendTextMessageToSelf(const std::string& text); void getMessage(int64_t chatid, int64_t messageid, std::function)> f); //drafts void setDraft(int64_t chat, const std::string& text = "", int64_t replyto = 0, int64_t messageThread = 0); void clearDraft(int64_t chat, int64_t messageThread = 0); bool hasDraft(int64_t chat) const; const User* getCachedUser(int64_t userid); int64_t me = 0; //my chatid private: bool shouldrun = false; bool initDone = false; std::unique_ptr client_manager_; std::int32_t client_id_{0}; td_api::object_ptr authorization_state_; bool are_authorized_{false}; bool need_restart_{false}; std::uint64_t current_query_id_ = STATICHANDLERCOUNT; std::uint64_t authentication_query_id_{0}; std::map> handlers_; //handler list maps updateid -> callback std::map> drafts; // chatid -> draft message / list of user drafts (does not contain drafts from the client itself) //user cache struct CachedUser : public User { uint64_t lastaccessed = 0; }; std::map usercache; //list of cached users std::multimap usercache_timeout; //list of last access -> to know when to clean up which object static const uint32_t MAXCACHEDUSERCOUNT; //at how many users are allowed until the cache si cleand static const uint64_t CACHEDUSERCOUNTCLEANUPTIME; //how old is a cached user allowed to get before beeing purged from the cache (in seconds) std::function initDoneCallback; //callbacks std::function getAuthCodeCallback; MessageCallback messageCallback; StatusCallback statusCallback; DraftCallback draftCallback; ActionCallback actionCallback; DeleteCallback deleteCallback; UserUpdateCallback userupdCallback; EditMessageCallback editMessageCallback; IndexDoneCallback indexDoneCallback; FileUpdateCallback fileUpdateCallback; NewChatCallback newChatCallback; ChatFiltersCallback chatFiltersCallback; //user cache void accessUser(std::map::iterator it); //iterator to cached user object in usercache void addUser(CachedUser& u); void removeUser(std::map::iterator it); //iterator to cached user object in usercache) void checkUserCache(); //wrapped requests void requestMessages(int64_t chatid, int64_t from_message_id = 0, std::function onDone = {}, std::function)> forMessage = {}); //helper void iterate(const td_api::array>& messages, std::function handler); void setOption(const std::string& name, objptr val); void setOptions(); //load me and chat list, initDoneCallback is triggered after that void loadInit(); void loadInitInternalCallback(Object o); void restart(); void send_query(td_api::object_ptr f, std::function handler = {}); void send_staticquery(td_api::object_ptr f, uint64_t handlerid); template void send_wrappedquery(td_api::object_ptr f, std::function)> handler = {}, std::function onError = {}, bool printError = true); template static bool catchErrors(const Object& o, std::function onError = {}, bool printError = true); template void send_inplace(Args... args, std::function)> handler = {}, bool printError = true); void process_response(td::ClientManager::Response response); void process_update(td_api::object_ptr update); auto create_authentication_query_handler(); void on_authorization_state_update(); void check_authentication_error(Object object); std::uint64_t next_query_id(); };