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:
-
A computer with an internet connection.
-
A Telegram account. If you don’t have one, you can download Telegram from the official website.
-
An OpenAI account with API access. You can create one at platform.openai.com.
-
An AWS (Amazon Web Services) account. You can create a free-tier account at aws.amazon.com.
-
Visual Studio Code (VS Code) installed on your machine. If you don’t have VS Code installed, you can download it from the official website.
-
Node.js (version 18 or later) installed on your machine. If you don’t have Node.js installed, you can download it from the official website.
-
Basic knowledge of how to use a terminal.
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:
-
Open Telegram on your phone or desktop.
-
In the search bar, search for
@BotFatherand open the chat with the verified BotFather account. -
Send the command
/newbotto BotFather. This tells BotFather that you want to create a new bot. -
BotFather will ask you to choose a display name for your bot. Enter a name, for example
My Research Assistant. -
BotFather will then ask you to choose a username for your bot. The username must end in
bot, for examplemy_research_assistant_bot. -
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. -
You can optionally send
/setdescriptionand/setabouttextto 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:
-
Navigate to platform.openai.com and log in to your account.
-
Navigate to the
Assistantstab on the left sidebar. -
Click on the
Createbutton. -
Enter the name of the assistant, for example
Research Assistant. -
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.”
-
Choose the model for the assistant. GPT-4o mini is recommended as it provides good quality responses at a lower cost.
-
Toggle on
Code Interpreterand/orRetrievalif you want to use these features. Code Interpreter allows the assistant to run code, while Retrieval allows it to search through uploaded documents. -
Upload files for the assistant if you want the assistant to use the contents of the files for code interpretation and/or retrieval.
-
Click on the
Savebutton. -
Note the
idof the assistant somewhere. It will be displayed at the top of the assistant’s page and looks something likeasst_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:
-
Navigate to platform.openai.com and log in to your account.
-
Navigate to the
API keystab on the left sidebar. -
Click on the
Create new secret keybutton. -
Enter a name for the API key, for example
Telegram Bot Key. -
Click on the
Create secret keybutton. -
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:
-
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.
-
Once logged in, navigate to the IAM (Identity and Access Management) console by searching for
IAMin the AWS search bar. -
In the IAM console, click on
Usersin the left sidebar, then clickCreate user. Enter a username, for exampletelegram-bot-deploy. On the permissions page, selectAttach policies directlyand attach theAdministratorAccesspolicy. Click through to create the user. -
After creating the user, click on the user, navigate to the
Security credentialstab, and clickCreate access key. SelectCommand Line Interface (CLI)as the use case. Copy both theAccess Key IDandSecret Access Keyand save them somewhere safe. -
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 Terminalin 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:
-
Launch VS Code on your machine.
-
In the VS Code interface, open the Command Palette by pressing
Ctrl+Shift+P(Windows/Linux) orCmd+Shift+P(macOS). TypeGit: Cloneand select it. This option allows you to create a new project in VS Code by cloning a repository from GitHub. -
In the repository URL field, enter:
https://github.com/daohoangson/bubby.git. -
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.
-
VS Code will clone the repository and ask if you want to open it. Click
Opento open the project in VS Code. You should now see the project files in the VS Code Explorer panel on the left. -
Open the integrated terminal in VS Code by navigating to
Terminal>New Terminalin the menu bar, or by pressingCtrl+`(backtick). Install pnpm, the package manager used by the project. If you do not have pnpm installed, run:
npm install -g pnpm
- 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:
-
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. -
Set your OpenAI API key (from Step 3):
pnpm sst secrets set OPENAI_API_KEY your-api-key-here
- Set your OpenAI Assistant ID (from Step 2):
pnpm sst secrets set OPENAI_ASSISTANT_ID your-assistant-id-here
- Set your Telegram bot token (from Step 1):
pnpm sst secrets set TELEGRAM_BOT_TOKEN your-bot-token-here
- 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:
-
In VS Code, open the integrated terminal if it is not already open. Make sure you are in the bubby project directory.
-
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.
-
Once the development server is running, open Telegram and find your bot by searching for the username you created in Step 1.
-
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:
-
In VS Code, open the integrated terminal if it is not already open. Make sure you are in the bubby project directory.
-
Deploy the bot to production:
pnpm run deploy --stage prod
-
Wait for the deployment to complete. SST will output the deployment progress in the VS Code terminal. This may take a few minutes.
-
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:
- Image analysis — Send an image to the bot and it will use vision capabilities to describe and interpret the image.
- Image generation — Ask the bot to create an image and it will generate one using DALL-E 3.
- Speech-to-text — Send a voice message and the bot will transcribe it.
- Text-to-speech — The bot can convert text responses to audio.
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