Ejemplo n.º 1
0
        private static async Task <SendBookingResponse> SendBooking(string hotelId, DateTime from, DateTime to)
        {
            var windingTreeModule = new WindingTreeModule();
            var hotels            = await windingTreeModule.GetAllHotels();

            var hotel = hotels.Items.Where(x => x.Id == hotelId).FirstOrDefault();

            var booking = new SendBookingModel()
            {
                HotelId  = hotelId,
                Customer = CreateCustomer(),
                Pricing  = CreatePricing(),
                Booking  = CreateBooking(hotel, from, to)
            };

            return(await windingTreeModule.SendBookingAsync(booking, hotel.BookingUri));
        }
Ejemplo n.º 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            WebhookRequest request = null;

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            string responseJson = null;
            string userId       = null;

            try
            {
                request = jsonParser.Parse <WebhookRequest>(requestBody);
            }
            catch (InvalidProtocolBufferException ex)
            {
                log.LogError(ex, "Web hook request could not be parsed.");
                return(new BadRequestObjectResult("Error deserializing Dialog flow request from message body"));
            }

            userId = GetUserId(request);
            string          intent               = request.QueryResult.Intent.DisplayName;
            RequestType     requestType          = GetRequestType(intent);
            WebhookResponse webHookResponse      = null;
            var             windingTreeConnector = new WindingTreeModule();

            switch (requestType)
            {
            case RequestType.GetHotels:
                var city   = request.QueryResult.Parameters.Fields["geo-city"].ToString().Trim('"');
                var hotels = await windingTreeConnector.GetAllHotels();

                var    select         = hotels.Items.Where(x => x.Address.City.Equals(city)).Select(x => x.Name).ToList();
                string selectedHotels = "";
                foreach (var hotel in select)
                {
                    selectedHotels += $"{hotel}, ";
                }
                if (select.Count() == 0)
                {
                    webHookResponse = GetDialogFlowResponse(userId, $"Sorry, there are no available hotels in {city}.");
                }
                else if (select.Count() == 1)
                {
                    webHookResponse = GetDialogFlowResponse(userId, $"Available hotel in {city} is {selectedHotels}. Do you wanna make reservation in {selectedHotels}?");
                }
                else
                {
                    webHookResponse = GetDialogFlowResponse(userId, $"Available hotels in {city} are {selectedHotels}.");
                }
                break;

            case RequestType.SendBooking:
                var from     = Convert.ToDateTime(request.QueryResult.Parameters.Fields["checkin"].ToString().Trim('"'));
                var to       = Convert.ToDateTime(request.QueryResult.Parameters.Fields["checkout"].ToString().Trim('"'));
                var response = await SendBooking("0x6036848A2C4c43e6EFAAF885615f8667569caA00", from, to);

                webHookResponse = GetDialogFlowResponse(userId, $"I am making a reservation from {from.ToShortDateString()} to {to.ToShortDateString()} in Foreign Friend Guest House. Your reservation ID is {response.Id.ToString()}. I sent you details to your email.");

                break;
            }

            responseJson = webHookResponse?.ToString();
            log.LogCritical(responseJson);
            ContentResult contRes = new ContentResult()
            {
                Content     = responseJson,
                ContentType = "application/json",
                StatusCode  = 200
            };

            return(contRes);
        }