Ejemplo n.º 1
0
        /// <summary>
        /// Configures the services. This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services">The services.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddBot <EchoBot>(options =>
            {
                IStorage dataStore = new MemoryStorage();

                // --> Adding conversation state handler which understands a team as single conversation.
                options.State.Add(new TeamSpecificConversationState(dataStore));

                // Drop all activities not coming from Microsoft Teams.
                options.Middleware.Add(new DropNonTeamsActivitiesMiddleware());

                // --> Add Teams Middleware.
                options.Middleware.Add(
                    new TeamsMiddleware(
                        new ConfigurationCredentialProvider(this.Configuration)));

                options.CredentialProvider = new ConfigurationCredentialProvider(this.Configuration);
            });

            services.AddSingleton(sp =>
            {
                BotFrameworkOptions options = sp.GetRequiredService <IOptions <BotFrameworkOptions> >().Value;
                if (options == null)
                {
                    throw new InvalidOperationException("BotFrameworkOptions must be configured prior to setting up the State Accessors");
                }

                TeamSpecificConversationState conversationState = options.State.OfType <TeamSpecificConversationState>().FirstOrDefault();
                if (conversationState == null)
                {
                    throw new InvalidOperationException("ConversationState must be defined and added before adding conversation-scoped state accessors.");
                }

                // Create the custom state accessor.
                // State accessors enable other components to read and write individual properties of state.
                var accessors = new EchoStateAccessor(conversationState)
                {
                    CounterState = conversationState.CreateProperty <EchoState>(EchoStateAccessor.CounterStateName),
                };

                return(accessors);
            });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="EchoBot"/> class.
 /// </summary>
 /// <param name="echoStateAccessor">The echo state accessor.</param>
 public EchoBot(EchoStateAccessor echoStateAccessor)
 {
     this.echoStateAccessor = echoStateAccessor;
 }