Before proceeding further, I recommend you read my first article: Getting Started with Microsoft Bot Framework.

Once you are done with creating the project as you can see there is an API Controller in Controllers folder named “MessageController”.

  1. [BotAuthentication]  
  2. public class MessagesController: ApiController  
  3. {  
  4.     /// <summary>    
  5.     /// POST: api/Messages    
  6.     /// Receive a message from a user and reply to it    
  7.     /// </summary>    
  8.     public async Task < Message > Post([FromBody] Message message)  
  9.     {  
  10.         if (message.Type == "Message")  
  11.         {  
  12.             // calculate something for us to return    
  13.             int length = (message.Text ? ? string.Empty).Length;  
  14.             // return our reply to the user    
  15.             return message.CreateReplyMessage($ "You sent {length} characters");  
  16.         }  
  17.         else  
  18.         {  
  19.             return HandleSystemMessage(message);  
  20.         }  
  21.     }  
  22. }   

I am changing text a little bit and my message will look like this.

  1. /// <summary>    
  2. /// POST: api/Messages    
  3. /// Receive a message from a user and reply to it    
  4. /// </summary>    
  5. public async Task < Message > Post([FromBody] Message message)  
  6. {  
  7.     if (message.Type == "Message")  
  8.     {  
  9.         // return our reply to the user    
  10.         return message.CreateReplyMessage($ "Raj said:{message.Text}");  
  11.     }  
  12.     else  
  13.     {  
  14.         return HandleSystemMessage(message);  
  15.     }  
  16. }  

Run your application,


If you want to run on emulator to test your calls, Install emulator form here.

Pass three things when you run Bot Emulator.

  • URL - You have to mention your application url with port before api/messages.
  • AppId - Defined in web.config
  • AppSecret - Defined in web.config


Conclusion

In this article, we have learned how to create a bot using Microsoft Bot Framework. In next article we will see how to publish your Bot Application to Microsoft Azure. If you have any question or comments, message me on C# Corner comments section.

 

Next Recommended Readings