Example #1
0
        static void Main()
        {
            // disable TDLib log
            Td.Client.Execute(new TdApi.SetLogVerbosityLevel(0));
            if (Td.Client.Execute(new TdApi.SetLogStream(new TdApi.LogStreamFile("tdlib.log", 1 << 27))) is TdApi.Error)
            {
                throw new System.IO.IOException("Write access to the current directory is required");
            }

            // create Td.Client
            _client = CreateTdClient();

            // test Client.Execute
            _defaultHandler.OnResult(Td.Client.Execute(new TdApi.GetTextEntities("@telegram /test_command https://telegram.org telegram.me @gif @test")));

            // main loop
            while (!_quiting)
            {
                // await authorization
                _gotAuthorization.Reset();
                _gotAuthorization.WaitOne();

                _client.Send(new TdApi.GetChats(null, Int64.MaxValue, 0, 100), _defaultHandler); // preload main chat list
                while (_haveAuthorization)
                {
                    GetCommand();
                }
            }
        }
Example #2
0
        /// <summary>
        /// executes custom tdlib command synchronously
        /// </summary>
        /// <param name="command">command to execute</param>
        /// <param name="expectedType">expected type to reveice after command</param>
        /// <param name="timeOut">timeout (ms) to wait for command response</param>
        /// <returns>ResponseObject providing results</returns>
        public async Task <ResponseObject> executeCommand(Function command, Type expectedType = null, int timeOut = 5000)
        {
            // object to return as response
            ResponseObject res = new ResponseObject();

            // function to get invoked when update came ( any type )
            Action <BaseObject> _handler = (obj) =>
            {
                // setting response
                res.obj = obj;

                // check if expected type is set
                if (expectedType != null)
                {
                    // check if gathered update type is same as expected type
                    if (obj.GetType() == expectedType)
                    {
                        res.status = Enum.ResponseStatus.Success;
                    }
                    else
                    {
                        res.status = Enum.ResponseStatus.Failed;
                    }
                }
                else
                {
                    // if expectedType is null, status is always successfull
                    res.status = Enum.ResponseStatus.Success;
                }
            };

            // custom handler
            ApiResponseHandler customHandler = new ApiResponseHandler(this, _handler);

            // sending request
            _client.Send(command, customHandler);

            // waiting for timeOut
            // if (!resetEvent.WaitOne(timeOut)) res.status = Enum.ResponseStatus.TimedOut;
            for (int i = 0; i < timeOut; i += 1000)
            {
                // check if response gathered
                if (res.status != Enum.ResponseStatus.Processing)
                {
                    break;
                }

                await Task.Delay(1000);
            }

            // if response object is null, means operation is timed out
            if (res.obj is null)
            {
                res.status = Enum.ResponseStatus.TimedOut;
            }

            return(res);
        }
Example #3
0
        static void Main()
        {
            // disable TDLib log
            Td.Client.Execute(new TdApi.SetLogVerbosityLevel(0));
            if (Td.Client.Execute(new TdApi.SetLogStream(new TdApi.LogStreamFile("tdlib.log", 1 << 27, false))) is TdApi.Error)
            {
                throw new System.IO.IOException("Write access to the current directory is required");
            }
            new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;
                Td.Client.Run();
            }).Start();

            // create Td.Client
            _client = CreateTdClient();

            // test Client.Execute
            _defaultHandler.OnResult(Td.Client.Execute(new TdApi.GetTextEntities("@telegram /test_command https://telegram.org telegram.me @gif @test")));

            // main loop
            while (!_needQuit)
            {
                // await authorization
                _gotAuthorization.Reset();
                _gotAuthorization.WaitOne();

                _client.Send(new TdApi.LoadChats(null, 100), _defaultHandler); // preload main chat list
                while (_haveAuthorization)
                {
                    GetCommand();
                }
            }
            while (!_canQuit)
            {
                Thread.Sleep(1);
            }
        }
Example #4
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var command = Input.Text;

            if (command.StartsWith("DESTROY"))
            {
                AcceptCommand("Destroy");
                _client.Send(new TdApi.Destroy(), _handler);
            }
            else if (command.StartsWith("lo"))
            {
                AcceptCommand("LogOut");
                _client.Send(new TdApi.LogOut(), _handler);
            }
            else if (command.StartsWith("gas"))
            {
                AcceptCommand(command);
                _client.Send(new TdApi.GetAuthorizationState(), _handler);
            }
            else if (command.StartsWith("sap"))
            {
                var args = command.Split(" ".ToCharArray(), 2);
                AcceptCommand(command);
                _client.Send(new TdApi.SetAuthenticationPhoneNumber(args[1], null), _handler);
            }
            else if (command.StartsWith("cac"))
            {
                var args = command.Split(" ".ToCharArray(), 2);
                AcceptCommand(command);
                _client.Send(new TdApi.CheckAuthenticationCode(args[1]), _handler);
            }
            else if (command.StartsWith("cap"))
            {
                var args = command.Split(" ".ToCharArray(), 2);
                AcceptCommand(command);
                _client.Send(new TdApi.CheckAuthenticationPassword(args[1]), _handler);
            }
            else if (command.StartsWith("gco"))
            {
                var args = command.Split(" ".ToCharArray(), 2);
                AcceptCommand(command);
                _client.Send(new TdApi.SearchContacts(), _handler);
            }
            else if (command.StartsWith("df"))
            {
                var args = command.Split(" ".ToCharArray(), 2);
                AcceptCommand(command);
                _client.Send(new TdApi.DownloadFile(Int32.Parse(args[1]), 1, 0, 0, false), _handler);
            }
            else if (command.StartsWith("bench"))
            {
                var args = command.Split(" ".ToCharArray(), 2);
                AcceptCommand(command);
                var cnt     = Int32.Parse(args[1]);
                var handler = new BenchSimpleHandler(this, cnt);
                for (int i = 0; i < cnt; i++)
                {
                    _client.Send(new TdApi.TestSquareInt(123), handler);
                }
            }
        }
Example #5
0
        private void OnAuthorizationStateUpdated(TdApi.AuthorizationState authorizationState)
        {
            if (authorizationState != null)
            {
                _authorizationState = authorizationState;
            }
            if (_authorizationState is TdApi.AuthorizationStateWaitTdlibParameters)
            {
                TdApi.TdlibParameters parameters = new TdApi.TdlibParameters();
                parameters.DatabaseDirectory  = "tdlibXXX";
                parameters.UseMessageDatabase = true;
                parameters.UseSecretChats     = true;
                parameters.ApiId                  = 94575;
                parameters.ApiHash                = "a3406de8d171bb422bb6ddf3bbd800e2";
                parameters.SystemLanguageCode     = "en";
                parameters.DeviceModel            = "Desktop";
                parameters.SystemVersion          = "Unknown";
                parameters.ApplicationVersion     = "1.0";
                parameters.EnableStorageOptimizer = true;

                _client.Send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitEncryptionKey)
            {
                _client.Send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitPhoneNumber)
            {
                string phoneNumber = ReadLine("Please enter phone number: ");
                _client.Send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitCode)
            {
                string code = ReadLine("Please enter authentication code: ");
                _client.Send(new TdApi.CheckAuthenticationCode(code, "", ""), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitPassword)
            {
                string password = ReadLine("Please enter password: "******"Logging out");
            }
            else if (_authorizationState is TdApi.AuthorizationStateClosing)
            {
                _haveAuthorization = false;
                Print("Closing");
            }
            else if (_authorizationState is TdApi.AuthorizationStateClosed)
            {
                Print("Closed");
                if (!_quiting)
                {
                    _client = CreateTdClient(); // recreate _client after previous has closed
                }
            }
            else
            {
                Print("Unsupported authorization state:" + _newLine + _authorizationState);
            }
        }