/// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                var    connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                string message;

                try
                {
                    var audioAttachment = activity.Attachments?.FirstOrDefault(a => a.ContentType.Equals("audio/wav") || a.ContentType.Equals("application/octet-stream"));
                    if (audioAttachment != null)
                    {
                        var stream = await GetAudioStream(connector, audioAttachment);

                        var text = await MicrosoftCognitiveSpeechService.GetInstance().GetTextFromAudioAsync(stream);

                        message = ProcessText(text);
                    }
                    else
                    {
                        message = SpringBottyConstants.NOT_AUDIBLE_FILE;
                    }
                }
                catch (Exception e)
                {
                    message = SpringBottyConstants.AUDIBLE_FILE_ERROR;
                    if (e is HttpException)
                    {
                        var httpCode = (e as HttpException).GetHttpCode();
                        if (httpCode == 401 || httpCode == 403)
                        {
                            message += $" [{e.Message} - tip: Revisa el API KEY de tu proyecto]";
                        }
                        else if (httpCode == 408)
                        {
                            message += $" [{e.Message} - tip: Intentemos con un audio más corto]";
                        }
                    }
                    Trace.TraceError(e.ToString());
                }

                Activity reply = activity.CreateReply(message);
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            else
            {
                await HandleSystemMessageService.GetInstance().HandleSystemMessage(activity);
            }

            var response = this.Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }
Beispiel #2
0
using Autofac;
using BotSession;
using HajjBot;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Connector;
using HajjBot.Helper;
using SpeechToText.Services;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace HajjBot
{
    [BotAuthentication]
    public class MessagesController : ApiController
    {
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        /// 
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            Common.CommonConversation.CurrentActivity = activity;
            Common.CommonConversation.Connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            if (activity.Type == ActivityTypes.Message)
            {
              
                    var oggAudioAttachment = activity.Attachments?.FirstOrDefault(a => a.ContentType.Equals("audio/ogg") || a.ContentType.Equals("application/octet-stream"));

                    if (oggAudioAttachment != null)
                    {
                        var connector = Common.CommonConversation.Connector;
                        MicrosoftCognitiveSpeechService speechService = new MicrosoftCognitiveSpeechService();

                        var stream = await new HajjHelper().GetAudioStream(connector, oggAudioAttachment);

                        string text = await Task.Factory.StartNew(
                            async () => await speechService.GetTextFromAudioAsync(stream)).Result;


                        Activity reply = activity.CreateReply(
                            $"Did you say?! ...    {text}");

                        await connector.Conversations.ReplyToActivityAsync(reply);

                        text = text.Replace(".", string.Empty).Trim();

                        activity.Text = text;
                    }

                    var dictionary = SessionTimeouter.Dictionary;

                    var key = activity.ChannelId + activity.From.Id + activity.Conversation.Id;

                    SessionTimeouter timeouter = null;

                    int millisecondsTime = 1000 * 60 * 5;

                    if (dictionary.ContainsKey(key))
                    {
                        timeouter = dictionary[key];
                        timeouter.Reset();
                    }
                    else
                    {
                        timeouter = new SessionTimeouter(key, activity, millisecondsTime);
                        timeouter.SetNewSession(millisecondsTime);
                        dictionary.Add(key, timeouter);
                    }

                    string msg = activity.Text.ToLower().Trim();
                    if (msg == "start over" || msg == "exit" || msg == "quit" || msg == "done" || msg == "start again" || msg == "restart" || msg == "leave" || msg == "reset")
                    {
                        await new HajjHelper().Reset(activity);
                    }
                    else
                    {

                        //await Conversation.SendAsync(
                        //                               activity, () => new Dialogs.HajjEnglishLuisDialog().DefaultIfException()
                        //                            );

                        await Task.Factory.StartNew(async () => await Conversation.SendAsync(activity, () => new Dialogs.HajjEnglishLuisDialog().DefaultIfException()));

                        //await Task.Factory.StartNew(async () => await Conversation.SendAsync(activity, MakeDeviceOrderDialog));


                    }
               


            }
            else
            {
                await HandleSystemMessageAsync(activity);
            }


            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

        private ConversationStarter GetConversationStarter(Activity message)
        {
            ConversationStarter cs = new ConversationStarter