Bing 圖片搜尋服務介接並不困難,主要在標頭放入 Ocp-Apim-Subscription-Key 與加上 q 的搜尋內容即可。注意地方為使用 GET 方法有字數限制,不能查詢過長的文字。如下圖所示,images/search API會回傳一串的資訊給你,我們需要解析並加工這些資訊,才得以回傳我們要的資訊。
Step 1. 開啟 Bot Template 新專案,並開啟 RootDialog.cs,先加上於Azure 啟用 Bing Spell Check 成功後,所記錄下來的 key
private string key = "your_key";
Step 2. 將我們在前一篇文章,透過postman 測試成功回傳的 json 檔案,貼上json2csharp,產生對應的物件,貼在 RootDialog.cs 內:
JSON 檔案
{
"_type": "Images",
"instrumentation": {},
"webSearchUrl": "https://www.bing.com/images/search?q=%E9%9B%AA%E7%B4%8D%E7%91%9E&FORM=OIIARP",
"totalEstimatedMatches": 838,
"nextOffset": 41,
"value": [
{
"webSearchUrl": "https://www.bing.com/images/search?view=detailv2&FORM=OIIRPO&q=%e9%9b%aa%e7%b4%8d%e7%91%9e&id=6CF952F881BAAFA1BD9323CA5E9416A1AFA00F05&simid=607993450671246848",
"name": "Raça Schnauzer - Cachorros Brasil",
"thumbnailUrl": "https://tse1.mm.bing.net/th?id=OIP.6pRgKSB_8dtdqtswcBayJgHaLL&pid=Api",
"datePublished": "2013-03-08T19:00:00.0000000Z",
"contentUrl": "http://www.cachorrosbrasil.com.br/wp-content/uploads/2013/03/schnauzer-07.jpg",
"hostPageUrl": "http://www.cachorrosbrasil.com.br/blog/racas-de-caes/raca-schnauzer/",
"contentSize": "488848 B",
"encodingFormat": "jpeg",
"hostPageDisplayUrl": "www.cachorrosbrasil.com.br/blog/racas-de-caes/raca-schnauzer",
"width": 1500,
"height": 2264,
"thumbnail": {
"width": 474,
"height": 715
},
"imageInsightsToken": "ccid_6pRgKSB/*mid_6CF952F881BAAFA1BD9323CA5E9416A1AFA00F05*simid_607993450671246848*thid_OIP.6pRgKSB\\_8dtdqtswcBayJgHaLL",
"insightsMetadata": {
"pagesIncludingCount": 72,
"availableSizesCount": 20
},
"imageId": "6CF952F881BAAFA1BD9323CA5E9416A1AFA00F05",
"accentColor": "446F16"
}
]
}
public class Instrumentation
{
}
public class Thumbnail
{
public int width { get; set; }
public int height { get; set; }
}
public class InsightsMetadata
{
public int pagesIncludingCount { get; set; }
public int availableSizesCount { get; set; }
}
public class Value
{
public string webSearchUrl { get; set; }
public string name { get; set; }
public string thumbnailUrl { get; set; }
public DateTime datePublished { get; set; }
public string contentUrl { get; set; }
public string hostPageUrl { get; set; }
public string contentSize { get; set; }
public string encodingFormat { get; set; }
public string hostPageDisplayUrl { get; set; }
public int width { get; set; }
public int height { get; set; }
public Thumbnail thumbnail { get; set; }
public string imageInsightsToken { get; set; }
public InsightsMetadata insightsMetadata { get; set; }
public string imageId { get; set; }
public string accentColor { get; set; }
}
public class RootObject
{
public string _type { get; set; }
public Instrumentation instrumentation { get; set; }
public string webSearchUrl { get; set; }
public int totalEstimatedMatches { get; set; }
public int nextOffset { get; set; }
public List<Value> value { get; set; }
}
Step 3. 我們增加一個私有方法,用來產生附件
private static Attachment GetInternetAttachment(string name, string contentType, string url)
{
return new Attachment
{
Name = name,
ContentType = "image/"+contentType,
ContentUrl = url
};
}
Step 4. 修改 MessageReceivedAsync 如下:
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
var client = new RestClient("https://api.cognitive.microsoft.com/bing/v7.0/");
var request = new RestRequest("images/search", Method.GET);
request.AddHeader("Ocp-Apim-Subscription-Key", key);
request.AddParameter("q", activity.Text);
var response = await client.ExecuteTaskAsync<RootObject>(request);
var replyMessage = context.MakeMessage();
List<Attachment> list = new List<Attachment>();
foreach (var item in response.Data.value)
{
Attachment attachment = new Attachment();
attachment = GetInternetAttachment(item.name, item.encodingFormat, item.contentUrl);
list.Add(attachment);
}
replyMessage.Attachments = list;
await context.PostAsync(replyMessage);
context.Wait(MessageReceivedAsync);
}
Step 5. 啟動程式並開啟模擬器,輸入雪納瑞:
https://github.com/matsurigoto/BingImageSearchBotExample.git
如果輸入正妹的話... (被毆