Tutorial: Creating Telegram ChatGPT Chat Bots in C#.
This tutorial will walk you through the process of creating a Telegram ChatBot using a .NET 6.0 Windows Console application. The application you will develop through this tutorial will connect with both the Telegram API and the ChatGPT API to enable basic features that merge these two services. The end goal is to host a responsive telegram bot that calls on ChatGPT to provide an answer to a query on Telegram, and also, to create an AI-generated chat conversation with multiple character personalities.
In summary, this project teaches the following:
- Learn how to connect and interact with the Telegram API through C# using Telegram.Bot.
- Learn how to connect and interact with to ChatGPT API through C# using HttpClient and JSON.
- Understand the basics of HttpClient and JSON serialization and deserialization.
- Learn how to merge Telegram and ChatGPT services for the purpose of creating a dynamic and responsive GPT chat bot.
- Learn how to create a fun bot chat where two GPT bots speak with one another through Telegram.
Step One – Connecting to the Telegram API:
Firstly, create a new .NET 6.0 or higher console application in Visual Studio. Title it as you wish, I titled mine TelegramChatBot.
Once the project is created, we need to import the Telegram dependency from NuGet. There are many libraries that exist to help your application interact with Telegram. In this tutorial, I will utilize Telegram.Bot v19.0.0, which you can find and download from NuGet through Visual Studio. Simply open NuGet, search for Telegram.Bot developed by RoundRobin, Poulad, and Tuscen, and proceed with the download Project GitHub here: https://github.com/TelegramBots/telegram.bot
We’ll also need to create our bots through Telegram. Follow these instructions here, scroll to “How Do I Create a Bot”: https://core.telegram.org/bots#how-do-i-create-a-bot
Now, create two classes as per the gist code below, ChatBot.cs and TelegramGPTBots.cs. The ChatBot has properties for a TelegramBotClient, bot name, full name, and personality. The class is initialized with an API token and default values for the bot’s name, full name, and personality, and it starts a Telegram bot client. It includes methods for handling updates from Telegram, managing errors, and a placeholder for the ChatGPT reply functionality which will develop in the proceeding steps..
Within TelegramGPTBots, I’ve initialized two Telegram bots and they are given API tokens, bot names, and other relevant parameters for the creation of a GPT chat bot. A bot client will run independently, and handle updates and communication with the Telegram API while connected. Replace the quoted text, “key”, “botname”, etc, with your Telegram bot info.
Step 2: Interfacing with ChatGPT API:
Within this step we’ll create the functionality to connect to ChatGPT. This will require that you have a ChatGPT account and an API key. You can generate and access your ChatGPT API keys here.
Within your project, create a new class file titled ChatGPT. We will use HttpClient to connect to ChatGPT API and JSON for serializing and deserializing data sent and received.
What is HttpClient?
- HttpClient is a class in .NET used to send HTTP requests and receive HTTP responses from a web service or API.
- In this class, an instance of HttpClient named
client
is created. It is used to make an HTTP POST request to the ChatGPT API endpoint. - The
client
instance is configured with the API key and other request parameters before making the request.
DefaultRequestHeaders.Authorization:
- This line of code sets the authorization header for the HttpClient instance. It adds an “Authorization” header with a “Bearer” token, followed by the API key. This header is used to authenticate the request with the ChatGPT API.
- By setting the Authorization header, the API key is sent with each request, allowing the API to verify the identity of the sender and grant or deny access accordingly.
What is JSON?
- JSON, or JavaScript Object Notation, is a lightweight data interchange format created and used for the purpose of structuring data in an easily transmissible way between client and server.
- For this project, we use JSON to create a request object with relevant data, like our query and GPT variables, required for interacting with the ChatGPT API. We then serialize that data with JsonConvert.SerializeObject and sent it to ChatGPT.
- ChatGPT then returns its response with a serialized JSON object, which we deserialize and read.
Basic ChatGPT Class:
Now that we have created the ChatGPT functionality, let us come back to ChatBot.cs HandleUpdateAsync function. In this code segment, an instance of ChatGPT is used to create a chat bot response (variable named gptResponse) when a message contains the bot’s name or when it’s a reply to a message from the bot itself. The message text is extracted and passed as a query to ChatGPT, generating a response based on the defined personality. The response is then sent back to the Telegram chat, effectively enabling the chatbot to engage in interactive conversations.
And finally, we need an entry point. Open Program.cs and we’ll write a few lines to start our Telegram bots and handle command prompt entries.
That’s it, it should work. If you have any issues, the full project can be downloaded on github here: https://github.com/MrChrisHammond/BasicTelegramChatGPTBot
Remember, this is just a very basic demo tutorial to get you started. You can build from here and have fun with it!