Using the OpenAI Assistant as a Telegram Bot

Nikita Melashchenko · (2023-12) · guide

This guide will walk you through how to deploy an OpenAI Assistant as a Telegram bot using daohoangson/bubby, an open-source project that bridges the OpenAI Assistants API with the Telegram Bot API. Once set up, you can interact with your custom AI assistant directly from Telegram on your phone or desktop, making it a convenient tool for quick research queries, brainstorming and on-the-go productivity.

This guide was prepared for users such as students, researchers and academics without a technical background. It is intended to be a step-by-step tutorial that covers the entire process from creating a Telegram bot to deploying it on the cloud.

Prerequisites

Before you start, make sure you have the following:

Getting started

Step 1: Create a Telegram Bot via BotFather

In this step, you will create a new Telegram bot using BotFather. BotFather is Telegram’s official tool for creating and managing bots. It is itself a bot that you interact with through Telegram’s chat interface.

Here are the detailed steps:

  1. Open Telegram on your phone or desktop.

  2. In the search bar, search for @BotFather and open the chat with the verified BotFather account.

  3. Send the command /newbot to BotFather. This tells BotFather that you want to create a new bot.

  4. BotFather will ask you to choose a display name for your bot. Enter a name, for example My Research Assistant.

  5. BotFather will then ask you to choose a username for your bot. The username must end in bot, for example my_research_assistant_bot.

  6. After you provide a valid username, BotFather will create your bot and send you a message containing the bot token. The token looks something like 123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ. Copy this token and save it somewhere safe.

  7. You can optionally send /setdescription and /setabouttext to BotFather to add a description to your bot.

⚠️ Note: Keep your bot token secret. Anyone with the token can control your bot. Do not share it publicly or commit it to a repository.

After completing these steps, you should have a new Telegram bot with a bot token that you will use later to connect the bot to your OpenAI Assistant.

Step 2: Set up an OpenAI Assistant

In this step, you will set up an OpenAI Assistant for the project. An OpenAI Assistant is an AI-powered chatbot that can answer questions, provide explanations, and generate code in accordance with custom instructions and uploaded documents.

Here are the detailed steps:

  1. Navigate to platform.openai.com and log in to your account.

  2. Navigate to the Assistants tab on the left sidebar.

  3. Click on the Create button.

  4. Enter the name of the assistant, for example Research Assistant.

  5. Enter the instructions for the assistant. These instructions tell the assistant how to behave and what kind of responses to provide. For example: “You are a helpful research assistant. Answer questions clearly and provide references where possible.”

  6. Choose the model for the assistant. GPT-4o mini is recommended as it provides good quality responses at a lower cost.

  7. Toggle on Code Interpreter and/or Retrieval if you want to use these features. Code Interpreter allows the assistant to run code, while Retrieval allows it to search through uploaded documents.

  8. Upload files for the assistant if you want the assistant to use the contents of the files for code interpretation and/or retrieval.

  9. Click on the Save button.

  10. Note the id of the assistant somewhere. It will be displayed at the top of the assistant’s page and looks something like asst_abc123def456. You will need it later.

⚠️ Note: The choice of model affects both the quality and cost of responses. GPT-4o mini is recommended for most use cases as it balances quality and affordability.

After completing these steps, you should have an OpenAI Assistant set up and ready to be connected to your Telegram bot.

Step 3: Obtain an OpenAI API Key

In this step, you will obtain an OpenAI API key. An API key is a unique identifier that allows the bubby application to communicate with OpenAI’s servers on your behalf.

Here are the detailed steps:

  1. Navigate to platform.openai.com and log in to your account.

  2. Navigate to the API keys tab on the left sidebar.

  3. Click on the Create new secret key button.

  4. Enter a name for the API key, for example Telegram Bot Key.

  5. Click on the Create secret key button.

  6. Copy the key and save it somewhere safe. You will need it later.

⚠️ Note: You will not be able to see this key again after you close the window. Store it in a safe place. OpenAI API usage is billed based on the number of tokens processed. Monitor your usage at platform.openai.com to avoid unexpected charges.

After completing these steps, you should have an OpenAI API key that you will use to authenticate your Telegram bot with the OpenAI API.

Step 4: Set up AWS Credentials

In this step, you will set up AWS credentials. The bubby project deploys as a serverless function on AWS (Amazon Web Services), which means your bot runs in the cloud without you needing to manage a server.

Here are the detailed steps:

  1. Navigate to aws.amazon.com and create an account if you don’t have one. The AWS Free Tier is sufficient for running a personal Telegram bot.

  2. Once logged in, navigate to the IAM (Identity and Access Management) console by searching for IAM in the AWS search bar.

  3. In the IAM console, click on Users in the left sidebar, then click Create user. Enter a username, for example telegram-bot-deploy. On the permissions page, select Attach policies directly and attach the AdministratorAccess policy. Click through to create the user.

  4. After creating the user, click on the user, navigate to the Security credentials tab, and click Create access key. Select Command Line Interface (CLI) as the use case. Copy both the Access Key ID and Secret Access Key and save them somewhere safe.

  5. Install the AWS CLI on your machine. You can download it from the official website. After installation, open VS Code and open the integrated terminal by navigating to Terminal > New Terminal in the menu bar. In the terminal, run:

aws configure

Enter your Access Key ID, Secret Access Key, choose a default region (for example us-east-1), and set the default output format to json.

⚠️ Note: The AWS Free Tier includes 1 million AWS Lambda requests per month. A personal Telegram bot will typically stay well within this limit, so you should not incur any AWS charges for normal use.

After completing these steps, you should have AWS credentials configured on your machine, which will allow the bubby project to deploy your bot to the cloud.

Step 5: Clone and Install the bubby Project

In this step, you will download the bubby project and install its dependencies using VS Code. Bubby is a TypeScript application that connects your OpenAI Assistant to Telegram.

Here are the detailed steps:

  1. Launch VS Code on your machine.

  2. In the VS Code interface, open the Command Palette by pressing Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS). Type Git: Clone and select it. This option allows you to create a new project in VS Code by cloning a repository from GitHub.

  3. In the repository URL field, enter: https://github.com/daohoangson/bubby.git.

  4. Choose the directory on your local machine where you want to clone the repository. This is where the copy of the project will be stored on your computer.

  5. VS Code will clone the repository and ask if you want to open it. Click Open to open the project in VS Code. You should now see the project files in the VS Code Explorer panel on the left.

  6. Open the integrated terminal in VS Code by navigating to Terminal > New Terminal in the menu bar, or by pressing Ctrl+` (backtick). Install pnpm, the package manager used by the project. If you do not have pnpm installed, run:

npm install -g pnpm
  1. Install the project dependencies by running:
pnpm install

⚠️ Note: The bubby project requires pnpm, not npm or yarn. Make sure to install pnpm first if you don’t already have it.

After completing these steps, you should have the bubby project open in VS Code with all its dependencies installed. You can now navigate the project files in the Explorer panel and use the integrated terminal for the remaining steps.

Step 6: Configure the Secrets

In this step, you will configure the secrets that the bubby project needs to connect your OpenAI Assistant to your Telegram bot. The project uses SST (Serverless Stack) to manage these secrets securely.

Here are the detailed steps:

  1. In VS Code, open the integrated terminal if it is not already open (Terminal > New Terminal). Make sure you are in the bubby project directory.

  2. Set your OpenAI API key (from Step 3):

pnpm sst secrets set OPENAI_API_KEY your-api-key-here
  1. Set your OpenAI Assistant ID (from Step 2):
pnpm sst secrets set OPENAI_ASSISTANT_ID your-assistant-id-here
  1. Set your Telegram bot token (from Step 1):
pnpm sst secrets set TELEGRAM_BOT_TOKEN your-bot-token-here
  1. Set your Telegram admin IDs. This is a comma-separated list of Telegram user IDs that are allowed to start conversations with the bot. To find your Telegram user ID, open Telegram and message @userinfobot — it will reply with your user ID.
pnpm sst secrets set TELEGRAM_ADMIN_IDS your-user-id-here

⚠️ Note: The TELEGRAM_ADMIN_IDS setting is a security feature. Only users whose Telegram IDs are listed will be able to start conversations with the bot. However, if you add the bot to a group chat, it will respond to all participants in that group.

After completing these steps, you should have all four secrets configured. The bubby project will use these to authenticate with OpenAI and Telegram when the bot is running.

Step 7: Run the Bot Locally

In this step, you will run the bot locally to test that everything is working correctly before deploying it to the cloud.

Here are the detailed steps:

  1. In VS Code, open the integrated terminal if it is not already open. Make sure you are in the bubby project directory.

  2. Start the local development server:

pnpm dev

SST will start a local development environment that connects to AWS. The first time you run this, it may take a few minutes to set up the necessary infrastructure. You will see the progress in the VS Code terminal.

  1. Once the development server is running, open Telegram and find your bot by searching for the username you created in Step 1.

  2. Send a message to the bot. It should respond using your OpenAI Assistant’s instructions and capabilities.

⚠️ Note: The local development server must remain running in the VS Code terminal for the bot to respond. If you close VS Code or stop the server, the bot will stop responding until you either run the dev server again or deploy to production.

After completing these steps, you should have a working Telegram bot that responds to your messages using your OpenAI Assistant. You can now test different types of queries and make sure the assistant behaves as expected.

Step 8: Deploy to Production

In this step, you will deploy the bot to production so that it runs continuously in the cloud without needing your local machine to be on.

Here are the detailed steps:

  1. In VS Code, open the integrated terminal if it is not already open. Make sure you are in the bubby project directory.

  2. Deploy the bot to production:

pnpm run deploy --stage prod
  1. Wait for the deployment to complete. SST will output the deployment progress in the VS Code terminal. This may take a few minutes.

  2. Once the deployment is complete, open Telegram and send a message to your bot to confirm it is working. The bot should respond without the local development server running.

⚠️ Note: The production deployment will remain running on AWS until you remove it. If you no longer need the bot, you can remove the deployment by running pnpm sst remove --stage prod in the VS Code terminal.

After completing these steps, you should have your OpenAI Assistant running as a Telegram bot in the cloud. The bot will be available around the clock and will respond to messages from authorised users.

Final notes

In addition to text conversations, bubby supports several other capabilities powered by OpenAI:

Regarding costs, OpenAI API usage is billed per token. For a personal research bot with moderate usage, the costs are generally modest. AWS Lambda costs should stay within the free tier for most personal use cases. You can monitor your OpenAI usage at platform.openai.com and your AWS usage at aws.amazon.com.

For updates, bug reports, and feature requests, visit the bubby GitHub repository.

Conclusion

That’s it! You now have an OpenAI Assistant running as a Telegram bot. This means you can interact with your custom AI assistant from anywhere on your phone, making it a convenient tool for quick research queries, brainstorming, and on-the-go productivity.

Also see

My Research Workflow with OpenAI Assistants

Using the OpenAI Assistant in a Jupyter Notebook with PyCharm