コード例 #1
0
        private async Task <long> TelegramToVkPhoto(PhotoSize photo)
        {
            try
            {
                var uplUrl = await _vkNet.Photo.GetMessagesUploadServerAsync(0);

                var file = await _telegram.GetFileAsync(photo.FileId);

                var fileStream = await _telegram.DownloadFileAsync(file.FilePath) as MemoryStream;

                var requestContent = new MultipartFormDataContent();
                var imageContent   = new ByteArrayContent(fileStream.ToArray());
                imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
                requestContent.Add(imageContent, "photo", "image.jpg");
                var uploadResponse = await _httpClient.PostAsync(uplUrl.UploadUrl, requestContent);

                var uploadRezult = await _vkNet.Photo.SaveMessagesPhotoAsync(await uploadResponse.Content.ReadAsStringAsync());

                return(await _vkNet.Messages.SendAsync(new VkNet.Model.RequestParams.MessagesSendParams
                {
                    UserId = idReceiver,
                    Attachments = uploadRezult
                }));
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
                throw;
            }
        }
コード例 #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jObject = JObject.Load(reader);

            if (!jObject.HasValues) return null;

            var photoSize = new PhotoSize();
            serializer.Populate(jObject.CreateReader(), photoSize);
            return photoSize;
        }
コード例 #3
0
        internal async Task <ModerationResult> doLabelsPhoto(Telegram.Bot.Types.PhotoSize inPhoto)
        {
            string           url = "";
            ModerationResult res = new ModerationResult("");

            try
            {
                // Create a new Cloud Vision client authorized via Application
                // Default Credentials
                VisionService vision = service;
                if (vision == null)
                {
                    MainParams.nlog.Trace("!!!!!!! GOOGLE VISION NULL");
                }
                // Use the client to get label annotations for the given image
                // [START parse_response]
                IList <AnnotateImageResponse> result = await DetectLabelsPhoto(vision, inPhoto);

                // Check if label annotations were found
                if (result != null)
                {
                    MainParams.nlog.Trace("doLabelsPhoto Labels for inPhoto: " + inPhoto.FilePath + "; result count=" + result.Count);
                    // Loop through and output label annotations for the image
                    int    cr   = 0;
                    string json = "";
                    foreach (var response in result)
                    {
                        MainParams.nlog.Trace("response count=" + cr);
                        if (response == null)
                        {
                            MainParams.nlog.Trace("doLabelsPhoto response NULL count=" + cr);
                        }
                        int cl = 0;
                        if (response.LabelAnnotations == null)
                        {
                            MainParams.nlog.Trace("doLabelsPhoto response LabelAnnotations  NULL count=" + cr);
                        }

                        else
                        {
                            foreach (var label in response.LabelAnnotations)
                            {
                                if (label == null)
                                {
                                    MainParams.nlog.Trace("doLabelsPhoto label NULL count=" + cl);
                                }
                                if (label.Description == null)
                                {
                                    MainParams.nlog.Trace("doLabelsPhoto label Description NULL count=" + cl);
                                }
                                if (label.Mid == null)
                                {
                                    MainParams.nlog.Trace("doLabelsPhoto label Mid NULL count=" + cl);
                                }

                                double _score = label.Score.GetValueOrDefault(0);
                                res.AddClassScore(label.Description.Trim(), _score, label.Mid);

                                cl++;
                            }
                        }
                        cr++;
                    }
                    res.json = res.ToStringNoNewline();
                    if (res.json.Length > 499)
                    {
                        res.json = res.json.Substring(0, 499);
                    }
                    MainParams.nlog.Trace(res.ToStringNoNewline());
                }
                else
                {
                    MainParams.nlog.Trace("No labels found.");
                }
            }
            catch (Exception e)
            {
                MainParams.nlog.Debug("***NewLogs; GOOGLE doLabelsPhoto!!! ;EX=;" + e.Message + ";Source=" + e.Source + ";stack=" + e.StackTrace + ";e.inner=" + e.InnerException);
                MainParams.nlog.Debug(e);
            }
            return(res);
        }