public string GetImageURL(ImageResult imageResult) { string imageUrl = string.Empty; if (imageResult != null && imageResult.query.pages != null && imageResult.query.pages.Count > 0 && imageResult.query.pages[0].thumbnail != null) { imageUrl = imageResult.query.pages[0].thumbnail.source; } else { // no image so use default Wikipedia image imageUrl = "https://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png"; } return(imageUrl); }
public async Task <ComposeExtensionResponse> GetComposeExtensionResponse(Activity activity, IBotDataStore <BotData> botDataStore) { ComposeExtensionResponse composeExtensionResponse = null; ImageResult imageResult = null; List <ComposeExtensionAttachment> composeExtensionAttachments = new List <ComposeExtensionAttachment>(); var userData = await TemplateUtility.GetBotUserDataObject(botDataStore, activity); var userPreferredCardType = userData.GetProperty <string>(Strings.ComposeExtensionCardTypeKeyword); bool isSettingUrl = false; var composeExtensionQuery = activity.GetComposeExtensionQueryData(); if (string.Equals(activity.Name.ToLower(), Strings.ComposeExtensionQuerySettingUrl)) { isSettingUrl = true; } if (composeExtensionQuery.CommandId == null || composeExtensionQuery.Parameters == null) { return(null); } var initialRunParameter = GetQueryParameterByName(composeExtensionQuery, Strings.manifestInitialRun); var queryParameter = GetQueryParameterByName(composeExtensionQuery, Strings.manifestParameterName); if (userData == null) { composeExtensionResponse = new ComposeExtensionResponse(); string message = Strings.ComposeExtensionNoUserData; composeExtensionResponse.ComposeExtension = GetMessageResponseResult(message); return(composeExtensionResponse); } /** * Below are the checks for various states that may occur * Note that the order of many of these blocks of code do matter */ // situation where the incoming payload was received from the config popup if (!string.IsNullOrEmpty(composeExtensionQuery.State)) { ParseSettingsAndSave(composeExtensionQuery.State, userData, activity, botDataStore); /** * //// need to keep going to return a response so do not return here * //// these variables are changed so if the word 'setting' kicked off the compose extension, * //// then the word setting will not retrigger the config experience **/ queryParameter = ""; initialRunParameter = "true"; } // this is a sitaution where the user's preferences have not been set up yet if (string.IsNullOrEmpty(userData.GetProperty <string>(Strings.ComposeExtensionCardTypeKeyword))) { composeExtensionResponse = GetConfig(); return(composeExtensionResponse); } /** * // this is the situation where the user has entered the word 'reset' and wants * // to clear his/her settings * // resetKeyword for English is "reset" **/ if (string.Equals(queryParameter.ToLower(), Strings.ComposeExtensionResetKeyword)) { userData.RemoveProperty(Strings.ComposeExtensionCardTypeKeyword); await TemplateUtility.SaveBotUserDataObject(botDataStore, activity, userData); composeExtensionResponse = new ComposeExtensionResponse(); composeExtensionResponse.ComposeExtension = GetMessageResponseResult(Strings.ComposeExtensionResetText); return(composeExtensionResponse); } /** * // this is the situation where the user has entered "setting" or "settings" in order * // to repromt the config experience * // keywords for English are "setting" and "settings" **/ if ((string.Equals(queryParameter.ToLower(), Strings.ComposeExtensionSettingKeyword) || string.Equals(queryParameter.ToLower(), Strings.ComposeExtensionSettingsKeyword)) || (isSettingUrl)) { composeExtensionResponse = GetConfig(); return(composeExtensionResponse); } /** * // this is the situation where the user in on the initial run of the compose extension * // e.g. when the user first goes to the compose extension and the search bar is still blank * // in order to get the compose extension to run the initial run, the setting "initialRun": true * // must be set in the manifest for the compose extension **/ if (initialRunParameter == "true") { //Signin Experience, please uncomment below code for Signin Experience //composeExtensionResponse = GetSignin(composeExtensionResponse); //return composeExtensionResponse; composeExtensionResponse = new ComposeExtensionResponse(); var historySearchWikiResult = userData.GetProperty <List <WikiHelperSearchResult> >(ComposeExtensionSelectedResultsKey); if (historySearchWikiResult != null) { foreach (var searchResult in historySearchWikiResult) { WikiHelperSearchResult wikiSearchResult = new WikiHelperSearchResult(searchResult.imageUrl, searchResult.highlightedTitle, searchResult.text); // create the card itself and the preview card based upon the information var createdCardAttachment = TemplateUtility.CreateComposeExtensionCardsAttachments(wikiSearchResult, userPreferredCardType); composeExtensionAttachments.Add(createdCardAttachment); } composeExtensionResponse = GetComposeExtensionQueryResult(composeExtensionAttachments); } else { composeExtensionResponse.ComposeExtension = GetMessageResponseResult(Strings.ComposeExtensionInitialRunText); } return(composeExtensionResponse); } /** * Below here is simply the logic to call the Wikipedia API and create the response for * a query; the general flow is to call the Wikipedia API for the query and then call the * Wikipedia API for each entry for the query to see if that entry has an image; in order * to get the asynchronous sections handled, an array of Promises for cards is used; each * Promise is resolved when it is discovered if an image exists for that entry; once all * of the Promises are resolved, the response is sent back to Teams */ WikiResult wikiResult = await SearchWiki(queryParameter, composeExtensionQuery); // enumerate search results and build Promises for cards for response foreach (var searchResult in wikiResult.query.search) { //Get the Image result on the basis of Image Title one by one imageResult = await SearchWikiImage(searchResult); //Get the Image Url from imageResult string imageUrl = GetImageURL(imageResult); string cardText = searchResult.snippet + " ..."; WikiHelperSearchResult wikiSearchResult = new WikiHelperSearchResult(imageUrl, searchResult.title, cardText); // create the card itself and the preview card based upon the information var createdCardAttachment = TemplateUtility.CreateComposeExtensionCardsAttachments(wikiSearchResult, userPreferredCardType); composeExtensionAttachments.Add(createdCardAttachment); } composeExtensionResponse = GetComposeExtensionQueryResult(composeExtensionAttachments); return(composeExtensionResponse); }