Пример #1
0
        /// <summary>Snippet for ListContexts</summary>
        public void ListContextsResourceNames()
        {
            // Snippet: ListContexts(SessionName, string, int?, CallSettings)
            // Create client
            ContextsClient contextsClient = ContextsClient.Create();
            // Initialize request argument(s)
            SessionName parent = SessionName.FromProjectSession("[PROJECT]", "[SESSION]");
            // Make the request
            PagedEnumerable <ListContextsResponse, Context> response = contextsClient.ListContexts(parent);

            // Iterate over all response items, lazily performing RPCs as required
            foreach (Context item in response)
            {
                // Do something with each item
                Console.WriteLine(item);
            }

            // Or iterate over pages (of server-defined size), performing one RPC per page
            foreach (ListContextsResponse page in response.AsRawResponses())
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (Context item in page)
                {
                    // Do something with each item
                    Console.WriteLine(item);
                }
            }

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int            pageSize   = 10;
            Page <Context> singlePage = response.ReadPage(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (Context item in singlePage)
            {
                // Do something with each item
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
        // [START dialogflow_create_context]
        public static int Create(string projectId,
                                 string sessionId,
                                 string contextId,
                                 int lifespanCount = 1)
        {
            var client = ContextsClient.Create();

            var context = new Context();

            context.ContextName   = new ContextName(projectId, sessionId, contextId);
            context.LifespanCount = lifespanCount;

            var newContext = client.CreateContext(
                parent: new SessionName(projectId, sessionId),
                context: context
                );

            Console.WriteLine($"Created Context: {newContext.Name}");

            return(0);
        }
        // [START dialogflow_list_contexts]
        public static int List(string projectId, string sessionId)
        {
            var client = ContextsClient.Create();

            var contexts = client.ListContexts(SessionName.FromProjectSession(projectId, sessionId).ToString());

            foreach (var context in contexts)
            {
                Console.WriteLine($"Context name: {context.Name}");
                Console.WriteLine($"Context lifespan count: {context.LifespanCount}");
                if (context.Parameters != null)
                {
                    Console.WriteLine("Fields:");
                    foreach (var field in context.Parameters.Fields)
                    {
                        Console.WriteLine($"{field.Key}: {field.Value}");
                    }
                }
            }

            return(0);
        }
        // [START dialogflow_create_context]
        public static int Create(string projectId,
                                 string sessionId,
                                 string contextId,
                                 int lifespanCount = 1)
        {
            var client = ContextsClient.Create();

            var context = new Context();

            context.ContextName   = new ContextName(projectId, sessionId, contextId);
            context.LifespanCount = lifespanCount;

            var createContextRequest = new CreateContextRequest
            {
                Parent  = SessionName.FromProjectSession(projectId, sessionId).ToString(),
                Context = context
            };

            var newContext = client.CreateContext(createContextRequest);

            Console.WriteLine($"Created Context: {newContext.Name}");

            return(0);
        }