How To Show Greet Message On Bot Chat

This is a very simple yet useful article. If you are new to learning bot framework, please follow my previous article.
 
When we launch any bot, asyncPost method is invoked on messagesController like below.
  1. public async Task<HttpResponseMessage> Post([FromBody]Activity activity)  
  2.        {  
  3.            if (activity.Type == ActivityTypes.Message)  
  4.            {  
  5.                await Conversation.SendAsync(activity, () => new BingSearchDialog());  
  6.            }  
  7.            else  
  8.            {  
  9.                await HandleSystemMessage(activity);  
  10.            }  
  11.            var response = Request.CreateResponse(HttpStatusCode.OK);  
  12.            return response;  
  13.        }  
In the above code, HandleSystemMessage method will be invoked by default.  
  1. private async Task<Activity> HandleSystemMessage(Activity message)  
  2.       {  
  3.           if (message.Type == ActivityTypes.DeleteUserData)  
  4.           {  
  5.               // Implement user deletion here  
  6.               // If we handle user deletion, return a real message  
  7.           }  
  8.           else if (message.Type == ActivityTypes.ConversationUpdate)  
  9.           {  
  10.               // Handle conversation state changes, like members being added and removed  
  11.               // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info  
  12.               // Not available in all channels  
  13.               IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;  
  14.               if (iConversationUpdated != null)  
  15.               {  
  16.                   ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));  
  17.                   foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())  
  18.                   {  
  19.                       // if the bot is added, then   
  20.                       if (member.Id == iConversationUpdated.Recipient.Id)  
  21.                       {  
  22.                           var reply = ((Activity)iConversationUpdated).CreateReply($"Hey Welcome to search chatbot.");  
  23.                           await connector.Conversations.ReplyToActivityAsync(reply);  
  24.                       }  
  25.                   }  
  26.               }                  
  27.           }  
  28.           else if (message.Type == ActivityTypes.ContactRelationUpdate)  
  29.           {  
  30.               // Handle add/remove from contact lists  
  31.               // Activity.From + Activity.Action represent what happened  
  32.           }  
  33.           else if (message.Type == ActivityTypes.Typing)  
  34.           {  
  35.               // Handle knowing tha the user is typing  
  36.           }  
  37.           else if (message.Type == ActivityTypes.Ping)  
  38.           {  
  39.           }  
  40.   
  41.           return null;  
  42.       }  
Output
 
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now