Telegram Bot with OpenAI GPT-3.5-turbo connection

As the world becomes more and more digital, people are constantly looking for new ways to communicate with each other. One of the most popular messaging apps today is Telegram, which allows users to send each other messages, photos, and videos. But what if you could do more than just send messages? What if you could talk to a bot that uses artificial intelligence to understand and respond to your messages?

This is exactly what I intended to do when I created the ChatGPT telegram bot. This bot uses OpenAI GPT-3.5 to provide users with a more natural and human-like communication experience. But I didn’t stop there. I also included context, AI drawing, and Google scraping to make the bot even more powerful and versatile.

First I needed to get the necessary APIs and set the environment variables. The first API I needed was an OpenAI key that I got from the OpenAI website. With this key, I was able to access the GPT-3.5-turbo model, which is a powerful language model that can understand and respond to natural language input.

Next, I needed a key for Stable Diffusion, which provides AI painting services. This key allowed me to enable drawing in the bot, which means that users can send a Paint command with a description of the scene.

Next, I needed a model key that provides image-to-text conversion. With this key, I was able to enable the OCR functionality.

Once I had all the required APIs and environment variables set up, I started building the bot. The first step was to create a Telegram bot using the Telegram Bot API. This API provides an easy way to create and manage bots on the Telegram platform.

const replicate = new Replicate({ token: process.env.REPLICATE_KEY });
const openai = new OpenAIApi(new Configuration({ apiKey: process.env.OPENAI_KEY }));
const bot = new TelegramBot(process.env.TELEGRAM_KEY, { polling: true });
const translation = new TranslationServiceClient();

Next, I needed to integrate the OpenAI API into the bot. This uses node-telegram-api, which receives messages from users and sends them to the OpenAI API for processing. Once the API returned a response, I had to format it and send it back to the user via the Telegram bot.

const getText = async (prompt, temperature, max_tokens, chatId) => {
    try {
        const completion = await openai.createChatCompletion({
            model: "gpt-3.5-turbo",
            messages: [{ role: "user", content: prompt }],
            max_tokens: max_tokens,
            temperature: temperature,
        const response = completion?.data?.choices?.[0]?.message?.content;
        return response;
    } catch (e) {

To make the bot even more powerful, I decided to include context in the conversation. This means that the bot remembers previous messages and uses them to provide more personalized responses. For example, if a user asks the bot to recommend a restaurant, the bot will remember the user’s location and previous restaurant preferences to provide a more relevant recommendation.

        // Brain activity
        context[chatId] = context[chatId]?.slice(-CONTEXT_SIZE * premium(chatId)) ?? "";

        if ( {
            // visual hemisphere (left)
            visualToText(chatId, msg);
        msg.text = msg.text?.substring(0, MAX_LENGTH * premium(chatId));
        if (msgL.startsWith("google")) {
            textToGoogle(chatId, msg.text.slice(7), msg.from?.language_code);
        } else {
            if (msgL.startsWith("draw") || msgL.startsWith("paint")) {
                // visual hemisphere (left)
                textToVisual(chatId, msgL, msg.from?.language_code);
            } else {
                // audio hemisphere (right)
                textToText(chatId, msg);

With the integrated Stable Diffusion and Google scraping APIs, the bot has become even more versatile. Users can now ask the bot to search the web for information and get relevant results.

const textToVisual = async (chatId, text, language_code) => {
    if (text === "draw" || text === "paint") {
        // link between right and left hemisphere (painting)
        text = last[chatId];
    if ((language_code == "ru" && !text?.startsWith("draw"))) {
        text = await translate(text, "en");
    const photo = await getArt(
        text +
                ? ""
                : ", deep focus, highly detailed, digital painting, artstation, 4K, smooth, sharp focus, illustration")
    if (photo) {
        bot.sendPhoto(chatId, photo);

Building the ChatGPT telegram bot was a challenging but rewarding experience. By integrating several APIs and using context, stable AI drawing, and Google scraping, I was able to create a bot that provides users with a more natural and human communication experience. As AI technology continues to evolve, I look forward to seeing what new possibilities it will bring to messaging apps like Telegram.

Github here

Working example
To try it for free, use

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *