這一篇使用 Bing Spell Check API 流程比較簡易,只需要組合相關參數、放入Key 與 Content-Type ,呼叫 SpellCheck ,在解析回傳的內容即可。如下圖所示:
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": "SpellCheck",
"flaggedTokens": [
{
"offset": 5,
"token": "its",
"type": "UnknownToken",
"suggestions": [
{
"suggestion": "it's",
"score": 0.817378790203206
}
]
}
]
}
public class Suggestion
{
public string suggestion { get; set; }
public double score { get; set; }
}
public class FlaggedToken
{
public int offset { get; set; }
public string token { get; set; }
public string type { get; set; }
public List<Suggestion> suggestions { get; set; }
}
public class RootObject
{
public string _type { get; set; }
public List<FlaggedToken> flaggedTokens { get; set; }
}
Step 3. 我們將 MessageReceivedAsync 方法更改如下:分別在header 加入Ocp-Apim-Subscription-Key 與 Content-Type,將 activity.Text 加入傳遞參數內。
註:Response 內容可能因為錯誤內容不同,請製作些防呆措施。
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("spellcheck?mode=proof&mkt=en-us", Method.POST);
request.AddHeader("Ocp-Apim-Subscription-Key", key);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("text", activity.Text);
var response = await client.ExecuteTaskAsync<RootObject>(request);
var suggestion = string.Empty;
foreach(var item in response.Data.flaggedTokens[0].suggestions)
{
suggestion += item.suggestion + " ";
}
await context.PostAsync($"{suggestion}");
context.Wait(MessageReceivedAsync);
}
Step 4. 你的 RootDialog.cs 應該長的如下:
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using RestSharp;
using System.Collections.Generic;
namespace BingSpellCheckBotApplication.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private string key = "your_key";
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("spellcheck?mode=proof&mkt=en-us", Method.POST);
request.AddHeader("Ocp-Apim-Subscription-Key", key);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("text", activity.Text);
var response = await client.ExecuteTaskAsync<RootObject>(request);
var suggestion = string.Empty;
foreach(var item in response.Data.flaggedTokens[0].suggestions)
{
suggestion += item.suggestion + " ";
}
await context.PostAsync($"{suggestion}");
context.Wait(MessageReceivedAsync);
}
}
public class Suggestion
{
public string suggestion { get; set; }
public double score { get; set; }
}
public class FlaggedToken
{
public int offset { get; set; }
public string token { get; set; }
public string type { get; set; }
public List<Suggestion> suggestions { get; set; }
}
public class RootObject
{
public string _type { get; set; }
public List<FlaggedToken> flaggedTokens { get; set; }
}
}
Step 5. 開啟模擬器進行測試,成功
https://github.com/matsurigoto/BingSpellCheckBotExample.git
終於到20篇惹,文章與程式地獄阿