Esempio n. 1
0
        // Main method for checking if any of the stored flights (being followed by users) have changed their prices
        public async Task <bool> CheckForPriceUpdates(Dictionary <string, Airport> airports, FlightDetails fd,
                                                      Activity activity, ConnectorClient connector, string guid)
        {
            bool changed = false;

            try
            {
                // Connects to Azure Table Storage in order to retrieve th details of
                // flights being watched by users
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting(StrConsts.cStrStorageConnectionStr));

                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

                CloudTable table = tableClient.GetTableReference(
                    CloudConfigurationManager.GetSetting(StrConsts.cStrBotId));

                TableQuery <TravelEntity> query = new TableQuery <TravelEntity>().
                                                  Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.NotEqual, string.Empty));

                // Gets all the flights being watched
                IEnumerable <TravelEntity> results = table.ExecuteQuery(query);

                // Let's check if any of the flights stored have a change in price
                foreach (TravelEntity entity in results)
                {
                    TripsSearchResponse result = null;
                    TripOptionsRequest  req    = null;

                    // Get the details of a stored flight (being followed)
                    fd = GetStoredFlightData(entity, storageAccount, out req);

                    // If the stored flight was submitted by the user currently
                    // interacting with our bot - very important :)
                    if (fd.UserId.ToLower() == activity.From.Id.ToLower())
                    {
                        // Query the QPX Express API to see if the current price
                        // of the flight stored differs
                        string res = ShowUpdatedResults(airports, fd, out result);

                        // If the flight is still relevant (has not expired)
                        if (res != string.Empty)
                        {
                            // If indeed the current price differs from when it was
                            // originally requested by the user
                            if (PriceHasChanged(entity, result, storageAccount))
                            {
                                changed = true;

                                // Save the new flight price on Azure Storage Table
                                using (TableStorage ts = new TableStorage(req, result, fd))
                                    guid = ts.Save();

                                changed = true;

                                // Create the response with the current flight price that will be
                                // sent back to the user
                                Activity reply = activity.CreateReply(GatherQuestions.cStrPriceChange +
                                                                      StrConsts._NewLine + StrConsts._NewLine + res + StrConsts._NewLine +
                                                                      StrConsts._NewLine + GatherQuestions.cStrGatherRequestProcessed +
                                                                      StrConsts._NewLine + StrConsts._NewLine +
                                                                      GatherQuestions.cStrUnFollow + guid);

                                await connector.Conversations.ReplyToActivityAsync(reply);
                            }
                        }
                        else
                        {
                            // The flight is no longer relevant
                            // so it should be removed from Azure Storage
                            RemoveEntity(GetKey(fd));
                        }
                    }
                }
            }
            catch { }

            // If there has been a price change
            // return true
            return(changed);
        }