Пример #1
0
        public async Task <SkillResponse> RouteRequest(SkillRequest input)
        {
            if (input.GetRequestType() == typeof(LaunchRequest))
            {
                await _mixpanel.TrackAsync("Launch", new {});

                return(await GetLaunchRequestResponse());
            }
            if (input.GetRequestType() == typeof(IntentRequest))
            {
                var intentRequest = (IntentRequest)input.Request;
                await _mixpanel.TrackAsync("Intent", new { Name = intentRequest.Intent.Name });

                if (intentRequest.Intent.Name == "InboxIntent")
                {
                    return(await GetInboxIntentResponse());
                }
                if (intentRequest.Intent.Name == "HotQuestionIntent")
                {
                    return(await GetHotQuestionIntentResponse());
                }
                if (intentRequest.Intent.Name == "HotQuestionDetailsIntent")
                {
                    return(await GetHotQuestionDetailsIntentResponse());
                }
                if (intentRequest.Intent.Name == "HotQuestionAnswersIntent")
                {
                    return(await GetHotQuestionAnswersIntentResponse());
                }
                if (intentRequest.Intent.Name == "UpvoteIntent")
                {
                    return(await GetUpvoteIntentResponse());
                }
                if (intentRequest.Intent.Name == "DownvoteIntent")
                {
                    return(await GetDownvoteIntentResponse());
                }
                if (intentRequest.Intent.Name == "FavoriteIntent")
                {
                    return(await GetFavoriteIntentResponse());
                }
                if (intentRequest.Intent.Name == "AMAZON.HelpIntent")
                {
                    return(CreateResponse(HelpText + MainMenuOptions));
                }
                if (intentRequest.Intent.Name == "AMAZON.CancelIntent")
                {
                    return(CreateResponse("Ok, bye!", true));
                }
                if (intentRequest.Intent.Name == "AMAZON.StopIntent")
                {
                    return(CreateResponse("Ok, bye!", true));
                }
            }
            return(CreateResponse("Sorry, not sure what you're saying."));
        }
Пример #2
0
        // Main entry point
        public async Task <SkillResponse> GetResponse(SkillRequest request, ILambdaContext context)
        {
            context.Logger.LogLine(JsonConvert.SerializeObject(request));
            MixpanelConfig.Global.SerializeJsonFn = JsonConvert.SerializeObject;
            _mixpanel = new MixpanelClient(MixPanelToken);

            //await _mixpanel.TrackAsync("GetResponse.Begin", new {});

            try
            {
                _context = context;
                _state   = RestoreState(request);
                _client  = new Client(StackApiKey, request?.Session?.User?.AccessToken);                // StackExchange API client
                var response = await RouteRequest(request);

                context.Logger.LogLine(JsonConvert.SerializeObject(response));
                //await _mixpanel.TrackAsync("GetResponse.End", new {});
                return(response);
            }
            catch (Exception ex)
            {
                await _mixpanel.TrackAsync("GetResponse.Exception", new {});

                context.Logger.LogLine("Unhandled exception:");
                context.Logger.LogLine(ex.ToString());
                context.Logger.LogLine(JsonConvert.SerializeObject(ex));
                return(CreateResponse($"Sorry, there was a technical problem. Please try again later."));
            }
        }
 public async Task TestMixpanel()
 {
     MixpanelConfig.Global.SerializeJsonFn = JsonConvert.SerializeObject;
     var mc = new MixpanelClient("<token>");
     await mc.TrackAsync("TestEvent", new {
         Time = DateTime.UtcNow
     });
 }
        public async Task TrackAsync_CanSendSingleEvent()
        {
            var client = new MixpanelClient(Token, new HttpClient());

            var mixpanelEvent = client.CreateEvent();

            mixpanelEvent.Name = nameof(TrackAsync_CanSendSingleEvent);

            var outcome = await client.TrackAsync(mixpanelEvent).ConfigureAwait(false);

            Assert.True(outcome.Successful);
        }
        public async Task TrackAsync_SendsSuccessfully_WhenEventIsNotNull()
        {
            var httpMessageHandlerMock = new Mock <HttpMessageHandler>();

            httpMessageHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("{\"status\": 1, \"error\": null}")
            })
            .Verifiable();

            var httpClient = new HttpClient(httpMessageHandlerMock.Object);

            IMixpanelClient client = new MixpanelClient(EmptyToken, httpClient);

            var @event = client.CreateEvent();

            var outcome = await client.TrackAsync(@event).ConfigureAwait(false);

            Assert.True(outcome.Successful);

            httpMessageHandlerMock.Protected().Verify("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>(request => request.Method == HttpMethod.Get), ItExpr.IsAny <CancellationToken>());
        }
        public void Track_AsyncHttpPostFnNotSet_ThrowsException()
        {
            var config = GetConfig();
            config.AsyncHttpPostFn = null;
            var mc = new MixpanelClient(Token, config);

            var aggregate = Assert.Throws<AggregateException>(
                () => mc.TrackAsync(Event, DistinctId, GetTrackDictionary()).Wait());
            Assert.That(aggregate.InnerExceptions.Single(), Is.InstanceOf<MixpanelConfigurationException>());
        }
        public void TrackAsync_ThrowsArgumentNullException_WhenEventIsNull()
        {
            IMixpanelClient client = new MixpanelClient(EmptyToken, EmptyHttpClientMock.Object);

            Assert.ThrowsAsync <ArgumentNullException>(async() => await client.TrackAsync(null).ConfigureAwait(false));
        }