Reputation: 2223
A client is using IBM's Watson Dialog service and I cannot find any examples of someone doing even the most basic stuff using .Net (c# specifically).
IBM only shows examples using Curl, Node, and Java...
My first goal is to be able to add a new xml file (dialog tree) to the watson service. Seems simple enough, but I have been beating my head for a while now.
Upvotes: 1
Views: 609
Reputation: 2223
So I finally got this working by cobbling together the help of about a dozen google searches on related topics. Thought I would post the working version here.
Below is my code which will upload an xml file to the Watson Dialog service using C# in a MVC controller.
The front end is a form that takes a friendly name (which I turn into a .xml file name), and an upload of the file itself (using dropzone).
I am sure there could be optimizations, but I hope this helps someone. The good news is, that this can be used as a foundation to do just about any Watson Dialog service call (add, update, delete).
public ContentResult Save(FormCollection form)
{
try
{
var name = form["txtDialogName"];
var filename = name + ".xml";
byte[] bytes = null;
foreach (string s in Request.Files)
{
var file = Request.Files[s];
using (Stream inputStream = file.InputStream)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
bytes = memoryStream.ToArray();
}
break;
}
if (bytes == null)
{
var contentResult = new ContentResult
{
ContentType = "application/json",
Content = null
};
return contentResult;
}
var basePath = ConfigurationManager.AppSettings["WatsonPath"];
var username = ConfigurationManager.AppSettings["WatsonUsername"];
var password = ConfigurationManager.AppSettings["WatsonPassword"];
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password)));
var values = new[]
{ new KeyValuePair<string, string>("name", filename) };
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
foreach (var keyValuePair in values)
{
formData.Add(new StringContent(keyValuePair.Value), string.Format("\"{0}\"", keyValuePair.Key));
}
formData.Add(new ByteArrayContent(bytes),
'"' + "file" + '"',
'"' + filename + '"');
var response = client.PostAsync(basePath + "/v1/dialogs", formData).Result;
var result = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode)
{
var contentResult = new ContentResult
{
ContentType = "application/json",
Content = response.ReasonPhrase
};
return contentResult;
}
var successResult = new ContentResult
{
ContentType = "application/json",
Content = result
};
return successResult;
}
}
catch (Exception ex)
{
HandleError(ex);
var contentResult = new ContentResult
{
ContentType = "application/json",
Content = "false"
};
return contentResult;
}
}
Upvotes: 3