예제 #1
0
파일: Form3.cs 프로젝트: anton-mix/Projects
        private void CreatePrintFile_Click(object sender, RoutedEventArgs e)
        {
            if (!ValidPrintForm())
            {
                return;
            }
            SendAlbum album;

            try {
                lErrors3.Document.Blocks.Clear();
                album = MyFunctions.GetSendPeoples(tbPathFolderPrint1.Text);
            }
            catch (BadFormatException ex) {
                lErrors3.Document.Blocks.Add(new Paragraph(new Run(ex.Error)));
                return;
            }
            catch (UnknownColumnException ex) {
                lErrors3.Document.Blocks.Add(new Paragraph(new Run("Найден неизвестный столбец")));
                return;
            }
            catch (Exception ex) {
                lErrors3.Document.Blocks.Add(new Paragraph(new Run("Не удалось прочесть файл. Возможно не хватает некоторых полей. " + ex.ToString())));
                return;
            }

            try {
                lErrors3.Document.Blocks.Clear();
                CreatePrintFiles(album, tbPathFolderPrint2.Text, cbFormat.SelectedIndex);
                lErrors3.Document.Blocks.Add(new Paragraph(new Run("Файл сформирован.")));
            }
            catch (BadFormatSafeException ex) {
                lErrors3.Document.Blocks.Add(new Paragraph(new Run("Невозможно сохранить. Возможно файл открыт или неправельно выбран формат Excel.")));
            }
            catch (Exception ex) {
                lErrors3.Document.Blocks.Add(new Paragraph(new Run("Произошла ошибка при создании файла. " + ex.ToString())));
            }
        }
예제 #2
0
파일: Form2.cs 프로젝트: anton-mix/Projects
        private void bResiveMessage_Click(object sender, RoutedEventArgs e)
        {
            if (!ValidForm2())
            {
                return;
            }

            SendAlbum album;

            //Формирование списка людей
            try {
                lErrors2.Document.Blocks.Clear();
                album = MyFunctions.GetSendPeoples(tbPathFolder2.Text);
            }
            catch (BadFormatException ex) {
                lErrors2.Document.Blocks.Add(new Paragraph(new Run(ex.Error)));
                return;
            }
            catch (UnknownColumnException ex) {
                lErrors2.Document.Blocks.Add(new Paragraph(new Run("Найден неизвестный столбец")));
                return;
            }
            catch (Exception ex) {
                lErrors2.Document.Blocks.Add(new Paragraph(new Run("Не удалось прочесть файл. Возможно не хватает некоторых полей.")));
                return;
            }

            //Авторатизация в vk
            var api2 = new VkApi();

            if (!api2.IsAuthorized)
            {
                try {
                    api2.Authorize(new ApiAuthParams {
                        ApplicationId = 5539924,
                        //Login = ULogin,
                        //Login = ALogin,

                        //Password = UserPassword,
                        //Password = AntonPassword,

                        AccessToken = "c2d23fe54097f46ea81f8103efd840bc5ca545408c14ee8873c9ee83711d123ca2d5a049f57b774028dfd",
                        Settings    = Settings.All
                    });
                }
                catch (Exception ex) {
                    lErrors2.Document.Blocks.Clear();
                    lErrors2.Document.Blocks.Add(new Paragraph(new Run("Не удалось авторитизироваться в VK. Возможно нет подключения к интеренту. " + ex.ToString())));
                    //lErrors2.Content = "Не удалось авторитизироваться в VK. Возможно нет подключения к интеренту.";
                    return;
                }
            }
            //lErrors2.Content = "";
            //Формирование и отправка сообщений
            for (int i = 0; i < album.Peoples.Count; i++)
            {
                string Message = new TextRange(rtbTextArea.Document.ContentStart, rtbTextArea.Document.ContentEnd).Text;
                Message = Message.Replace("{имя}", album.Peoples[i].Name);

                if (rbChouse1.IsChecked == true)
                {
                    Message = Message.Replace("{стоимость}", album.Peoples[i].AllCost.ToString());
                    Message = Message.Replace("{альбом}", album.Name);
                    string listProduct = "";
                    for (int j = 0; j < album.Peoples[i].Product.Count; j++)
                    {
                        listProduct = listProduct + (j + 1) + ". " + album.Peoples[i].Product[j].Name + ((album.Peoples[i].Product[j].Size == "") ? "" : " " + album.Peoples[i].Product[j].Size + " ") + ": " + album.Peoples[i].Product[j].Count + " шт * " + album.Peoples[i].Product[j].Cost + " руб = " + album.Peoples[i].Product[j].Count * album.Peoples[i].Product[j].Cost + " руб.\n";
                    }
                    Message = Message.Replace("{список}", listProduct);
                }


                long?  captcha_sid = null;
                string captcha_key = null;
                Random prob1       = new Random();

                for (int j = 0; j < 10; j++)
                {
                    try {
                        api2.Messages.Send(new MessagesSendParams {
                            UserId = album.Peoples[i].Id, Message = Message, CaptchaSid = captcha_sid, CaptchaKey = captcha_key, RandomId = prob1.Next()
                        });
                        break;
                    }
                    catch (CaptchaNeededException ex) {
                        CaptchaForm prob = new CaptchaForm(ex.Img, "Сообщение № " + i);
                        prob.ShowDialog();
                        captcha_sid = ex.Sid;
                        captcha_key = prob.CaptchaKey;
                        if (j == 9)
                        {
                            throw new Exception();
                        }
                    }
                    catch (Exception exc) {
                        lErrors2.Document.Blocks.Add(new Paragraph(new Run("Не удалось отправить сообщение покупателю " + album.Peoples[i].Name + ".")));
                        if (exc.Message == "Can't send messages for users without permission")
                        {
                            lErrors2.Document.Blocks.Add(new Paragraph(new Run("У пользователя запрещены сообщения от группы.")));
                        }
                        lErrors2.Document.Blocks.Add(new Paragraph(new Run(exc.ToString())));
                        //lErrors2.Content = lErrors2.Content + "Не удалось отправить сообщение покупателю " + peoples[i].Name + ". " + exc.ToString();
                        break;
                    }
                }
            }


            if (new TextRange(lErrors2.Document.ContentStart, lErrors2.Document.ContentEnd).Text == "")
            {
                lErrors2.Document.Blocks.Clear();
                lErrors2.Document.Blocks.Add(new Paragraph(new Run("Сообщения отправлены.")));
                //lErrors2.Content = "Сообщения отправлены.";
            }
        }