コード例 #1
0
ファイル: MessagesCore.cs プロジェクト: Hamikadze/VkBotApi
        public static void VkSendImageMessage(string accessToken, RMessagesData.RCmdsListCollect.RCommandsItem rCommandItem)
        {
            try
            {
                int peerId = rCommandItem.PeerId;
                ThreadCore.UploadingPhotos = true;
                var url        = SiteUtils.GetMessageAttachmentUrl(accessToken);
                var attachment = SiteUtils.GetImageByQuery(accessToken, rCommandItem);
                if (attachment == null)
                {
                    string message = $"Изображение по запросу {rCommandItem.Message} не найдено!";
                    SiteUtils.SendMessage(accessToken, rCommandItem, message);
                    LogCore.Log(message, JsonConvert.SerializeObject(rCommandItem),
                                EnumData.LogTypeCommand.CMessage, EnumData.LogSourceCommand.VK);
                    return;
                }
                if (attachment.Size() == new Size(0, 0))
                {
                    LogCore.Log("Смена ключей Google", JsonConvert.SerializeObject(rCommandItem),
                                EnumData.LogTypeCommand.CMessage, EnumData.LogSourceCommand.VK);
                    return;
                }

                SiteUtils.MessagesSetActivity(accessToken, peerId);

                var photos  = SiteUtils.UploadPhotoToServerAsync(url, attachment);
                var photoId = SiteUtils.SaveMessagePhoto(accessToken, photos);

                //SiteUtils.MessagesSetActivity(accessToken, peerId);

                SiteUtils.SendMessage(accessToken, rCommandItem, "",
                                      $"photo{photoId}");
                ThreadCore.UploadingPhotos = false;
                LogCore.Log(rCommandItem.Message, JsonConvert.SerializeObject(rCommandItem),
                            EnumData.LogTypeCommand.CMessage, EnumData.LogSourceCommand.VK);
            }
            catch (Exception ex)
            {
                ex.Error();
            }
        }
コード例 #2
0
ファイル: MessagesCore.cs プロジェクト: Hamikadze/VkBotApi
        public static void VkSendAnonymousMessage(string accessToken, RMessagesData.RCmdsListCollect.RCommandsItem rCommandItem)
        {
            try
            {
                if (rCommandItem.Message.IndexOf(" ", StringComparison.OrdinalIgnoreCase) < 0)
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, "Ошибка в распозновании названия группы!");
                    return;
                }

                Regex r         = new Regex(@"\s+");
                var   groupData = DataStorage.DialogsList.Find(
                    i => r.Replace(i.Title, @" ").Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .All(j => r.Replace(rCommandItem.Message, @" ").IndexOf(j, StringComparison.OrdinalIgnoreCase) >= 0));

                if (string.IsNullOrWhiteSpace(groupData?.Title))
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, "Ошибка в распозновании названия группы!");
                    return;
                }
                rCommandItem.Message =
                    Regex.Replace(rCommandItem.Message, groupData.Title, "", RegexOptions.IgnoreCase).Trim();

                if (groupData.ChatId == 0)
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, "Ошибка в распозновании идентификатора группы!");
                    return;
                }

                if (DataStorage.LastAnonymousMessage.ContainsKey(rCommandItem.FromId))
                {
                    if ((DateTime.Now - DataStorage.LastAnonymousMessage[rCommandItem.FromId]).TotalSeconds < 30)
                    {
                        SiteUtils.SendMessage(accessToken, rCommandItem, "Анонимные сообщения можно отправлять ТОЛЬКО один раз в 30 секунд!");
                        //return;
                    }
                }
                else
                {
                    DataStorage.LastAnonymousMessage.Add(rCommandItem.FromId, DateTime.Now);
                }

                var photoId = string.Empty;
                if (rCommandItem?.Attachments != null && rCommandItem.Attachments.Any(i => i.Photo != null))
                {
                    int peerId = rCommandItem.PeerId;
                    ThreadCore.UploadingPhotos = true;
                    var url = SiteUtils.GetMessageAttachmentUrl(accessToken);
                    SiteUtils.MessagesSetActivity(accessToken, peerId);
                    var    attachments = rCommandItem?.Attachments?.Where(i => i.Photo != null);
                    var    attachment  = attachments.First();
                    string imageLink   = attachment.Photo.Sizes.First(i => i.Width * i.Height == attachment.Photo.Sizes.Max(size2 => size2.Height * size2.Width)).Url;
                    var    imagePath   = $"{FileCore.PathImages}\\{imageLink.GetDeterministicHashCode()}.png";
                    using (WebClient wc = new WebClient())
                    {
                        wc.DownloadFile(new Uri(imageLink), imagePath);
                    }
                    var image = Image.Load <Rgba32>(imagePath);
                    if (image.Height > 720)
                    {
                        int height = 720;
                        int width  = (int)(720 / (double)image.Height * image.Width);
                        image.Mutate(i => i.Resize(width, height));
                    }
                    var photos = SiteUtils.UploadPhotoToServerAsync(url, image);
                    photoId = SiteUtils.SaveMessagePhoto(accessToken, photos);

                    SiteUtils.MessagesSetActivity(accessToken, peerId);
                    ThreadCore.UploadingPhotos = false;
                }

                rCommandItem.PeerId = groupData.ChatId;

                var message = $"[АНОНИМНОЕ СООБЩЕНИЕ]"
                              +
                              "\n"
                              +
                              rCommandItem.Message
                              .Substring(rCommandItem.Message.LastIndexOf("*", StringComparison.OrdinalIgnoreCase) +
                                         1).Trim();
                SiteUtils.MakeAsReadMessage(accessToken, rCommandItem);
                rCommandItem.Id = -1;
                DataStorage.LastAnonymousMessage[rCommandItem.FromId] = DateTime.Now;

                if (string.IsNullOrWhiteSpace(photoId))
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, message);
                }
                else
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, message,
                                          $"photo{photoId}");
                }
                LogCore.Log(rCommandItem.Message, JsonConvert.SerializeObject(rCommandItem), EnumData.LogTypeCommand.CMessage, EnumData.LogSourceCommand.VK);
            }
            catch (Exception ex)
            {
                ex.Error();
            }
        }