コード例 #1
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            var activity = await result as Activity;

            if (count++ == 0)
            {
                Activity reply = activity.CreateReply();
                reply.AddHeroCard("Choose any option:", new List <string> {
                    "Hiking", "Safari"
                }, new List <string> {
                    "Hiking", "Safari"
                });
                await context.PostAsync(reply);

                context.Wait(MessageReceivedAsync);
            }
            else
            {
                StateClient state    = activity.GetStateClient();
                BotData     userData = await state.BotState.GetPrivateConversationDataAsync(activity.ChannelId, activity.Conversation.Id, activity.From.Id);

                userData.SetProperty <string>("SelectedOption", activity.Text);

                await context.Forward(new NextStepDialog(), ResumeAfter, activity, CancellationToken.None);
            }
        }
コード例 #2
0
        private async Task HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                // Handle conversation state changes, like members being added and removed
                // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
                // Not available in all channels

                // When the bot enters the conversation, post a first message to provide the user some information on the bot's capabilities
                // We filter for the name of the new user in the chat, so we need to enter the possibles names of the bot here
                var botnames = new string[] { "Bot", "SupportBot", "Support" };
                // What is the name of the user who entered the conversation
                var newuser = message.MembersAdded?.ElementAt(0).Name;
                // Did the bot enter the conversation?
                if (botnames.Contains(newuser))
                {
                    // Create a message activity to send to the conversation
                    Activity categoriesMessage = message.CreateReply();
                    Activity helpHintMessage   = message.CreateReply();
                    helpHintMessage.Text = Resources.BotTexts.HelpHint;

                    // We want to provide buttons for the user to pick between the different bot capabilities.
                    // To create buttons, we use a Hero Card with a number of buttons.
                    categoriesMessage.AddHeroCard <string>(Resources.BotTexts.WelcomeTextCategories, new List <string>()
                    {
                        Resources.BotTexts.TechnicalQuestion, Resources.BotTexts.SearchWikipediaTerm, Resources.BotTexts.SearchThesaurusTerm, Resources.BotTexts.TranslateTerm
                    });

                    // We need a client to send the message to the conversation, so we create a new one from the the current scope.
                    // This is a new client, which sends to the user. It is decoupled from the main dialog
                    using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
                    {
                        var client = scope.Resolve <IConnectorClient>();
                        // Send the message to the conversation
                        await client.Conversations.ReplyToActivityAsync(categoriesMessage);

                        await client.Conversations.ReplyToActivityAsync(helpHintMessage);
                    }
                }
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
            }
            else if (message.Type == ActivityTypes.Ping)
            {
            }
        }