The basic bot template shows how to use dialogs to respond to user input. For information about using dialogs, see Dialogs.
When a user posts a message, it’s sent to the bot’s Run method in the Run.csx file. Before the bot processes the message, it first authenticates the request (see Authentication). If the validation fails, the bot responds with Unauthorized. The following shows the code that the template uses to authenticate the request.
If the request is authentic, the bot processes the message. The message includes an activity type, which the bot uses to decide what action to take. You would update this code if you want to respond to the other activity types. For example, if you save user state, you’ll need to respond to the DeleteUserData message.
The first message that the channel sends to the bot is a ConversationUpdate message that contains the users that are in the conversation. The template shows how you’d use this message to welcome the users to the conversation. Because the bot is considered a user in the conversation, the list of users will include the bot. The code identifies the bot and ignores it when welcoming the other users.
Most messages will have a Message activity type, and will contain the text and attachments that the user sent. If the message’s activity type is Message, the template posts the message to EchoDialog in the context of the current message (see EchoDialog.csx).
The EchoDialog.csx file contains the dialog that controls the conversation with the user. When the dialog’s instantiated, the dialog’s StartAsync method runs and calls IDialogContext.Wait with the continuation delegate that’s called when there is a new message. In the initial case, there is an immediate message available (the one that launched the dialog) and the message is immediately passed to the MessageReceivedAsync method.
The MessageReceivedAsync method echoes the user’s input and counts the number of interactions with the user. If the user’s input is the word, reset, the method uses PromptDialog to confirm that the user wants to reset the counter. The method calls the IDialogContext.Wait method which suspends the bot until it receives the next message.
The AfterResetAsync method processes the user’s response to PromptDialog. If the user confirms the request, the counter is reset and a message is sent to the user confirming the action. The method then calls the IDialogContext.Wait method to suspend the bot until it receives the next message, which gets processed by MessageReceivedAsync.
For information about the other files that Bot Service creates, see Bot Service Template Overview.
You might update this code if you want to change the conversation with the user. For example, by adding other prompts to get information from the user, adding attachments such as cards to provide a richer user experience, or chaining together other dialogs (see Dialog Chains).