private async void SearchImage(string phraseToSearch) { var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "Key_Bing_Image_Search"); // Request parameters queryString["q"] = phraseToSearch; queryString["count"] = "1"; queryString["offset"] = "0"; queryString["mkt"] = "en-us"; queryString["safeSearch"] = "Moderate"; var uri = "https://api.cognitive.microsoft.com/bing/v5.0/images/search?" + queryString; var response = await client.GetAsync(uri); var json = await response.Content.ReadAsStringAsync(); // MessageBox.Show(json.ToString()); BingImageSearchResponse bingImageSearchResponse = JsonConvert.DeserializeObject <BingImageSearchResponse>(json); var uriSource = new Uri(bingImageSearchResponse.value[0].contentUrl, UriKind.Absolute); await Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Normal, new Action(() => { this.searchImage.Source = new BitmapImage(uriSource); })); await GetEmotion(uriSource.ToString()); }
static async Task <BingImageSearchResponse> MakeRequest(string searchString, int offSet, int numResults) { var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); // Request parameters //../images/search?q=Ferrari&count=10&offset=0&mkt=en-us&safeSearch=Strict queryString["q"] = searchString; queryString["count"] = numResults.ToString("###0"); queryString["offset"] = offSet.ToString("###0"); queryString["mkt"] = "en-us"; queryString["safeSearch"] = "Strict"; var uri = uriBase + "?" + queryString; HttpResponseMessage response; byte[] byteData = Encoding.UTF8.GetBytes(""); string contentString = ""; using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data"); response = await client.PostAsync(uri, content); contentString = await response.Content.ReadAsStringAsync(); } BingImageSearchResponse returnValue = JsonConvert.DeserializeObject <BingImageSearchResponse>(contentString); return(returnValue); }
private static async Task <BingImageSearchResponse> GetImages(string searchString, int numResults, int offSet) { BingImageSearchResponse results = null; results = await SearchImages(searchString, numResults, offSet); return(results); }
public void FindAndDownloadMatchingImages(string searchString, string rootFolder, string prefix, int maxImagesToDownload) { // How many images> BingImageSearchResponse initResults = GetImages(searchString, 1, 0).Result; int estTotalImages = initResults.TotalEstimatedMatches; var msg = String.Format("Est. #Results=" + estTotalImages); int numResultsAtATime = 100; bool keepGettingImages = true; var offSet = 0; var imageFoundCount = 0; var downloadCount = 0; while (keepGettingImages) { BingImageSearchResponse searchResults = GetImages(searchString, numResultsAtATime, offSet).Result; offSet += numResultsAtATime; imageFoundCount += numResultsAtATime; InformationEvent(this, new InformationEventArgs(msg)); var destinationFolder = GetDestinationFolder(rootFolder, searchString); var values = searchResults.Value; foreach (ImageInfo info in values) { downloadCount++; if (downloadCount > maxImagesToDownload) { keepGettingImages = false; break; } var sourceUrl = info.ContentUrl; string fileName = DestinationFileName(info, prefix, destinationFolder, downloadCount); var fdleArgs = new FileDownloadingEventArgs(searchString, sourceUrl, fileName, downloadCount); FileDownloading(this, fdleArgs); DownloadFile(sourceUrl, fileName); } if (imageFoundCount > estTotalImages) { keepGettingImages = false; } } var summaryMsg = String.Format( "Est. #Results={0}\n#Results returned={1}\n", estTotalImages, downloadCount); InformationEvent(this, new InformationEventArgs(summaryMsg)); }
public static async Task <BingImageSearchResponse> SearchImages(string searchString, int numResults, int offSet) { BingImageSearchResponse results = null; //int numResults = 10; //int offSet = 0; int totalReturned = 0; bool lookForMore = true; while (lookForMore) { results = await MakeRequest(searchString, offSet, numResults); int resultsReturned = results.Value.Length; totalReturned += resultsReturned; lookForMore = false; } return(results); }
/* Code controlling the Bot */ //function which is called whenever an "activity" is encountered public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { var connector = new ConnectorClient(new Uri(activity.ServiceUrl)); if (activity.Type == ActivityTypes.Message) { StateClient sc = activity.GetStateClient(); BotData userData = sc.BotState.GetPrivateConversationData(activity.ChannelId, activity.Conversation.Id, activity.From.Id); var boolProfileComplete = userData.GetProperty <bool>("is_done"); if (!boolProfileComplete) { await Conversation.SendAsync(activity, MakeRootDialog); } else { var reply = new Activity(); //configuring the API's components - key, url const string apiKey = "0299fdb1d6d84142a31908ad1b2a45e3"; const string queryUri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment"; var client = new HttpClient { DefaultRequestHeaders = { { "Ocp-Apim-Subscription-Key", apiKey }, { "Accept", "application/json" } } }; //detecting the sentiment in user's answer var sentimentInput = new BatchInput { documents = new List <DocumentInput> { new DocumentInput { id = 1, text = activity.Text, } } }; var json = JsonConvert.SerializeObject(sentimentInput); var sentimentPost = await client.PostAsync(queryUri, new StringContent(json, Encoding.UTF8, "application/json")); var sentimentRawResponse = await sentimentPost.Content.ReadAsStringAsync(); var sentimentJsonResponse = JsonConvert.DeserializeObject <BatchResult>(sentimentRawResponse); var sentimentScore = sentimentJsonResponse?.documents?.FirstOrDefault()?.score ?? 0; //updating the sentimentScore depending upon the expected sentiment sentimentScore = Global.updateSentiment(sentimentScore); //sending the bot's response depending on the detected sentiment Global.total_score += sentimentScore; string message = Global.GetBotResponse(sentimentScore); reply = activity.CreateReply(message); await connector.Conversations.ReplyToActivityAsync(reply); // DETECTING KEYPHRASES IF SENTIMENT-SCORE IS LOWER THAN NORMAL // if (sentimentScore < 0.3) { //configuring keyPhrase Detection API's components - key, url const string kpd_queryUri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases"; var kpd_client = new HttpClient { DefaultRequestHeaders = { { "Ocp-Apim-Subscription-Key", apiKey }, { "Accept", "application/json" } } }; var keyphraseInput = new KPD_Input { documents = new List <KPD_DocumentInput> { new KPD_DocumentInput { language = "en", id = 1, text = activity.Text, } } }; // sending activity.Text for analysis var kpd_json = JsonConvert.SerializeObject(keyphraseInput); var kpd_Post = await kpd_client.PostAsync(kpd_queryUri, new StringContent(kpd_json, Encoding.UTF8, "application/json")); var kpd_RawResponse = await kpd_Post.Content.ReadAsStringAsync(); KPD_Result kpd_res = JsonConvert.DeserializeObject <KPD_Result>(kpd_RawResponse); // storing keyPhrases of a given activity in a List List <string> phrases = new List <string>(); for (int i = 0; i < kpd_res.documents.Count; i++) { for (int j = 0; j < kpd_res.documents[i].keyPhrases.Count; j++) { phrases.Add(kpd_res.documents[i].keyPhrases[j]); } } // search for jokes and one-liners based on detected keyphrases if (phrases[0] != "") { // storing above List in the Global List Global.KeyPhrases.Add(phrases); reply = activity.CreateReply(string.Format("I perceive that you are sad due to \'{0}\'.", string.Join("\', \'", phrases.ToArray()))); await connector.Conversations.ReplyToActivityAsync(reply); reply = activity.CreateReply("Time to cheer up! :)"); await connector.Conversations.ReplyToActivityAsync(reply); //configuring BingWebSearch and BingImageSearch API - key, url, url_parameters const string Key = "0cd4df3677d64499b05eda5a4912510a"; List <string> web_url = new List <string>(); List <string> surl = new List <string>(); // DISPLAYING JOKES (MAX. 5) List <Value> websearchResult = new List <Value>(); for (int i = 0; i < Math.Min(5, phrases.Count); i++) { web_url.Add("https://api.cognitive.microsoft.com/bing/v5.0/search?q=jokes+on+" + phrases.ToArray()[i] + "&count=" + ((int)Math.Floor(5.0 / Math.Min(5, phrases.Count))).ToString() + "&safeSearch=Strict"); } for (int i = 0; i < Math.Min(3, phrases.Count); i++) { HttpClient client1 = new HttpClient(); client1.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Key); //authentication header to pass the API key client1.DefaultRequestHeaders.Add("Accept", "application/json"); string bingRawResponse = null; RootObject bingJsonResponse = null; bingRawResponse = await client1.GetStringAsync(web_url.ToArray()[i]); bingJsonResponse = JsonConvert.DeserializeObject <RootObject>(bingRawResponse); websearchResult.AddRange(bingJsonResponse.webPages.value); } if (websearchResult.Count == 0) { //added code to handle the case where results are null or zero reply = activity.CreateReply("Sorry, no jokes today."); await connector.Conversations.ReplyToActivityAsync(reply); } else { // create replies with joke_links string[] number_response = { "The first link to some hilarious jokes -", "Here's the second one -", "Have some more - the third link -", "I think the fourth one would be better -", "And finally, the last one -" }; reply = activity.CreateReply("Read these jokes to lighten a bit."); await connector.Conversations.ReplyToActivityAsync(reply); for (int i = 0; i < websearchResult.Count; i++) { await connector.Conversations.ReplyToActivityAsync(activity.CreateReply(number_response[i])); string joke_link = ""; if (websearchResult[i].displayUrl.Contains("http") == false) { joke_link = websearchResult[i].name + "\n\n" + "http://" + websearchResult[i].displayUrl + "\n\n" + websearchResult[i].snippet; } else { joke_link = websearchResult[i].name + "\n\n" + websearchResult[i].displayUrl + "\n\n" + websearchResult[i].snippet; } //Reply to user message with image attachment await connector.Conversations.ReplyToActivityAsync(activity.CreateReply(joke_link)); } } // DISPLAYING ONE-LINERS/MEMES aka SORROW-BUSTERS AS IMAGES (MAX. 3) List <ImageResult> imageResult = new List <ImageResult>(); for (int i = 0; i < Math.Min(3, phrases.Count); i++) { surl.Add("https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=" + phrases.ToArray()[i] + "+funny+memes&offset=5&count=1&safeSearch=Strict"); } for (int i = 0; i < Math.Min(3, phrases.Count); i++) { HttpClient client1 = new HttpClient(); client1.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Key); //authentication header to pass the API key client1.DefaultRequestHeaders.Add("Accept", "application/json"); string bingRawResponse = null; BingImageSearchResponse bingJsonResponse = null; bingRawResponse = await client1.GetStringAsync(surl.ToArray()[i]); bingJsonResponse = JsonConvert.DeserializeObject <BingImageSearchResponse>(bingRawResponse); imageResult.AddRange(bingJsonResponse.value); } if (imageResult.Count == 0) { //added code to handle the case where results are null or zero reply = activity.CreateReply("It seems that I have run out of sorrow-busters..."); await connector.Conversations.ReplyToActivityAsync(reply); } else { // create a reply with one-liner images as attachments string[] number_response = { "Here's the first one", "Then the second...", "And finally the third." }; reply = activity.CreateReply("Some sorrow-busters."); await connector.Conversations.ReplyToActivityAsync(reply); for (int i = 0; i < imageResult.Count; i++) { string Result = imageResult.ToArray()[i].contentUrl; var replyMessage = activity.CreateReply(); replyMessage.Recipient = activity.From; replyMessage.Type = ActivityTypes.Message; replyMessage.Text = number_response[i]; replyMessage.Attachments = new List <Attachment>(); replyMessage.Attachments.Add(new Attachment() { ContentUrl = Result, ContentType = "image/png" }); //Reply to user message with image attachment await connector.Conversations.ReplyToActivityAsync(replyMessage); } } } } ////////////////////////////////////////////////////////////////////////////////////////////// //asking a random question from the database to the user reply = activity.CreateReply(Global.PopQuestion()); await connector.Conversations.ReplyToActivityAsync(reply); //terminating case - when all questions have been asked if (Global.questions[0] == "done") { //averaging the total score Global.total_score = Global.total_score / Global.q_length; //assigning "number of responses" value based on final score int n_response = Global.GetNResponse(Global.total_score); reply = activity.CreateReply("Here's something to cheer you up..." + "\nPlease do have a look!"); await connector.Conversations.ReplyToActivityAsync(reply); // DISPLAYING INSPIRATIONAL QUOTES, NUMBER DEPENDING UPON THE RESPONSE //configuring BingImageSearch API - key, url, url_parameters const string Key = "0cd4df3677d64499b05eda5a4912510a"; string surl = "https://api.cognitive.microsoft.com/bing/v5.0/images/search" + "?q=motivational quotes&offset=" + ((n_response + 1) * 5).ToString() + "&count=" + (n_response * 2).ToString() + "&safeSearch=Moderate"; HttpClient client1 = new HttpClient(); client1.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Key); //authentication header to pass the API key client1.DefaultRequestHeaders.Add("Accept", "application/json"); string bingRawResponse = null; BingImageSearchResponse bingJsonResponse = null; bingRawResponse = await client1.GetStringAsync(surl); bingJsonResponse = JsonConvert.DeserializeObject <BingImageSearchResponse>(bingRawResponse); ImageResult[] imageResult = bingJsonResponse.value; if (imageResult == null || imageResult.Length == 0) { //added code to handle the case where results are null or zero reply = activity.CreateReply("Sorry...could not find any image-quote."); await connector.Conversations.ReplyToActivityAsync(reply); } else { reply = activity.CreateReply("Have a look at some motivational quotes!"); await connector.Conversations.ReplyToActivityAsync(reply); // create a reply with image-quotes as attachments var replyMessage = activity.CreateReply(); replyMessage.Recipient = activity.From; replyMessage.Type = ActivityTypes.Message; replyMessage.Text = "Hope you like them! Remain motivated!"; replyMessage.Attachments = new List <Attachment>(); for (int i = 0; i < n_response * 2; i++) { string Result = imageResult[i].contentUrl; replyMessage.Attachments.Add(new Attachment() { ContentUrl = Result, ContentType = "image/png" }); } //Reply to user message with image attachment await connector.Conversations.ReplyToActivityAsync(replyMessage); } reply = activity.CreateReply("Keep sharing your experiences :) !!!"); await connector.Conversations.ReplyToActivityAsync(reply); reply = activity.CreateReply("Bye! Meet you later!"); await connector.Conversations.ReplyToActivityAsync(reply); Environment.Exit(0); } } } else { //to handle events other than messages await connector.Conversations.ReplyToActivityAsync(HandleSystemMessage(activity)); } var response1 = Request.CreateResponse(HttpStatusCode.OK); return(response1); }