I’m sure that by now many of you have played around with the ChatGPT model by OpenAI. If not, you should definitely try it out! You can interact with the model via your web browser, but it’s more enjoyable to experiment and create something yourself. Hence, I decided to code a Telegram bot that connects to the OpenAI API.

The main features of the bot include:

  • Using the ChatGPT model with the /chat command.
  • Generating images using the DALL-E model with the /pic command.
  • Transcribing voice and audio messages to text, which is the most important feature for me.

Nowadays, sending voice messages is quite common, which can be frustrating for some people. I prefer reading rather than listening to long audio messages.

OpenAI has published detailed API documentation on their website.

If you’ve never experimented with Telegram bots before, I highly recommend starting with this tutorial on creating your first bot.

The tutorial is self-explanatory, so I won’t delve too deep into it here. You’ll need to create a bot API token first – to do this, contact @BotFather in Telegram. You’ll also need OpenAI API tokens (the process of obtaining them is explained in the OpenAI API documentation).

There’s a dedicated Python OpenAI library, but I prefer using the Python Requests library when possible. It’s more convenient since, once you learn how to use Python Requests, you can adapt it for any API.

Without further ado, I invite you to check out the code on my GitHub. Feel free to use it and make your own adjustments as needed.