Monday, October 24, 2016

MessageController in Bot Framework

  The Message Controller is the meat of  Bot application

  • It resides in an asp.net application
  • It can accept a message and return a message
The message controller monitors the different activities of the User

The default type of activity is is Activity.message



 [BotAuthentication]
    public class MessagesController : ApiController
    {
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            { }
            else
            { }
           
            return response;
        }
}
If the activity type is not default message then we switch handle to different other activity types

1) DeleteUserData 
       Handle user deletion
2) ConversationUpdate
       Handle conversation state changes, like members being added and        removed
3) ContactRelationUpdate
       Handle add/remove from contact list
4) Typing
       Handle knowing that the user is typing
5) Ping 
      Handle for ping



 private Activity HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
                return message.CreateReply("Delete User data");
            }
            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
                return message.CreateReply("Conversation update");
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
                return message.CreateReply("Contact Relationship update");
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
                return message.CreateReply("typing");
            }
            else if (message.Type == ActivityTypes.Ping)
            {
                return   message.CreateReply("ping");
            }

            return message;
        }

No comments:

Post a Comment