Ejemplo n.º 1
0
            public override async Task <TextRespone> Invork(VoiceRequest request)
            {
                var time = DateTime.Now;

                string     result     = string.Empty;
                WebRequest webRequest = WebRequest.Create($"https://www.google.com/speech-api/v2/recognize?output=json&lang={_language}&key={AppConfig.GoogleSpeechApiKey}");

                webRequest.Method        = "POST";
                webRequest.ContentType   = "audio/l16; rate=16000"; //"16000";
                webRequest.ContentLength = request.VoiceData.Length;
                await webRequest.GetRequestStream().WriteAsync(request.VoiceData, 0, request.VoiceData.Length);

                HttpWebResponse webResponse = (HttpWebResponse)await webRequest.GetResponseAsync();

                using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
                {
                    var jsonResult = await streamReader.ReadToEndAsync();

                    try
                    {
                        var resStrings = jsonResult.Split("\"transcript\":\"");
                        if (resStrings.Length > 1)
                        {
                            result = resStrings[1].Split('"').First();
                        }
                    }
                    catch { throw new Exceptions.InvalidTextDataException(); }
                    finally { if (string.IsNullOrWhiteSpace(result))
                              {
                                  throw new Exceptions.InvalidTextDataException();
                              }
                    }
                }
                TextRespone respone = new TextRespone()
                {
                    Id       = request.Id.Value,
                    TextData = result
                };

                Log.LogInformation(request.Id.Value, 0, this.GetType().ToString(), $"service end work in {(DateTime.Now - time).Milliseconds} ms");

                return(respone);
            }
Ejemplo n.º 2
0
        /// <summary>
        /// 关注事件的处理
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public override string HandleSubscribeEvent(SubscribeEvent message)
        {
            string result  = string.Empty;
            var    Manager = DI.DIEntity.Instance.GetImpl <IWeChatConcern>();
            //根据原始ID获取关注回复
            var concern = Manager.GetByOriginalId(message.ToUserName);

            if (concern.bIsConcernOn)
            {     //开启关注回复
                if (concern.iConcernType == 1)
                { //文本回复
                    TextRespone model = new TextRespone(message);
                    model.Content = concern.sContent;
                    result        = ActioningRespone.Serialize(model);
                }
                if (concern.iConcernType == 2)
                {//图文回复
                    NewsRespone model = new NewsRespone(message);
                    model.Articles.Add(new item()
                    {
                        Title       = "测试图文消息",
                        Description = "123456",
                        Url         = "http://www.baidu.com",
                        PicUrl      = "https://p1.ssl.qhimg.com/d/_hao360/weather/0.png"
                    });
                    model.Articles.Add(new item()
                    {
                        Title       = "测试图文消息1",
                        Description = "555555",
                        Url         = "http://www.baidu.com",
                        PicUrl      = "https://p1.ssl.qhimg.com/d/_hao360/weather/0.png"
                    });
                    model.ArticleCount = model.Articles.Count;
                    result             = ActioningRespone.Serialize(model);
                }
            }
            return(result);
        }