

If it doesn’t, we try getting another object from the collection. This way, we’re always ensuring that we get an element that exists in the list!Īnother complication was that not all the objects in the collection have an image associated with them, and so we need to use a while loop which checks to see if the collectionItem has an image associated with it. Create a listener so that we can wait for messages to be sent to the bot public async Task ListenForMessagesAsync () īy passing in the objectList, RandomNumberFromList() selects a random element inside the list and returns it to RandomImageRequestAsync(). Access token in hand, I started a new console project in Visual Studio and then entered the following test code into my Program.cs: After installing the NuGet package, the first thing I did was to create my bot using the Telegram Botfather in order to obtain my access token. To interface with the Telegram API, I used the Telegram.Bot library, and getting started was simple. You can find the entire codebase for MetBot on GitHub (feel free to give it a star if you like it!). Requesting a random artwork from the collection: When you finish this tutorial, you should have a bot which looks similar to the following: Not only does this project make for a useful way to learn the basics of Telegram bot development, it also serves a practical purpose of exposing myself to interesting artworks that I may not have otherwise known about! A few days later, I stumbled upon the Metropolitan Museum of Art Collection API, and suddenly I had a great idea - to make a simple bot which displays artworks from the Met’s collection! The Met has an impressive collection consisting of data and 406,000 images of works from their collection, all available for use without copyright or restriction and licensed under Creative Commons Zero. I’ve been using Telegram more often recently to speak with friends, and after reading an article on Twitter bots I became inspired to try my hand at creating a Telegram bot.
