Пример #1
0
        static void Main(string[] args)
        {
            List <string> argsList = args.ToList();

            List <string> lDirectoriesToSearchIn = new List <string>();
            List <string> lFilenamesToSearch     = new List <string>();

            if (!argsList.Any(str => str.Contains("--directories-listfile=")))
            {
                Console.WriteLine("--directories-listfile is not specified. Current working directory will be used.");
                lDirectoriesToSearchIn.Add(Directory.GetCurrentDirectory());
            }
            else
            {
                string arg = argsList.First(str => str.Contains("--directories-listfile="));
                string strDirectoriesListFile = arg.Replace("--directories-listfile=", "");
                if (!File.Exists(strDirectoriesListFile))
                {
                    Console.WriteLine("--directories-listfile does not exist. Current working directory will be used.");
                    lDirectoriesToSearchIn.Add(Directory.GetCurrentDirectory());
                }
                else
                {
                    string        strFileContents = ServiceTools.ReadTextFromFile(strDirectoriesListFile);
                    List <string> lDirectories    = strFileContents.Split(Environment.NewLine.ToCharArray()).ToList();
                    lDirectories.RemoveAll(str => str.Length == 0);
                    lDirectoriesToSearchIn.AddRange(lDirectories);
                }
            }

            Console.WriteLine("Files will be searched in directories:");
            foreach (string s in lDirectoriesToSearchIn)
            {
                Console.WriteLine(s);
            }



            if (!argsList.Any(str => str.Contains("--fileslist=")))
            {
                Console.WriteLine("--fileslist is not specified. Can`t proceed.");
                return;
            }
            else
            {
                string arg = argsList.First(str => str.Contains("--fileslist="));
                string strFilesList_file = arg.Replace("--fileslist=", "");
                if (!File.Exists(strFilesList_file))
                {
                    Console.WriteLine("--fileslist does not exist. Can`t proceed.");
                    return;
                }
                else
                {
                    string        strFileContents = ServiceTools.ReadTextFromFile(strFilesList_file);
                    List <string> lFilenames      = strFileContents.Split(Environment.NewLine.ToCharArray()).ToList();
                    lFilenames.RemoveAll(str => str.Length == 0);
                    lFilenamesToSearch.AddRange(lFilenames);
                }
            }

            Console.WriteLine("Files to search count: " + lFilenamesToSearch.Count);

            List <string> lFilesFound = new List <string>();

            foreach (string filename in lFilenamesToSearch)
            {
                Console.WriteLine("searching for file " + filename);
                foreach (string dirname in lDirectoriesToSearchIn)
                {
                    DirectoryInfo   currDirInfo    = new DirectoryInfo(dirname);
                    List <FileInfo> filenamesFound = currDirInfo.GetFiles(filename, SearchOption.AllDirectories).ToList();
                    if (filenamesFound.Any())
                    {
                        lFilesFound.AddRange(filenamesFound.ConvertAll(finfo => finfo.FullName));
                        break;
                    }
                }
            }

            Console.WriteLine("Found files:");
            foreach (string fileFound in lFilesFound)
            {
                Console.WriteLine(fileFound);
            }

            if (argsList.Any(str => str.Contains("--copy-to=")))
            {
                string arg = argsList.First(str => str.Contains("--copy-to="));
                string directoryToCopyTo = arg.Replace("--copy-to=", "");
                directoryToCopyTo = directoryToCopyTo +
                                    ((directoryToCopyTo.Last() == Path.DirectorySeparatorChar)
                                        ? ("")
                                        : (Path.DirectorySeparatorChar.ToString()));

                foreach (string fileFound in lFilesFound)
                {
                    File.Copy(fileFound, directoryToCopyTo + Path.GetFileName(fileFound));
                    Console.WriteLine(fileFound + " >> " + directoryToCopyTo + Path.GetFileName(fileFound));
                }
            }
        }
Пример #2
0
        private async Task Run()
        {
            if (tgrm_token == "")
            {
                Console.WriteLine("telegram token for channel is not set. Processing will not proceed.");
                Console.WriteLine("Finished. Press any key...");
                Console.ReadKey();
                return;
            }
            var Bot = new Api(tgrm_token);

            var me = await Bot.GetMe();

            Console.WriteLine("Hello my name is {0}", me.Username);

            var offset = 0;

            while (true)
            {
                var updates = await Bot.GetUpdates(offset);

                foreach (var update in updates)
                {
                    if (update.Message.Type == MessageType.TextMessage)
                    {
                        if (ServiceTools.CheckIfDirectoryExists(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "logs"))
                        {
                            ServiceTools.logToTextFile(logFilename,
                                                       "" + update.Message.Chat.Id + " : " + update.Message.Text + Environment.NewLine, true, true);
                        }



                        if (update.Message.Text == "/current_image")
                        {
                            string            FilenameToSend   = "";
                            Image <Bgr, byte> lastImagesCouple = CurrentImagesCouple(out FilenameToSend);
                            string            tmpFNameToSave   = Path.GetTempPath();
                            tmpFNameToSave += (tmpFNameToSave.Last() == Path.DirectorySeparatorChar)
                                ? ("")
                                : (Path.DirectorySeparatorChar.ToString());
                            tmpFNameToSave += FilenameToSend;
                            lastImagesCouple.Save(tmpFNameToSave);
                            var     fileStream  = File.Open(tmpFNameToSave, FileMode.Open);
                            Message sentMessage = await Bot.SendPhoto(update.Message.Chat.Id, new FileToSend(FilenameToSend, fileStream));

                            File.Delete(tmpFNameToSave);
                        }
                        else if (update.Message.Text == "/concurrent_info")
                        {
                            string lastConcurrentInfoFileName = GetLastConcurrentInfo();

                            Message sentMessage =
                                await
                                Bot.SendTextMessage(update.Message.Chat.Id, lastConcurrentInfoFileName, true, false,
                                                    update.Message.MessageId);
                        }
                        else if (update.Message.Text == "/current_cc")
                        {
                            Task <string> tskObtainCC = ObtainLastImageCC();

                            //Message sentMessage1 =
                            //    await
                            //        Bot.SendTextMessage(update.Message.Chat.Id,
                            //            "Started calculations. Be patient please, it may take a few minutes.", true,
                            //            false, update.Message.MessageId);

                            string strReply = await tskObtainCC;

                            Message sentMessage2 =
                                await
                                Bot.SendTextMessage(update.Message.Chat.Id, strReply, true, false,
                                                    update.Message.MessageId);
                        }
                        else if (update.Message.Text == "/meteo_info")
                        {
                            string strCurrentMeteoParametersString = await ObtainLatestMeteoParameters();

                            Message sentMessage =
                                await
                                Bot.SendTextMessage(update.Message.Chat.Id, strCurrentMeteoParametersString, true, false,
                                                    update.Message.MessageId);
                        }
                        //else if (update.Message.Text == "/solar_irradiation")
                        //{
                        //    string strCurrentRadiometersString = await ObtainLatestRadiometersParameters();
                        //    Message sentMessage =
                        //        await
                        //            Bot.SendTextMessage(update.Message.Chat.Id, strCurrentRadiometersString, true, false,
                        //                update.Message.MessageId);

                        //}
                        else if (update.Message.Text == "/start")
                        {
                            string strStartMessage =
                                ServiceTools.ReadTextFromFile(Directory.GetCurrentDirectory() +
                                                              Path.DirectorySeparatorChar + "settings" +
                                                              Path.DirectorySeparatorChar + "BotStartMessage.txt");
                            Message sentMessage =
                                await
                                Bot.SendTextMessage(update.Message.Chat.Id, strStartMessage, true, false,
                                                    update.Message.MessageId);

                            Console.WriteLine("Echo Message: {0}", update.Message.Text);
                        }
                        else
                        {
                            // await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                            // await Task.Delay(200);
                            var t = await Bot.SendTextMessage(update.Message.Chat.Id, "I can`t get your request. Please try again.");

                            Console.WriteLine("Echo Message: {0}", update.Message.Text);
                        }
                    }

                    if (update.Message.Type == MessageType.PhotoMessage)
                    {
                        var file = await Bot.GetFile(update.Message.Photo.LastOrDefault()?.FileId);

                        Console.WriteLine("Received Photo: {0}", file.FilePath);

                        var filename = IncomingImagesBasePath + file.FileId + "." + file.FilePath.Split('.').Last();

                        using (var profileImageStream = File.Open(filename, FileMode.Create))
                        {
                            await file.FileStream.CopyToAsync(profileImageStream);
                        }

                        Console.WriteLine("Photo saved to: {0}", filename);

                        if (ServiceTools.CheckIfDirectoryExists(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "logs"))
                        {
                            ServiceTools.logToTextFile(logFilename,
                                                       "" + update.Message.Chat.Id + " : Received Photo: " + file.FilePath + Environment.NewLine + "\t\tSaved to file: " + filename + Environment.NewLine, true, true);
                        }
                    }

                    offset = update.Id + 1;
                }

                await Task.Delay(200);

                if (NeedToStopFlag)
                {
                    break;
                }
            }
        }
        private void Bot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
        {
            Message currMsg = e.Message;


            if (currMsg.Type == MessageType.TextMessage)
            {
                Console.WriteLine("{0} from {1} ({2})", currMsg.Text, currMsg.Chat.Id, currMsg.Chat.Username);


                if (ServiceTools.CheckIfDirectoryExists(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "logs"))
                {
                    ServiceTools.logToTextFile(logFilename,
                                               "" + currMsg.Chat.Id + " (" + currMsg.Chat.Username + ") : " + currMsg.Text + Environment.NewLine, true, true);
                }



                if ((currMsg.Text == "/current_image") || (currMsg.Text == "Image"))
                {
                    Task <string> taskA        = Task.Run(() => CurrentImagesCoupleImageFilename());
                    Task          continuation = taskA.ContinueWith(antecedent =>
                    {
                        string FilenameToSend = antecedent.Result;
                        var fileStream        = File.Open(FilenameToSend, FileMode.Open, FileAccess.Read, FileShare.Read);
                        Bot.SendPhotoAsync(currMsg.Chat.Id, new FileToSend(FilenameToSend, fileStream));
                    });
                }
                else if ((currMsg.Text == "/concurrent_info") || (currMsg.Text == "Info"))
                {
                    Task <string> taskA        = Task.Run(() => ReadLastConcurrentInfo());
                    Task          continuation = taskA.ContinueWith(antecedent =>
                    {
                        string strReply = antecedent.Result;
                        Bot.SendTextMessageAsync(currMsg.Chat.Id, strReply, true, false,
                                                 currMsg.MessageId, BasicKeyboard());
                    });
                }
                else if ((currMsg.Text == "/current_cc") || (currMsg.Text == "TCC_SDC"))
                {
                    Task <string> taskA        = Task.Run(ReadCurrentCCinfo);
                    Task          continuation = taskA.ContinueWith(antecedent =>
                    {
                        string strReply = antecedent.Result;
                        Bot.SendTextMessageAsync(currMsg.Chat.Id, strReply, true, false,
                                                 currMsg.MessageId, BasicKeyboard());
                    });
                }
                else if ((currMsg.Text == "/meteo_info") || (currMsg.Text == "Meteo"))
                {
                    Task <string> taskA        = Task.Run(ObtainLatestMeteoParameters);
                    Task          continuation = taskA.ContinueWith(antecedent =>
                    {
                        string strReply = antecedent.Result;
                        Bot.SendTextMessageAsync(currMsg.Chat.Id, strReply, true, false,
                                                 currMsg.MessageId, BasicKeyboard());
                    });
                }
                else if (currMsg.Text == "/start")
                {
                    Task taskA = Task.Run(() =>
                    {
                        string strStartMessage =
                            ServiceTools.ReadTextFromFile(Directory.GetCurrentDirectory() +
                                                          Path.DirectorySeparatorChar + "settings" +
                                                          Path.DirectorySeparatorChar + "BotStartMessage.txt");
                        Bot.SendTextMessageAsync(currMsg.Chat.Id, strStartMessage, true, false,
                                                 currMsg.MessageId, BasicKeyboard());
                        Console.WriteLine("Echo Message: {0}", currMsg.Text);
                    });
                }
                else if (currMsg.Text.Contains("/fix"))
                {
                    Console.WriteLine("Echo Message: {0}", currMsg.Text);

                    Task <bool> taskA = Task.Run(() => RegisterFixStage1(currMsg));
                }
                else if (currMsg.Text.Contains("/help") || currMsg.Text == "Help")
                {
                    Task taskA = Task.Run(() =>
                    {
                        string strStartMessage =
                            ServiceTools.ReadTextFromFile(Directory.GetCurrentDirectory() +
                                                          Path.DirectorySeparatorChar + "settings" +
                                                          Path.DirectorySeparatorChar + "BotHelpMessage.txt");
                        Bot.SendTextMessageAsync(currMsg.Chat.Id, strStartMessage, true, false,
                                                 currMsg.MessageId, BasicKeyboard());
                        Console.WriteLine("Echo Message: {0}", currMsg.Text);
                    });
                }
                else
                {
                    // await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                    // await Task.Delay(200);

                    Task taskA = Task.Run(() =>
                    {
                        Bot.SendTextMessageAsync(currMsg.Chat.Id,
                                                 "Sorry, I can`t understand you. Please try again using the following list of commands:" +
                                                 Environment.NewLine + "/current_image" +
                                                 Environment.NewLine + "/concurrent_info" +
                                                 Environment.NewLine + "/current_cc" +
                                                 Environment.NewLine + "/meteo_info" +
                                                 Environment.NewLine + "/fix",
                                                 replyMarkup: BasicKeyboard());

                        Console.WriteLine("Echo Message: {0}", currMsg.Text);
                    });
                }
            }
        }