Bing Speech API and bots Frameworks - c#

I am trying to use Bing's speech API within the Bot Framework (I am familiarizing myself with both of these technologies). Specifically, I am trying to use the DataClientWithIntent that it supports. I was able to look at this example in GitHub, but unfortunately this is seems to be using DataClient only and I am not able to identify where this is specified. The API is being called in the following manner:
using (var client = new HttpClient())
{
var token = Authentication.Instance.GetAccessToken();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.access_token);
using (var binaryContent = new ByteArrayContent(StreamToBytes(audiostream)))
{
binaryContent.Headers.TryAddWithoutValidation("content-type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
var response = await client.PostAsync(requestUri, binaryContent);
var responseString = await response.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(responseString);
return data.header.name;
}
As you can see a stream is passed in, but unfortunately this only writes back what the user wrote.
I have already developed a test bot that uses a Luis application for what I want, but I want to add the ability for the user to either talk to it or type and achieve the same results. I did find this other example, but this is implementing it directly through the Skype framework, which is something I am not interested in at the moment.
Any ideas, documentation, or clarification would be appreciated.

Related

Is there an gcloud auth application-default print-access-token equivalent in C#?

I am currently working on a .net web app that uses GCP's automl vision model service which I query using RestSharp, the problem is that after some time I start getting Unauthorized responses from the model, I currently fix this by running
gcloud auth application-default print-access-token
In a CMD and copying and pasting the token manually in the request like so:
var request = new RestRequest(Method.POST);
request.AddHeader("authorization", "Bearer " + "I paste it here" );
request.AddJsonBody(new {
payload = new {
image = new
{
imageBytes = base64
}
}
});
Is there any way to do this automatically in C#?
If not, what's the correct approach to authenticating in this model?
You'd be better placed using Google's auth library for .NET to manage the OAuth flow and use of credentials programmatically. You can then drop back to making REST calls against the API.
https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-csharp
Suggestion: create a service account (with appropriate permissions) and use that for auth instead of your user account as you're doing.
It's unfortunate that Google does not appear to provide a Cloud Client Library(/-ies) for AuthML for .NET. You should pester Google Engineering to do this by filing an issue on issuetracker
I was able to bypass this by using this NuGet Package
It is as simple as using this code in case anyone needs it in the future:
GoogleAutoMLVisionClient automlClient = new GoogleAutoMLVisionClient("<My credentials.json>");
ICollection<PredictResult> automlResponses = automlClient.Predict("My endpoint:predict", file.OpenReadStream()).Result.payload;
foreach(PredictResult automlResult in automlResponses)
{
Debug.WriteLine("Name: " + automlResult.displayName + "Score:" + automlResult.classification.score.ToString());
}

Microsoft Translator API work using console but not working within MVC

I have been able to get the Microsoft Translator API to work with creating a console project. I could only find examples with using console projects.
When trying to get the Translator API working within a controller I am not having any luck. I am using the same code.
Do I need to add some other type of reference to get the Translator to work with in MVC?
public async Task<string> GetAuthenticationToken(string key)
{
string endpoint = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "xxxxxxxxxxxxxxx501b7b1ce");
var response = await client.PostAsync(endpoint, null);
var token = await response.Content.ReadAsStringAsync();
return token;
}
}
Error Message
Is there any particular reason you have to create the console application first and then run it as such?
Instead of running the executable from your MVC project, I would recommend calling the TranslatorAsync() function instead.
My guess would be that the process.Start() and process.Close() calls are killing the process before it has a chance to do anything. However, without more specifics on how the function is failing, it's hard to give a better answer.
I had to change
TranslateAsync(productTest, getUserName).Wait;
to
await TranslateAsync(productTest, getUserName);
It’s working now.

Is it possible to create firebase user using RestSharp?

I just knew about the RestSharp and started exploring it.
I could find how to "Log-in" using a user already created doing this:
var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("username", "foo", "password", "bar");
var request = new RestRequest("resource", Method.GET);
client.Execute(request);
But is there a way of actually creating a user in the firebase using the Restshart? Equivilant to this using the Firebase object in JAVA
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.createUser({email:"bobtony#firebase.com",password:"correcthorsebatterystaple"});
I know it is possible to create a node for emails/passwords and manage them manually, but I would like to use the Firebase users auths.
Since the library is called RestSharp, it likely builds on top of the Firebase Legacy REST API. That API does not have functionality to create users.

Facebook Retrive Data using Graph API using c#

i have created desktop Facebook application using c# .net. i want to retrieve users message,post and chat history. which is convenient way to retrieve users all information.i have started with Facebook Graph API but i am not getting any example.
can any one help me ?
A bit late to the party but anyway:
Add a reference to System.Net.Http and Newtonsoft.Json
string userToken = "theusertokentogiveyoumagicalpowers";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://graph.facebook.com");
HttpResponseMessage response = client.GetAsync($"me?fields=name,email&access_token={userToken}").Result;
response.EnsureSuccessStatusCode();
string result = response.Content.ReadAsStringAsync().Result;
var jsonRes = JsonConvert.DeserializeObject<dynamic>(result);
var email = jsonRes["email"].ToString();
}
Go to developer.facebook.com -> Tools & Support -> Select Graph API Explorer
Here U get FQL Query, Access Token
Then write code in C#.....
var client = new FacebookClient();
client.AccessToken = Your Access Token;
//show user's profile picture
dynamic me = client.Get("me?fields=picture");
pictureBoxProfile.Load(me.picture.data.url);
//show user's birthday
me = client.Get("me/?fields=birthday");
labelBirthday.Text = Convert.ToString(me.birthday);
http://www.codeproject.com/Articles/380635/Csharp-Application-Integration-with-Facebook-Twitt
I hope this will help you.!!!
you can check the Graph explorer tool on Developer.facebook.com , go to Tools and select graph explorer, its a nice tool which gives you exact idea about what you can fetch by sending "GET" and "POST" method on FB Graph APis
From what i see the app now only uses webhooks to post data to a data endpoint (in your app) at which point you can parse and use this. (FQL is deprecated). This is used for things like messaging.
A get request can be send to the API to get info - like the amt. of likes on your page.
The docs of FB explain the string you have to send pretty nicely. Sending requests can be done with the webclient, or your own webrequests.
https://msdn.microsoft.com/en-us/library/bay1b5dh(v=vs.110).aspx
Then once you have a string of the JSON formatted page you can parse this using JSON.NET library. It's available as a NUGEt package.

Fetch Twitter Timeline as XML/JSON, show in Windows 8 app

I'm currently following a tutorial on how to show Twitter feeds with LINQ to XML in C#. However, seeing as Microsoft has replaced WebClient with HTTPClient, I'm unsure how to proceed on my quest of integrating this into my Windows 8 application.
The tutorial: http://www.codeproject.com/Articles/117614/How-to-query-twitter-public-status-using-LINQ-to-X
The thing is that I'm "halfway" used to DownloadStringCompleted, which I believe is not in the HTTPClient api. So after searching for a while on the async/await functionality, I mixed some of my knowledge together with some tutorials and snippets I found, but what I ended up with would not work:
public async Task<XElement> GetXmlAsync()
{
var client = new HttpClient();
var response = await client.GetAsync("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=VladimirPutin");
var text = response.Content.ReadAsStringAsync();
return XElement.Parse(text.Result);
XElement xmlTweets = XElement.Parse(text.Result);
listboxTweetsSecond.ItemsSource = from tweet in xmlTweets.Descendants("status")
select new UserTweet
{
UserImageSrc = tweet.Element("user").Element("profile_image_url").Value,
UserMessage = tweet.Element("text").Value,
UserName = tweet.Element("user").Element("screen_name").Value
};
}
(Yeah, the Twitter username is just a placeholder)
I do not need an entire code as an answer, but I'd love it if someone could point out where I should go from here, as well as what I should do in general, as this (of course) does not work (as in, it does not show anything when I run it/debug mode).
Comment out the line
return XElement.Parse(text.Result);
and it should probably work.
Having done that (i.e. you are no longer returning an XElement) you can probably change the method signature top return just a task.
public async Task GetXmlAsync()
{
// method body
}

Categories

Resources