Welcome to the TS ChatGPT Telegram Bot! π€π¬
Imagine having a friendly AI companion right in your Telegram app, ready to chat with you about anything. Have your English Teacher π©π½βπ«, Rubber duck π¦, Story Teller, Language Translator or even your personal weather forecaster βοΈ right in your pocket!
This repository contains the source code for a Telegram bot powered by ChatGPT built with Node.js (Express.js). It's seamlessly integrated with OpenAI Streams
, compatible with Tool choices
, allowing users to interact with an AI-powered chatbot directly from their Telegram app. The bot supports both voice and text communication πβ¨
Additionally, context information is stored in a Redis cache
, ensuring smooth and continuous conversations with the bot.



Before diving in, ensure you have the following installed:
- Docker
- Node.js (v21) π
Let's get this bot up and running in no time! Here's what you need to do:
Clone the repository:
git clone https://github.com/inikki/ts-chatgpt-telegram-bot.git
cd ts-chatgpt-telegram-bot
npm install
Copy env example file. We will update envs with your variables:
cp .env.example .env
- Register https://platform.openai.com/
- Create new secret key in API keys section and store in .env here
OPEN_AI_KEY
- You will need some credits in your account.
Use Makefile to run:
make local

- Download and open your Telegram application on your mobile device or desktop. Search for @BotFather in the Telegram search bar.
- Click on the BotFather account and click the "Start" button to begin the conversation.
- In the chat with BotFather, type or click on /newbot and hit Send.
- Follow Instructions. You will be prompted to provide a name for your bot.
- Once you've successfully created the bot, you will receive a message with bot token. Keep your token secure and store it safely, it can be used by anyone to control your bot.
- Copy your token to .env
TELEGRAM_BOT_TOKEN
.
We have two options how to use telegram. We can use webhooks or long polling (but not both):
Set env variable LONG_POLLING_FLAG=true
(default).
This will start bot and poll messages.
docker-compose up
or when you also need to rebuild image:
docker-compose build
docker-compose up
To check incoming messages, you can run make get-updates
- You will need domain or a public url with SSL certificate. Save it in env
SSL_URL
- Create secret token to secure communication
TELEGRAM_WEBHOOK_SECRET_TOKEN
. Only characters A-Z, a-z, 0-9, _ and - are allowed. - Run
make set-webhook
- Make sure, variable
LONG_POLLING_FLAG
is not defined. Remove it from .env's.
docker-compose up
or when you need to rebuild image:
docker-compose build
docker-compose up
Send message with curl (or postman) if you don't have app deployed:
curl --location 'http://localhost:7999/telegram-webhook' \
--header 'x-telegram-bot-api-secret-token: your-secret-token' \
--header 'Content-Type: application/json' \
--data '{
"message": {
"chat": {
"first_name": "Nikoleta",
"id": your-chat-id,
"type": "private"
},
"date": 1710178201,
"entities": [
{
"length": 6,
"offset": 0,
"type": "bot_command"
}
],
"from": {
"first_name": "Nikoleta",
"id": your-chat-id,
"is_bot": false,
"language_code": "en"
},
"message_id": 440,
"text": "Hi, this is my test question."
},
"update_id": 902460408
}'
- To keep it simple, this is only working with
long polling
option, so make sure, this .env is set. (Using webhooks requires additional configuration such as setting up an ingress controller, cert manager, and issuer.)
Setting up the bot on Kubernetes is easy! Follow these steps:
-
Create a Kubernetes Cluster: Set up a Kubernetes cluster, set up Containter registry.
-
Push image to your registry
docker build -t your-container-registry/telegram-express-app .
docker push your-container-registry/telegram-express-app:openai
- Deploy the Bot: Navigate to the k8s directory, edit deployment.yaml with your image and env variables and apply the deployment configurations:
cd k8s
kubectl apply deployment.yaml
kubectl apply service.yaml
kubectl apply redis/service.yaml
kubectl apply redis/statefulSet.yaml
- Implement image generation feature
- Implement reaction system to identify inadequate responses π
- Summarize long conversations to fit within the context token limitation
- Integrate embeddings functionality
- Set up Pulumi for streamlined deployment, including teardown processes π
- Tiktoken