public override async Task Execute(Message message, TelegramBotClient botClient)
        {
            var chatId = message.Chat.Id;

            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Get Chat Id for response message: " + chatId);
            }

            var myMessage = "Here is my data: \n\n";

            myMessage += "//=======\n";
            myMessage += "//-------\n";
            var messageForLogs = "Here is my data: ";

            myMessage      += "BaseDir: " + SentinelMethods.GetBaseDir() + "\n";
            myMessage      += "//-------\n";
            messageForLogs += "BaseDir: " + SentinelMethods.GetBaseDir() + " | ";
            myMessage      += "Base EMS Url: " + SentinelMethods.UrlBuilder() + "\n";
            myMessage      += "//-------\n";
            messageForLogs += "Base EMS Url: " + SentinelMethods.UrlBuilder() + " | ";
            myMessage      += "Url for Login by PK: " + SentinelMethods.UrlBuilder(SentinelSettings.actionsList["loginpk"]) + "\n";
            myMessage      += "//-------\n";
            messageForLogs += "Url for Login by PK: " + SentinelMethods.UrlBuilder(SentinelSettings.actionsList["loginpk"]) + " | ";
            myMessage      += "Url for Get Info by PK: " + SentinelMethods.UrlBuilder(SentinelSettings.actionsList["getinfo"], SentinelSettings.testProductKey) + "\n";
            messageForLogs += "Url for Get Info by PK: " + SentinelMethods.UrlBuilder(SentinelSettings.actionsList["getinfo"], SentinelSettings.testProductKey);
            myMessage      += "//-------\n";
            var myAuthData = SentinelSettings.authXmlString;

            myAuthData      = myAuthData.Replace("{PLACEHOLDER_LOGIN}", SentinelSettings.vendorLogin).Replace("{PLACEHOLDER_PASSWORD}", SentinelSettings.vendorPassword);
            myHttpConnector = myHttpConnector.GetRequest("login", HttpMethod.Post, null, new KeyValuePair <string, string>("authenticationDetail", myAuthData)); // TODO vendor login
            XDocument response = XDocument.Parse(myHttpConnector.httpClientResponseStr);

            myMessage      += "Test Login by Vendor - auth response: \n" + response.ToString() + "\n";
            myMessage      += "//-------\n";
            myHttpConnector = myHttpConnector.GetRequest("logout", HttpMethod.Post, null, new KeyValuePair <string, string>("", ""), myHttpConnector); // TODO vendor logout
            myMessage      += "Test Logout status: \n" + myHttpConnector.httpClientResponseStatus.ToString() + " (If \"204\" or \"NoContent\" - this is mean all correct!)\n";
            myMessage      += "//-------\n";
            myMessage      += "//=======\n";

            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Try to send response message - " + messageForLogs);
            }
            await botClient.SendTextMessageAsync(chatId, myMessage, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown);
        }
Exemplo n.º 2
0
        public override async Task Execute(Message message, TelegramBotClient botClient)
        {
            var chatId = message.Chat.Id;

            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Get Chat Id for response message: " + chatId);
            }

            var myMessage    = "";
            var pKey         = "";
            var savingResult = "";
            var pathForSave  = "";
            var fileName     = "";

            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Try to get Product Key from message: " + message.Text);
            }
            try { pKey = message.Text.Split(" ")[1]; } catch { }
            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Product Key is: " + pKey);
            }

            if (!SentinelMethods.ProductKeyAndAidValidator(pKey))
            {
                // Return error message if PK is invalid
                myMessage = "Product Key is invalid (format)! Please check Product key and try again. ";
            }
            else
            {
                // TODO some logics here...
                myHttpConnector = myHttpConnector.GetRequest("loginpk", HttpMethod.Post, null, new KeyValuePair <string, string>("productKey", pKey)); // TODO login first!
                if (myHttpConnector.httpClientResponseStatus == "OK")
                {
                    myHttpConnector.GetRequest(Name, HttpMethod.Get, pKey, new KeyValuePair <string, string>("productKey", pKey), myHttpConnector);

                    if (myHttpConnector.httpClientResponseStatus == "OK")
                    {
                        var tmpXml = SentinelSettings.pkInfoXmlString;
                        tmpXml = tmpXml.Replace("{PLACEHOLDER}", myHttpConnector.httpClientResponseStr);
                        XDocument pkInfoXml = XDocument.Parse(tmpXml);
                        fileName    = SentinelMethods.FileNameBuilder(Name);
                        pathForSave = SentinelMethods.PathBuilder(fileName);
                        if (!String.IsNullOrEmpty(pathForSave))
                        {
                            var tmp = SentinelSettings.pkInfoXmlString;
                            tmp = tmp.Replace("{PLACEHOLDER}", pkInfoXml.ToString());

                            savingResult = SentinelMethods.SaveFile(pathForSave, tmp);

                            if (savingResult == "OK")
                            {
                                myMessage = "File was saved in dir: " + pathForSave + "\n\n";
                            }
                            else
                            {
                                myMessage = "Problem with saving file on server. Saving result: " + savingResult + "\n\n";
                            }
                        }
                    }
                    else
                    {
                        myMessage = "Get Info error: " + myHttpConnector.httpClientResponseStatus;
                    }
                }
                else
                {
                    myMessage = "Login error: " + myHttpConnector.httpClientResponseStatus;
                }
            }

            if (savingResult == "OK")
            {
                using (var fileStream = new FileStream(pathForSave, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    await botClient.SendDocumentAsync(
                        chatId : chatId,
                        document : new InputOnlineFile(fileStream, fileName),
                        caption : "Here is info about your Product Key: " + pKey
                        );
                }
            }
            else
            {
                if (Startup.myAppSettings.LogIsEnabled)
                {
                    Log.Write(@"Try to send response message - " + myMessage);
                }
                await botClient.SendTextMessageAsync(chatId, myMessage, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown);
            }
        }
        public override async Task Execute(Message message, TelegramBotClient botClient)
        {
            bool goNext = true;

            int maxFileSize = 8 * 1024 * 100; // 100Kb

            string myMessage    = "";
            string savingResult = "";
            string pathForSave  = "";
            string fileName     = "";
            string fileId       = "";
            string fileData     = "";
            string targetXml    = "";
            string haspId       = "";

            var chatId = message.Chat.Id;

            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Get Chat Id for response message: " + chatId);
            }

            if (message.Type != Telegram.Bot.Types.Enums.MessageType.Document)
            {
                // Return error message if C2V is not provided
                myMessage = "C2V is missing! Please do not forget add C2V and try again. ";
                goNext    = false;
            }

            if (goNext)
            {
                fileName    = SentinelMethods.FileNameBuilder(Name, true);
                pathForSave = SentinelMethods.PathBuilder(fileName, true);

                if (!String.IsNullOrEmpty(pathForSave))
                {
                    fileId = message.Document.FileId;
                    if (Startup.myAppSettings.LogIsEnabled)
                    {
                        Log.Write(@"File ID is: " + fileId);
                    }
                    var fileInfo = await botClient.GetFileAsync(fileId);

                    if (Startup.myAppSettings.LogIsEnabled)
                    {
                        Log.Write(@"File size is: " + fileInfo.FileSize.ToString() + " bits (Max file size is: " + maxFileSize.ToString() + " bit (((8 bit * 1024) byte * 100) Kb = 100Kb))");
                    }

                    if (fileInfo.FileSize < maxFileSize)
                    {
                        using (var saveFileStream = System.IO.File.Open(pathForSave, FileMode.Create))
                        {
                            await botClient.DownloadFileAsync(fileInfo.FilePath, saveFileStream);

                            savingResult = "OK";
                        }

                        if (savingResult == "OK")
                        {
                            fileData = await System.IO.File.ReadAllTextAsync(pathForSave);

                            if (Startup.myAppSettings.LogIsEnabled)
                            {
                                Log.Write("Incomming file(size: " + fileInfo.FileSize.ToString() + " bits) was saved in dir: " + pathForSave);
                            }
                        }
                        else
                        {
                            myMessage = "Problem with saving file on server. Saving result: " + savingResult + "\n\n";
                            if (Startup.myAppSettings.LogIsEnabled)
                            {
                                Log.Write("Problem with saving file on server. Saving result: " + savingResult);
                            }
                            goNext = false;
                        }
                    }
                    else
                    {
                        myMessage = "Incomming file size is to big (more then 2 Mb): " + fileInfo.FileSize.ToString() + " bits\n\n";
                        if (Startup.myAppSettings.LogIsEnabled)
                        {
                            Log.Write(@"File size: " + fileInfo.FileSize.ToString() + " bits more then: " + maxFileSize.ToString());
                        }
                        goNext = false;
                    }
                }
                else
                {
                    goNext = false;
                }
            }

            if (goNext && savingResult == "OK")
            {
                targetXml = fileData;

                myHttpConnector = myHttpConnector.GetRequest(Name, HttpMethod.Post, null, new KeyValuePair <string, string>("targetXml", targetXml)); // TODO get updates!
                if (myHttpConnector.httpClientResponseStatus == "OK")
                {
                    XDocument v2cXml = XDocument.Parse(myHttpConnector.httpClientResponseStr);
                    haspId      = v2cXml.Descendants("hasp").FirstOrDefault().Attributes("id").FirstOrDefault().Value.ToString();
                    fileName    = SentinelMethods.FileNameBuilder(Name, key: haspId);
                    pathForSave = SentinelMethods.PathBuilder(fileName);
                    if (!String.IsNullOrEmpty(pathForSave))
                    {
                        savingResult = SentinelMethods.SaveFile(pathForSave, v2cXml.ToString());

                        if (savingResult == "OK")
                        {
                            using (var fileStream = new FileStream(pathForSave, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                await botClient.SendDocumentAsync(
                                    chatId : chatId,
                                    document : new InputOnlineFile(fileStream, fileName),
                                    caption : "Here is your updates for Protection Key ID: " + v2cXml.Descendants("hasp").FirstOrDefault().Attributes("id").FirstOrDefault().Value.ToString()
                                    );

                                myMessage = "";
                                if (Startup.myAppSettings.LogIsEnabled)
                                {
                                    Log.Write("Updates for Protection Key ID: " + v2cXml.Descendants("hasp").FirstOrDefault().Attributes("id").FirstOrDefault().Value.ToString());
                                }
                            }
                        }
                        else
                        {
                            myMessage = "Problem with saving file on server. Saving result: " + savingResult + "\n\n";
                            if (Startup.myAppSettings.LogIsEnabled)
                            {
                                Log.Write("Problem with saving file on server. Saving result: " + savingResult);
                            }
                        }
                    }
                }
                else
                {
                    myMessage = "Get Fetch Pending Updates error: " + myHttpConnector.httpClientResponseStatus;
                    if (Startup.myAppSettings.LogIsEnabled)
                    {
                        Log.Write("Get Fetch Pending Updates error: " + myHttpConnector.httpClientResponseStatus);
                    }
                }

                if (!String.IsNullOrEmpty(myMessage))
                {
                    if (Startup.myAppSettings.LogIsEnabled)
                    {
                        Log.Write(@"Try to send response message - " + myMessage);
                    }
                    await botClient.SendTextMessageAsync(chatId, myMessage, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown);
                }
            }
            else
            {
                if (Startup.myAppSettings.LogIsEnabled)
                {
                    Log.Write(@"Try to send response message - " + myMessage);
                }
                await botClient.SendTextMessageAsync(chatId, myMessage, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown);
            }
        }
        public override async Task Execute(Message message, TelegramBotClient botClient)
        {
            bool goNext = true;

            int maxFileSize = 8 * 1024 * 100; // 100Kb

            string myMessage    = "";
            string pKey         = "";
            string savingResult = "";
            string pathForSave  = "";
            string fileName     = "";
            string fileId       = "";
            string fileData     = "";
            string actXml       = SentinelSettings.activationXmlString;

            var chatId = message.Chat.Id;

            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Get Chat Id for response message: " + chatId);
            }

            if (message.Type != Telegram.Bot.Types.Enums.MessageType.Document)
            {
                // Return error message if C2V is not provided
                myMessage = "C2V is missing! Please do not forget add C2V and try again. ";
                goNext    = false;
            }

            if (goNext)
            {
                if (Startup.myAppSettings.LogIsEnabled)
                {
                    Log.Write(@"Try to get Product Key from message: " + message.Caption);
                }
                try { pKey = message.Caption.Split(" ")[1]; } catch { goNext = false; }
                if (Startup.myAppSettings.LogIsEnabled)
                {
                    Log.Write(@"Product Key is: " + pKey);
                }

                if (String.IsNullOrEmpty(pKey) || !SentinelMethods.ProductKeyAndAidValidator(pKey))
                {
                    // Return error message if PK is invalid
                    myMessage = "Product Key is invalid (format)! Please check Product key and try again. ";
                    goNext    = false;
                }
            }

            if (goNext)
            {
                fileName    = SentinelMethods.FileNameBuilder(Name, true, pKey);
                pathForSave = SentinelMethods.PathBuilder(fileName, true);

                if (!String.IsNullOrEmpty(pathForSave))
                {
                    fileId = message.Document.FileId;
                    if (Startup.myAppSettings.LogIsEnabled)
                    {
                        Log.Write(@"File ID is: " + fileId);
                    }
                    var fileInfo = await botClient.GetFileAsync(fileId);

                    if (Startup.myAppSettings.LogIsEnabled)
                    {
                        Log.Write(@"File size is: " + fileInfo.FileSize.ToString() + " bits (Max file size is: " + maxFileSize.ToString() + " bit (((8 bit * 1024) byte * 100) Kb = 100Kb))");
                    }

                    if (fileInfo.FileSize < maxFileSize)
                    {
                        using (var saveFileStream = System.IO.File.Open(pathForSave, FileMode.Create))
                        {
                            await botClient.DownloadFileAsync(fileInfo.FilePath, saveFileStream);

                            savingResult = "OK";
                        }

                        if (savingResult == "OK")
                        {
                            fileData = await System.IO.File.ReadAllTextAsync(pathForSave);

                            if (Startup.myAppSettings.LogIsEnabled)
                            {
                                Log.Write("Incomming file(size: " + fileInfo.FileSize.ToString() + " bits) was saved in dir: " + pathForSave);
                            }
                        }
                        else
                        {
                            myMessage = "Problem with saving file on server. Saving result: " + savingResult + "\n\n";
                            if (Startup.myAppSettings.LogIsEnabled)
                            {
                                Log.Write("Problem with saving file on server. Saving result: " + savingResult);
                            }
                            goNext = false;
                        }
                    }
                    else
                    {
                        myMessage = "Incomming file size is to big (more then 2 Mb): " + fileInfo.FileSize.ToString() + " bits\n\n";
                        if (Startup.myAppSettings.LogIsEnabled)
                        {
                            Log.Write(@"File size: " + fileInfo.FileSize.ToString() + " bits more then: " + maxFileSize.ToString());
                        }
                        goNext = false;
                    }
                }
                else
                {
                    goNext = false;
                }
            }

            if (goNext && savingResult == "OK")
            {
                actXml          = actXml.Replace("{PLACEHOLDER}", fileData);
                myHttpConnector = myHttpConnector.GetRequest("loginpk", HttpMethod.Post, null, new KeyValuePair <string, string>("productKey", pKey)); // TODO login first!
                if (myHttpConnector.httpClientResponseStatus == "OK")
                {
                    myHttpConnector = myHttpConnector.GetRequest(Name, HttpMethod.Post, pKey, new KeyValuePair <string, string>("activationXml", actXml), myHttpConnector); // TODO activation!
                    if (myHttpConnector.httpClientResponseStatus == "OK")
                    {
                        XDocument activationResultXml = XDocument.Parse(myHttpConnector.httpClientResponseStr);
                        fileName    = SentinelMethods.FileNameBuilder(Name);
                        pathForSave = SentinelMethods.PathBuilder(fileName);
                        if (!String.IsNullOrEmpty(pathForSave))
                        {
                            XDocument v2cXml = XDocument.Parse(activationResultXml.Descendants("activationString").FirstOrDefault().Value);

                            savingResult = SentinelMethods.SaveFile(pathForSave, v2cXml.ToString());

                            if (savingResult == "OK")
                            {
                                using (var fileStream = new FileStream(pathForSave, FileMode.Open, FileAccess.Read, FileShare.Read))
                                {
                                    await botClient.SendDocumentAsync(
                                        chatId : chatId,
                                        document : new InputOnlineFile(fileStream, fileName),
                                        caption : "Here is your license by Product Key: " + pKey + "\n" + "AID: " + activationResultXml.Descendants("AID").FirstOrDefault().Value
                                        );

                                    myMessage = "";
                                    if (Startup.myAppSettings.LogIsEnabled)
                                    {
                                        Log.Write("License by Product Key: " + pKey + " | AID: " + activationResultXml.Descendants("AID").FirstOrDefault().Value);
                                    }
                                }
                            }
                            else
                            {
                                myMessage = "Problem with saving file on server. Saving result: " + savingResult + "\n\n";
                                if (Startup.myAppSettings.LogIsEnabled)
                                {
                                    Log.Write("Problem with saving file on server. Saving result: " + savingResult);
                                }
                            }
                        }
                    }
                    else
                    {
                        myMessage = "Activation error: " + myHttpConnector.httpClientResponseStatus;
                        if (Startup.myAppSettings.LogIsEnabled)
                        {
                            Log.Write("Activation error: " + myHttpConnector.httpClientResponseStatus);
                        }
                    }
                }
                else
                {
                    myMessage = "Login error: " + myHttpConnector.httpClientResponseStatus;
                    if (Startup.myAppSettings.LogIsEnabled)
                    {
                        Log.Write("Login error: " + myHttpConnector.httpClientResponseStatus);
                    }
                }

                if (!String.IsNullOrEmpty(myMessage))
                {
                    if (Startup.myAppSettings.LogIsEnabled)
                    {
                        Log.Write(@"Try to send response message - " + myMessage);
                    }
                    await botClient.SendTextMessageAsync(chatId, myMessage, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown);
                }
            }
            else
            {
                if (Startup.myAppSettings.LogIsEnabled)
                {
                    Log.Write(@"Try to send response message - " + myMessage);
                }
                await botClient.SendTextMessageAsync(chatId, myMessage, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown);
            }
        }
        public override async Task Execute(Message message, TelegramBotClient botClient)
        {
            var chatId = message.Chat.Id;

            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Get Chat Id for response message: " + chatId);
            }

            var myMessage    = "";
            var pKey         = "";
            var aid          = "";
            var savingResult = "";
            var pathForSave  = "";
            var fileName     = "";

            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"Try to get AID and Product Key from message: " + message.Text);
            }
            try {
                var tmpMass = message.Text.Split(" ");
                foreach (string el in tmpMass)
                {
                    if (el.Contains("AID:") || el.Contains("aid:"))
                    {
                        aid = el.Split(":")[1];
                    }
                    if (el.Contains("PK:") || el.Contains("pk:"))
                    {
                        pKey = el.Split(":")[1];
                    }
                }
            } catch { }
            if (Startup.myAppSettings.LogIsEnabled)
            {
                Log.Write(@"AID is: " + aid + " | Product Key is: " + pKey);
            }

            if (!SentinelMethods.ProductKeyAndAidValidator(aid) || !SentinelMethods.ProductKeyAndAidValidator(pKey))
            {
                // Return error message if AID or Product Key is invalid
                myMessage = "AID or Product Key is invalid (format)! Please check AID and Product Key and try again. ";
            }
            else
            {
                // TODO some logics here...
                myHttpConnector = myHttpConnector.GetRequest("loginpk", HttpMethod.Post, null, new KeyValuePair <string, string>("productKey", pKey)); // TODO login first!
                if (myHttpConnector.httpClientResponseStatus == "OK")
                {
                    myHttpConnector.GetRequest(Name, HttpMethod.Get, aid, new KeyValuePair <string, string>("aid", aid), myHttpConnector); // TODO get license by AID
                    if (myHttpConnector.httpClientResponseStatus == "OK")
                    {
                        XDocument response = XDocument.Parse(myHttpConnector.httpClientResponseStr);

                        fileName    = SentinelMethods.FileNameBuilder(Name);
                        pathForSave = SentinelMethods.PathBuilder(fileName);
                        if (!String.IsNullOrEmpty(pathForSave))
                        {
                            XDocument v2cXml = XDocument.Parse(response.Descendants("activationString").FirstOrDefault().Value);

                            savingResult = SentinelMethods.SaveFile(pathForSave, v2cXml.ToString());

                            if (savingResult == "OK")
                            {
                                myMessage = "File was saved in dir: " + pathForSave + "\n\n";
                            }
                            else
                            {
                                myMessage = "Problem with saving file on server. Saving result: " + savingResult + "\n\n";
                            }
                        }
                    }
                    else
                    {
                        myMessage = "Get license by AID error: " + myHttpConnector.httpClientResponseStatus;
                    }
                }
                else
                {
                    myMessage = "Login error: " + myHttpConnector.httpClientResponseStatus;
                }
            }

            if (savingResult == "OK")
            {
                using (var fileStream = new FileStream(pathForSave, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    await botClient.SendDocumentAsync(
                        chatId : chatId,
                        document : new InputOnlineFile(fileStream, fileName),
                        caption : "Here is your license getting by AID: " + aid
                        );
                }
            }
            else
            {
                if (Startup.myAppSettings.LogIsEnabled)
                {
                    Log.Write(@"Try to send response message - " + myMessage);
                }
                await botClient.SendTextMessageAsync(chatId, myMessage, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown);
            }
        }