예제 #1
0
 public VFatumbot(ConversationState conversationState, UserPersistentState userPersistentState, UserTemporaryState userTemporaryState, MainDialog dialog, ILogger <VFatumbot <MainDialog> > logger)
 {
     _conversationState   = conversationState;
     _userPersistentState = userPersistentState;
     _userTemporaryState  = userTemporaryState;
     _mainDialog          = dialog;
     _logger = logger;
 }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // Create the credential provider to be used with the Bot Framework Adapter.
            services.AddSingleton <ICredentialProvider, ConfigurationCredentialProvider>();

            // Add Application Insights services into service collection
            services.AddApplicationInsightsTelemetry();

            // Create the telemetry client.
            services.AddSingleton <IBotTelemetryClient, BotTelemetryClient>();

            // Add telemetry initializer that will set the correlation context for all telemetry items.
            services.AddSingleton <ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();

            // Add telemetry initializer that sets the user ID and session ID (in addition to other bot-specific properties such as activity ID)
            services.AddSingleton <ITelemetryInitializer, TelemetryBotIdInitializer>();

            // Create the telemetry middleware to initialize telemetry gathering
            services.AddSingleton <IMiddleware, TelemetryInitializerMiddleware>();

            // Create the telemetry middleware (used by the telemetry initializer) to track conversation events
            services.AddSingleton <TelemetryLoggerMiddleware>();

            // Create the Bot Framework Adapter with error handling enabled.
            services.AddSingleton <IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

            // For the bot running in the Azure cloud, we need to use Cosmos DB (or Azure's Blob Storage service)
            // to keep data persistent, otherwise the stateless nature of the bot would be useless in keeping
            // track of users's locations, radius settings etc.
            var persistentStorage = new CosmosDbStorage(new CosmosDbStorageOptions
            {
                AuthKey          = Consts.COSMOS_DB_KEY,
                CollectionId     = Consts.COSMOS_CONTAINER_NAME_PERSISTENT,
                CosmosDBEndpoint = new Uri(Consts.COSMOS_DB_URI),
                DatabaseId       = Consts.COSMOS_DB_NAME,
            });

            var temporaryStorage = new CosmosDbStorage(new CosmosDbStorageOptions
            {
                AuthKey          = Consts.COSMOS_DB_KEY,
                CollectionId     = Consts.COSMOS_CONTAINER_NAME_TEMPORARY,
                CosmosDBEndpoint = new Uri(Consts.COSMOS_DB_URI),
                DatabaseId       = Consts.COSMOS_DB_NAME,
            });

            var conversationState   = new ConversationState(persistentStorage);
            var userPersistentState = new UserPersistentState(persistentStorage);
            var userTemporaryState  = new UserTemporaryState(temporaryStorage);

            // Add the states as singletons
            services.AddSingleton(conversationState);
            services.AddSingleton(userPersistentState);
            services.AddSingleton(userTemporaryState);

/*
 *          //
 *          // In-mem only way
 *          //
 *          // Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
 *          services.AddSingleton<IStorage, MemoryStorage>();
 *
 *          // Create the User state.
 *          services.AddSingleton<UserPersistentState>();
 *          services.AddSingleton<UserTemporaryState>();
 *
 *          // Create the Conversation state.
 *          services.AddSingleton<ConversationState>();
 */

            // The Dialog that will be run by the bot.
            services.AddSingleton <MainDialog>();

            // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
            services.AddTransient <IBot, VFatumbot <MainDialog> >();
        }
예제 #3
0
        public MainDialog(UserPersistentState userPersistentState, UserTemporaryState userTemporaryState, ConversationState conversationState, ILogger <MainDialog> logger, IBotTelemetryClient telemetryClient) : base(nameof(MainDialog))
        {
            _logger = logger;
            _userPersistentState = userPersistentState;
            _userTemporaryState  = userTemporaryState;

            TelemetryClient = telemetryClient;

            if (_userPersistentState != null)
            {
                _userProfilePersistentAccessor = userPersistentState.CreateProperty <UserProfilePersistent>(nameof(UserProfilePersistent));
            }

            if (userTemporaryState != null)
            {
                _userProfileTemporaryAccessor = userTemporaryState.CreateProperty <UserProfileTemporary>(nameof(UserProfileTemporary));
            }

            AddDialog(new PrivacyAndTermsDialog(_userProfilePersistentAccessor, logger, telemetryClient));
            AddDialog(new MoreStuffDialog(_userProfileTemporaryAccessor, this, logger, telemetryClient));
            AddDialog(new TripReportDialog(_userProfileTemporaryAccessor, this, logger, telemetryClient));
            AddDialog(new ScanDialog(_userProfileTemporaryAccessor, this, logger, telemetryClient));
            AddDialog(new SettingsDialog(_userProfileTemporaryAccessor, logger, telemetryClient));
            AddDialog(new ChoicePrompt(nameof(ChoicePrompt))
            {
                TelemetryClient = telemetryClient,
            });
            AddDialog(new ChoicePrompt("AskHowManyIDAsChoicePrompt",
                                       (PromptValidatorContext <FoundChoice> promptContext, CancellationToken cancellationToken) =>
            {
                // override validater result to also allow free text entry for ratings
                int idacou;
                if (int.TryParse(promptContext.Context.Activity.Text, out idacou))
                {
                    if (idacou < 1 || idacou > 20)
                    {
                        return(Task.FromResult(false));
                    }

                    return(Task.FromResult(true));
                }

                return(Task.FromResult(false));
            })
            {
                TelemetryClient = telemetryClient,
            });
            AddDialog(new TextPrompt("GetQRNGSourceChoicePrompt",
                                     (PromptValidatorContext <string> promptContext, CancellationToken cancellationToken) =>
            {
                // verify it's a 64 char hex string (sha256 of the entropy generated)
                if (promptContext.Context.Activity.Text.Length != 64)
                {
                    return(Task.FromResult(false));
                }

                // regex check
                Regex regex = new Regex("^[a-fA-F0-9]+$");
                return(Task.FromResult(regex.IsMatch(promptContext.Context.Activity.Text)));
            })
            {
                TelemetryClient = telemetryClient,
            });
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                ChoiceActionStepAsync,
                PerformActionStepAsync,
                AskHowManyIDAsStepAsync,
                SelectQRNGSourceStepAsync,
                GetQRNGSourceStepAsync,
                GenerateIDAsStepAsync
            })
            {
                TelemetryClient = telemetryClient,
            });

            InitialDialogId = nameof(WaterfallDialog);
        }