Integrate Telegram Chat Bot With .NET Core 3

In my previous articles, I briefly explained about front-end frameworks and cross-platform applications with .NET Core 3. Now, I have decided to work on ChatBot and it's very popular presently. That's why I prepared this article on Telegram chatbot with .NET Core 3.

What is a Chat Bot? 

A chatbot is an artificial intelligence software that can replicate a conversation with a user in natural language through a messaging application, websites, ad mobile app. However, from a technical point of view a chatbot acts as a Question Answering System. 
Integrate Telegram Chat Bot With .NET Core

Why is chatbot important nowadays?

There are many points to describe the importance of chatbots but here I have listed only a few of them, such as scaling up operations, handling a lot of customer queries, marketing content through online channels, driving up organization efficeincy,  and selling to millennials.
Let's implement a telegram chatbot with .NET Core 3.
First, we have to generate a token with BotFather to handle messages.
Login with telegram and Type @BotFather in search area.
Integrate Telegram Chat Bot With .NET Core
Now, type /newbot in the message. It will ask you to name your bot. In this example. My username is csharpcorner_bot (username must be end with _bot).
Integrate Telegram Chat Bot With .NET Core
Finally, you have your bot token. Now we have to handle messages with .NET Core 3.
Download the latest version of this SDK and install it.
Integrate Telegram Chat Bot With .NET Core
Create a new console application with core 3. The name of my application is csharpcornertelegramchatbot.
Integrate Telegram Chat Bot With .NET Core
You have to add one nuget packge to use inbuit methods. Install this nuget as described here. After, open program.cs file and add the below code in that file.
Integrate Telegram Chat Bot With .NET Core
  1. using System;  
  2. using Telegram.Bot;  
  3. using Telegram.Bot.Args;  
  5. namespace csharpcornertelegramchatbot  
  6. {  
  7.     class Program  
  8.     {  
  9.         /// <summary>  
  10.         /// Declare Telegrambot object  
  11.         /// </summary>  
  12.         private static readonly TelegramBotClient bot = new TelegramBotClient("paste your token");  
  14.         /// <summary>  
  15.         /// csharp corner chat bot web hook  
  16.         /// </summary>  
  17.         /// <param name="args"></param>  
  18.         static void Main(string[] args)  
  19.         {  
  20.             bot.OnMessage += Csharpcornerbotmessage;  
  21.             bot.StartReceiving();           
  22.             bot.StopReceiving();  
  24.         }  
  26.         /// <summary>  
  27.         /// Handle bot webhook  
  28.         /// </summary>  
  29.         /// <param name="sender"></param>  
  30.         /// <param name="e"></param>  
  31.         private static void Csharpcornerbotmessage(object sender, MessageEventArgs e)  
  32.         {  
  33.             if(e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Text)             
  34.                 PrepareQuestionnaires(e);              
  35.         }  
  36.         public static void PrepareQuestionnaires(MessageEventArgs e)  
  37.         {  
  38.             if (e.Message.Text.ToLower() == "hi")  
  39.                 bot.SendTextMessageAsync(e.Message.Chat.Id, "hello dude" + Environment.NewLine + "welcome to csharp corner chat bot." + Environment.NewLine + "How may i help you ?");  
  40.             if (e.Message.Text.ToLower().Contains("know about"))  
  41.                 bot.SendTextMessageAsync(e.Message.Chat.Id, "Yes sure..!!" + Environment.NewLine + "Mahesh Chand is the founder of C# Corner.Please go through for more detail." + Environment.NewLine + "");  
  42.             if (e.Message.Text.ToLower().Contains("csharpcorner logo?"))  
  43.             {  
  44.                 bot.SendStickerAsync(e.Message.Chat.Id, "");  
  45.                 bot.SendTextMessageAsync(e.Message.Chat.Id, "Anything else?");  
  46.             }  
  47.             if (e.Message.Text.ToLower().Contains("list of featured"))  
  48.                 bot.SendTextMessageAsync(e.Message.Chat.Id, "Give me your profile link ?");  
  49.             if (e.Message.Text.ToLower().Contains("here it is"))  
  50.                 bot.SendTextMessageAsync(e.Message.Chat.Id, Environment.NewLine+"" + Environment.NewLine + Environment.NewLine +  
  51.                     "" + Environment.NewLine + Environment.NewLine+  
  52.                     "");  
  53.         }  
  54.     }  
  55. }  
It's done, so now run your console app ask the csharpcorner bot some questions.My some questions are listed here.
User : hi 
Bot : Hello dude welcome to csharp corner chat bot.How may i help you ?
User : want to know about csharp corner ?
Bot : Yes sure..!! Mahesh Chand is the founder of C# Corner.Please go through for more detail.
So, the below gif describes a Q& A with the csharpcorner bot.
Integrate Telegram Chat Bot With .NET Core
Hope you guys learned something new. Keep learning!