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.
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.
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).
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.
Create a new console application with core 3. The name of my application is csharpcornertelegramchatbot.
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.
- using System;
- using Telegram.Bot;
- using Telegram.Bot.Args;
-
- namespace csharpcornertelegramchatbot
- {
- class Program
- {
-
-
-
- private static readonly TelegramBotClient bot = new TelegramBotClient("paste your token");
-
-
-
-
-
- static void Main(string[] args)
- {
- bot.OnMessage += Csharpcornerbotmessage;
- bot.StartReceiving();
- bot.StopReceiving();
-
- }
-
-
-
-
-
-
- private static void Csharpcornerbotmessage(object sender, MessageEventArgs e)
- {
- if(e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Text)
- PrepareQuestionnaires(e);
- }
- public static void PrepareQuestionnaires(MessageEventArgs e)
- {
- if (e.Message.Text.ToLower() == "hi")
- bot.SendTextMessageAsync(e.Message.Chat.Id, "hello dude" + Environment.NewLine + "welcome to csharp corner chat bot." + Environment.NewLine + "How may i help you ?");
- if (e.Message.Text.ToLower().Contains("know about"))
- 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 + "https://www.c-sharpcorner.com/about");
- if (e.Message.Text.ToLower().Contains("csharpcorner logo?"))
- {
- bot.SendStickerAsync(e.Message.Chat.Id, "https://csharpcorner-mindcrackerinc.netdna-ssl.com/App_Themes/CSharp/Images/SiteLogo.png");
- bot.SendTextMessageAsync(e.Message.Chat.Id, "Anything else?");
- }
- if (e.Message.Text.ToLower().Contains("list of featured"))
- bot.SendTextMessageAsync(e.Message.Chat.Id, "Give me your profile link ?");
- if (e.Message.Text.ToLower().Contains("here it is"))
- bot.SendTextMessageAsync(e.Message.Chat.Id, Environment.NewLine+"https://www.c-sharpcorner.com/article/getting-started-with-ionic-framework-angular-and-net-core-3/" + Environment.NewLine + Environment.NewLine +
- "https://www.c-sharpcorner.com/article/getting-started-with-ember-js-and-net-core-3/" + Environment.NewLine + Environment.NewLine+
- "https://www.c-sharpcorner.com/article/getting-started-with-vue-js-and-net-core-32/");
- }
- }
- }
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 ?
So, the below gif describes a Q& A with the csharpcorner bot.
Hope you guys learned something new. Keep learning!
Here are the links to my previous articles.