BotFramework Skype语音到文本

时间:2017-11-20 14:10:02

标签: c# speech-recognition botframework skype

申请目的:与Skype Bot交谈 - 用文本转换语音

CallingController -

public CallingController() : base()
    {
        CallingConversation.RegisterCallingBot(callingBotService => new IVRBot(callingBotService));
    }

    [Route("callback")]
    public async Task<HttpResponseMessage> ProcessCallingEventAsync()
    {
        return await CallingConversation.SendAsync(this.Request, CallRequestType.CallingEvent);
    }

    [Route("call")]
    public async Task<HttpResponseMessage> ProcessIncomingCallAsync()
    {
        return await CallingConversation.SendAsync(this.Request, CallRequestType.IncomingCall);
    }

MessageController -

 public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {           
        if (activity.Type == ActivityTypes.Message)
        {
            TextOrSpeech tos = new TextOrSpeech();
            await tos.ToSAsync(activity);
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

    private Activity HandleSystemMessage(Activity activity)
    {
        if (activity.Type == ActivityTypes.ConversationUpdate)
        {
            IConversationUpdateActivity iConversationUpdated = activity as IConversationUpdateActivity;
            if (iConversationUpdated != null)
            {
                ConnectorClient connector = new ConnectorClient(new System.Uri(activity.ServiceUrl));
                foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
                {
                    if (member.Id == iConversationUpdated.Recipient.Id)
                    {
                        var reply = ((Activity)iConversationUpdated).CreateReply($"Hello, how can i help you?");
                        connector.Conversations.ReplyToActivity(reply);
                    }
                }
            }
        }
        return null;
    }       

我想复制Botframework行为:按下说话按钮与机器人对话转换我的语音文本自动发送到机器人(不按回车等)并根据我的问题得到回复。

ToS类句柄接收活动,如果活动是音频,则将其转换为文本。我想在Skype上模仿这种行为。

 public ConvertToText(Activity activity)
    {
        audioAttachment = activity.Attachments?.FirstOrDefault(a => a.ContentType.Equals("audio/wav") || a.ContentType.Equals("application/octet-stream"));
        activity = this.activity;
    }

    public async System.Threading.Tasks.Task ConvertAsync()
    {
        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
        var stream = await AudioStream.GetAudioStream(connector, audioAttachment);
        var text = await speechService.GetTextFromAudioAsync(stream);
        Activity reply = activity.CreateReply(text);
        await connector.Conversations.ReplyToActivityAsync(reply);
    }

我真的不明白CallingController是如何工作的,它调用了IVRBOT,我收到了欢迎消息,然后关闭了对话。

public class IVRBot : IDisposable, ICallingBot
{

    private readonly Dictionary<string, CallState> callStateMap = new Dictionary<string, CallState>();
    private readonly MicrosoftSpeech.MicrosoftCognitiveSpeechService speechService = new MicrosoftSpeech.MicrosoftCognitiveSpeechService();
    public ICallingBotService CallingBotService { get; }

    public IVRBot(ICallingBotService callingBotService)
    {
        if (callingBotService == null)
        {
            throw new ArgumentNullException(nameof(callingBotService));
        }

        this.CallingBotService = callingBotService;

        this.CallingBotService.OnIncomingCallReceived += this.OnIncomingCallReceived;

    }
    public void Dispose()
    {
        if (this.CallingBotService != null)
        {
            this.CallingBotService.OnIncomingCallReceived -= this.OnIncomingCallReceived;
            this.CallingBotService.OnPlayPromptCompleted -= this.OnPlayPromptCompleted;
        }
    }

    private static PlayPrompt GetPromptForText(string text)
    {
        var prompt = new Prompt { Value = text, Voice = VoiceGender.Male };
        return new PlayPrompt { OperationId = Guid.NewGuid().ToString(), Prompts = new List<Prompt> { prompt } };
    }

    private Task OnIncomingCallReceived(IncomingCallEvent incomingCallEvent)
    {
        this.callStateMap[incomingCallEvent.IncomingCall.Id] = new CallState(incomingCallEvent.IncomingCall.Participants);

        incomingCallEvent.ResultingWorkflow.Actions = new List<ActionBase>
            {
                new Answer { OperationId = Guid.NewGuid().ToString() },
                GetPromptForText("Hello, I'm  QnA Concept. How can i help you ?")
            };

        return Task.FromResult(true);
    }

    private Task OnPlayPromptCompleted(PlayPromptOutcomeEvent playPromptOutcomeEvent)
    {
        var callState = this.callStateMap[playPromptOutcomeEvent.ConversationResult.Id];

        return Task.FromResult(true);
    }

    private class CallState
    {
        public CallState(IEnumerable<Participant> participants)
        {
            this.Participants = participants;
        }

        public string ChosenMenuOption { get; set; }

        public IEnumerable<Participant> Participants { get; }
    }
}

哪种方法正在收听我的输入(在skype案例中输入语音)? 我需要这个才能将其转换为文本。

0 个答案:

没有答案