How I created a Telegram bot that helps you cook from any product

The background to the creation of the “CyberChiefCooker” bot

The idea for creating “CyberChiefCooker” came to me during my student years, when I often found myself in a situation where I needed to cook, and only had a selection of random products at hand. The life of a student was busy and limited in terms of finances, so it was often necessary to make do with what was in the refrigerator. One day I caught myself thinking that I didn’t know at all what could be prepared from these leftover products, and this thought became the beginning of the path to creating a bot.

I discussed similar problems with my friends and classmates, and we often joked that it would be nice to have a “smart assistant” who could immediately tell what could be cooked. These conversations and personal situations inspired me to create CyberChiefCooker, a bot that can help people with meal ideas using available ingredients.

Creating a smart bot “CyberChiefCooker” to generate recipes: how localization improves user experience

This idea, which originated during my student years, transformed into a desire to provide users with a convenient way to use existing products to prepare various dishes. With increasing interest in artificial intelligence technologies, it has become clear that many tasks related to food preparation can be greatly simplified and automated. We wanted to create a tool that would be accessible and useful to everyone, regardless of their culinary skills, and thus “CyberChiefCooker” was born – a bot that can adapt to each user and his preferences.

Every day, artificial intelligence technologies penetrate various aspects of our lives, and Telegram bots are no exception. Today I will talk about the process of creating the CyberChiefCooker bot – a unique assistant that helps users prepare a dish based on the products they have, suggesting a suitable recipe. I will pay special attention to how localization support was implemented to improve the user experience and make the bot interface more convenient and accessible to users from different countries.

Introduction to the functionality of the “CyberChiefCooker” bot

CyberChiefCooker is a powerful Telegram bot that generates recipes using products that the user has. You can send your grocery list via text, voice message, or even a photo. The bot will recognize products and offer recipes of varying degrees of complexity. To work with the bot, both text and voice input are used, which allows you to significantly expand the audience of users, making interaction with the bot as intuitive and simple as possible.

A special feature of this bot is the ability to select the difficulty level of a recipe depending on the number of available ingredients and the user’s skills. Thanks to artificial intelligence technology and localization into different languages, CyberChiefCooker provides a convenient and intuitive interface for both Russian and English speaking users. Localization support makes the bot more versatile, which allows it to gain popularity in different countries and cultures.

Localization as the basis for a user-friendly interface

One of the key aspects that CyberChiefCooker has implemented is localization support. I understand that for many users it is important that the interface be available in their native language in order to feel comfortable and confident when interacting with the bot. Therefore, a system for translating messages and buttons was developed to tailor the bot to user preferences and create a sense of personalized experience.

The implementation is based on the `translate.py` file, which contains the `localize()` function. This function takes two arguments: message text and language. Depending on the language, it returns either the translated string or the original text if there is no translation. This makes it easy to add new languages ​​and makes the translation system flexible and easily customizable.

Example of using the localization function:

from translate import localize

message = localize("Choose your language:", "ru")
print(message)  # Вывод: "Выберите ваш язык:"

As you can see from the example, using the `localize()` function, the bot adapts its messages to the language selected by the user. This is especially important for creating a convenient and understandable interface for users in different countries, which allows each user to feel at home when working with the bot.

Main menu in two languages

The main bot menu is in English

The main bot menu is in English

The main menu of the bot is in Russian

The main menu of the bot is in Russian

Interface examples and usability

To improve user experience, the bot also supports inline keyboards, which are localized depending on the selected language. For example, after activating the command, the user sees the “Main Menu” or “Buy recipe units” buttons, depending on the selected language. This allows you to navigate the bot’s capabilities as efficiently as possible without experiencing translation difficulties.

Keyboards are generated by the `generate_inline_keyboard()` function, which creates buttons with the desired text depending on the user's selected language. Example of buttons for the main menu:

from utils import generate_inline_keyboard

buttons = generate_inline_keyboard([
    [(localize("Main menu", language), "Main_menu")],
    [(localize("Buy recipe units", language), "Pay_recipe_units")]
])
The keyboard is displayed in case of zero balance

The keyboard is displayed in case of zero balance

This system allows you to flexibly adapt the bot's interface, providing users with a feeling of care and individual attention, which in turn increases their trust and satisfaction. Thanks to this approach, users can easily and quickly find the functions they need without being distracted by language barriers.

Recipe generation: from product recognition to difficulty level selection

The process of using the CyberChiefCooker bot is as follows:

  1. The user sends a list of products (by text, voice message or photo).

  2. The bot recognizes the products and prompts the user to select the difficulty level of the recipe.

  3. The user selects a difficulty level, and the bot provides a detailed recipe.

To recognize products, the `utils.py` module is used, which is responsible for analyzing images and voice messages. For example, the `recognize_products_in_image_ru()` function allows you to recognize products in an image using the Pixtral neural network model. This neural network model is trained specifically to effectively recognize food items in user-supplied images. Pixtral is a powerful neural network designed to work with visual data, allowing it to identify different types of foods and ingredients with high accuracy.

An example of a function for recognizing products in an image in Russian:

async def recognize_products_in_image_ru(image_path, api_key):
    base64_image = encode_image(image_path)
    if not base64_image:
        return None

    client = Mistral(api_key=api_key)
    messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Перечисли только продукты питания на этом изображении на русском языке через запятую. Не перечисляй объекты не являющиеся продуктами питания."
                },
                {
                    "type": "image_url",
                    "image_url": f"data:image/jpeg;base64,{base64_image}"
                }
            ]
        }
    ]
    chat_response = await client.chat.complete_async(model="pixtral-12b-2409", messages=messages)
    response_text = chat_response.choices[0].message.content
    return response_text.strip()

Voice messages are transcribed using the “whisper-large-v3-turbo” model, which is used for accurate speech recognition. This neural network model allows you to convert voice input into text with high accuracy, which greatly simplifies interaction with the bot for those users who prefer voice commands. After successful transcription, the bot analyzes the list of products and moves on to the next step – selecting the difficulty level of the recipe.

def transcribe_audio(filename, api_key):
    with open(filename, "rb") as file:
        response = requests.post(
            "https://api.groq.com/openai/v1/audio/transcriptions",
            headers={"Authorization": f"Bearer {api_key}"},
            files={"file": file},
            data={"model": "whisper-large-v3-turbo", "language": "ru"}
        )

    if response.status_code == 200:
        return response.json().get("text", "")
    else:
        print(f"Ошибка транскрипции: {response.status_code}")
        return None

An example of the result of the bot's work

After the user has chosen the difficulty level, the bot generates a recipe. For example, when you select the difficulty level “Medium” and send a list of products such as: Chicken fillet, potatoes, garlic, the bot may offer the following recipe:

Recipe output screen 1

Recipe output screen 1

To implement localization in the bot, a dictionary structure was used, in which the keys are the original messages in English, and the values ​​are translations into Russian.

`translate.py` implements the following structure:

translations = {
    "ru": {
        "Choose your language:": "Выберите ваш язык:",
        "Main menu": "Главное меню",
        "Buy recipe units": "Купить рецепт-юниты",
        # ... и другие переводы
    },
    "en": {
        "Choose your language:": "Choose your language:",
        "Main menu": "Main menu",
        "Buy recipe units": "Buy recipe units",
        # ... и другие переводы
    }
}

The system is quite simple, but allows you to easily add new languages ​​and messages. If you need to add a new language, just create a new dictionary inside `translations`, adding the corresponding translations there. Thanks to such a flexible system, the bot can easily scale and adapt to the needs of users in different countries, offering them a familiar and understandable interface.

Technology stack used

When developing CyberChiefCooker, a modern technology stack was used, which includes various technologies and tools for data processing, machine learning and creating a user-friendly interface. Main technologies used in the project:

  • Python — the main programming language used to write bot code and its logic.

  • AIogram — a library for developing Telegram bots in Python, which provides easy integration with the Telegram API and message management.

  • Groq API and Mistral — for generating texts and interacting with artificial intelligence models, such as Pixtral for image analysis and Whisper for speech recognition.

  • Pixtral — a neural network model trained to recognize objects in images. Used to identify products in photos submitted by users.

  • Whisper is a speech recognition model used to convert voice messages into text.

  • Redis — used as a repository of bot states, which provides convenient management of user sessions.

  • Docker — for containerization and deployment of the bot, which allows you to maintain its functionality on different platforms and environments.

This approach allows you to create a reliable and scalable solution that is easy to develop and maintain, providing high performance and convenience

Conclusion

Localization is one of the key aspects that helps make the CyberChiefCooker bot more convenient and attractive to users. Thanks to the ability to choose a language and adapt the interface, the bot offers users an intuitive way to interact, regardless of their place of residence. This adaptability makes the bot unique and sets it apart from others, providing a truly personalized experience.

If you're interested in learning more about the technical details or how you can integrate localization into your project, leave your questions in the comments. We will be happy to share our experience and solutions!

Try the bot right now using the link: Telegram: Contact @CyberChiefCooker_bot

Similar Posts

Leave a Reply

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