I have an Azure bot which is capable of getting a wav audio file and translating to text using the Bing Speech API. I am trying to hook this up to Skype. I can't work out how to get a stream from the attachment. The first part of my code finds the Skype attachment with ContentType='audio' ok:
public async Task<string> GetText(IMessageActivity messageActivity)
{
var connector = new ConnectorClient(new Uri(messageActivity.ServiceUrl));
try
{
var skypeAudioAttachment = messageActivity.Attachments?.FirstOrDefault(sa => sa.ContentType.Equals("audio"));
if (skypeAudioAttachment != null)
{
var stream = await GetAudioStream(connector, skypeAudioAttachment);
But then I'm trying to use the code below (taken from the Controller\MessagesController.cs file of the Microsoft BotBuilder sample here: https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/intelligence-SpeechToText) to get a Stream:
private static async Task<Stream> GetAudioStream(ConnectorClient connector, Attachment audioAttachment)
{
using (var httpClient = new HttpClient())
{
// The Skype attachment URLs are secured by JwtToken,
// you should set the JwtToken of your bot as the authorization header for the GET request your bot initiates to fetch the image.
// https://github.com/Microsoft/BotBuilder/issues/662
var uri = new Uri(audioAttachment.ContentUrl);
if (uri.Host.EndsWith("skype.com") && uri.Scheme == "https")
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await GetTokenAsync(connector));
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
}
return await httpClient.GetStreamAsync(uri);
}
}
private static async Task<string> GetTokenAsync(ConnectorClient connector)
{
var credentials = connector.Credentials as MicrosoftAppCredentials;
if (credentials != null)
{
return await credentials.GetTokenAsync();
}
return null;
}
This fails, I think because I can't access the url. Has anyone got an example of how to get hold of the Skype audio stream or know what I should do?
The ContentUrl is of the form: https://smba.trafficmanager.net/apis/v3/attachments/LONG_ID_GOES_HERE/views/original. The error I get back from the httpClient.GetStreamAsync request is "Response status code does not indicate success: 401 (Unauthorized)."
Thank you