#include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace tg { namespace td_api = td::td_api; using TgObject = td_api::object_ptr; namespace detail { template struct overload; template struct overload : public F { explicit overload(F f) : F(f) { } }; template struct overload : public overload , public overload { overload(F f, Fs... fs) : overload(f), overload(fs...) { } using overload::operator(); using overload::operator(); }; } // namespace detail typedef std::function)> AuthCodeProvider; typedef std::function)> PasswordProvider; class TelegramClient { public: TelegramClient(uv_loop_t *eventLoop, long apiId, std::string apiHash, std::string phoneNumber); ~TelegramClient(); void add_update_handler(std::function handler, void *context = nullptr); void send_query(td_api::object_ptr f, std::function callback); bool start(); AuthCodeProvider authCodeProvider; PasswordProvider passwordProvider; private: static void uv_callback(uv_async_t *h); static void uv_close_handle_callback(uv_handle_t *h); void run_thread(); void run_telegram_main_loop(); bool process_response(td::ClientManager::Response response); void dispatch_response(std::uint64_t queryId, TgObject response); void dispatch_update(TgObject &update); void set_tdlib_parameters(); void set_phone_number(); void set_code(std::string code); void set_password(std::string password); bool m_running = false; bool m_authorized = false; std::uint64_t m_nextQueryId = 0; std::map> m_handlers; std::vector, void*>> m_updateHandlers; std::thread m_thread; std::unique_ptr m_clientManager; std::int32_t m_clientId; uv_loop_t *m_eventLoop; uv_async_t *m_uvHandle = nullptr; long m_apiId; std::string m_apiHash; std::string m_phoneNumber; std::string m_password; std::deque m_responseQueue; std::mutex m_responseQueueMutex; }; }