On 12th February in Europe, we have Junior Achievement day for school kids. It`s an amazing day when kids can work together with industry professionals one day in any profession. I participate from the industry side, this was the fourth time for me. I prepare for this day very property because you are that person who must give the best feedback about your profession.
This year I had three boys from Class 5 and 6. They were very attractive. After a few questions from them, I got an understanding of what kind of practical task they need. So these three young boys wanted some fun programming task and my task was to do my best. I decided to build small and simple chatbot using Telegram messenger.
Our plan was
- Describe and talk about Bot we would like to build
- Find technologies we can use to build our Bot
- Build environment, configure tools
- Write a simple prototype and test them
- Improve prototype and made it complete
Describe and talk about Bot we would like to build
What our bot will do? After receiving the message "notepad" from the user our program opens notepad on PC (Personal Computer). That was our goal. It seems quite fun π!
Find technologies we can use to build our Bot
Like all developers do a lot, we start googling. The main keywords were "how to build Telegram bot using C#". We filtered all information and came to a simple framework build on .NET for building simple Bots. And this framework has a lot of GitHub stars and the last commit was not so long time ago. Then we decide to choose this framework for our Bot. π
Build environment, configure tools
Telegram.Bot is written on .NET, which means that we need to find tools that we will use for building Bot prototype.
Our research completed with the following list:
After we completed the installation of our tool we were ready to start to write the first line of code. We developers this part like the most. π
Write Β simple prototype and test them
The framework we choose has good quick start documentation. We started with creating a simple Console .NET Core application by using the Windows command prompt.
We added NuGet package Telegram.Bot to our newly created application. And opened our application using Visual Studio Code. Like quick start documentation described we added a few lines of code to test our Bot.
After completing the compilation of our code (use F5 in Visual Studio Code to run your code) we needed to register our new Bot and get access token. Using @BotFather in Telegram messenger we registered our Bot and god access token. After that, we had the first run π€.
But as we remember our goal was to receive a message from the user and handle them. For this, we used an example found in documentation First chat bot.
Our complete code for the current state was.
After a few tests with our prototype, we now were ready to build our idea we had.
Improve prototype and made it complete
As before we started to google how to open a program, like a notepad from our console application. Of course solution, we found in StackOverflow. The keywords we used "how to launch notepad C#".
The solution we found.
Process.Start("notepad");
We modified our Bot_OnMessage method to execute the code we found.
static async void Bot_OnMessage(object sender, MessageEventArgs e) {
if (e.Message.Text != null)
{
Process.Start("notepad");
}
}
When we test our application the first time I saw so many exciting emotions in kid's eyes. π
After that, we play some time with this application and make some more fun to open notepad, paint and close them using Telegram messenger.
We had a lot of fun! π
[eof] Β