Telegram bots are widely used for task automation, customer support, content generation, and AI assistant development. By connecting Serverspace GPT API, you can add artificial intelligence capabilities to your bot and receive responses from modern language models directly within Telegram.
Serverspace GPT API uses an OpenAI-compatible request format, allowing integration to be completed in just a few minutes and implemented in virtually any programming language.
Requirements Before You Start
Before configuring the integration, make sure you have:
- A Serverspace account;
- An activated GPT API service;
- An API key for accessing the service;
- A Telegram bot created via BotFather;
- A server or local environment with Python installed;
- Basic Python knowledge.
The following API endpoint will be used:
Step 1. Create a Telegram Bot
Open Telegram and find the BotFather bot:
You can create a bot either through the mini application or via chat.
Using the Mini Application:
Enter the bot name, description (optional), and choose a username for the bot:
After entering this information, your first bot will be created. In the menu, you will find the API token and many additional settings:
Once completed, you will receive an access token similar to:
Save it — it will be required to connect to the Telegram API.
Step 2. Obtain a Serverspace GPT API Key
In the Serverspace control panel, navigate to the GPT API section.
Open the API key management tab and copy the generated access key.
This key will be used to authorize requests to the GPT API.
Step 3. Install the Required Libraries
Install the dependencies:
Once the installation is complete, you can proceed with writing the bot code.
Step 4. Create the Telegram Bot Application
Create a file:
Add the following code:
from telegram import Update
from telegram.ext import Application, MessageHandler, ContextTypes, filters
TELEGRAM_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
GPT_API_KEY = "YOUR_SERVERSPACE_API_KEY"
API_URL = "https://gpt.serverspace.io/v1/chat/completions"
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_message = update.message.text
```
headers = {
"Authorization": f"Bearer {GPT_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": "gpt-5.2",
"messages": [
{
"role": "user",
"content": user_message
}
],
"temperature": 0.7,
"max_tokens": 500
}
response = requests.post(
API_URL,
headers=headers,
json=payload
)
result = response.json()
answer = result["choices"][0]["message"]["content"]
await update.message.reply_text(answer)
```
def main():
app = Application.builder().token(TELEGRAM_TOKEN).build()
```
app.add_handler(
MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)
)
app.run_polling()
```
if **name** == "**main**":
main()
This script receives messages from Telegram users, sends them to the GPT API, and returns the generated response back to the chat.
Step 5. Configure Your Credentials
Replace the following values:
GPT_API_KEY = "YOUR_SERVERSPACE_API_KEY"
with your Telegram bot token and Serverspace API key.
Step 6. Run the Bot
Execute the following command:
After startup, the bot will begin receiving messages and forwarding them to the GPT API.
Try sending any message to your Telegram bot — it will process the request using the AI model and return a response.
Example of a Direct GPT API Request
To better understand how the API works, consider the following cURL request example:
curl https://gpt.serverspace.io/v1/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer YOUR_API_KEY"
-d '{
"model": "gpt-5.2",
"messages": [
{
"role": "user",
"content": "Hello! Tell me about cloud technologies."
}
],
"temperature": 0.7,
"max_tokens": 500
}'
The API will return a JSON response containing the generated text.
How to Preserve Conversation History
By default, the GPT API does not store conversation history between requests.
If you need to preserve context, send previous messages together with each new request:
{
"role": "user",
"content": "Hello"
},
{
"role": "assistant",
"content": "Hello! How can I help you?"
},
{
"role": "user",
"content": "Tell me about virtual servers"
}
]
This way, the model will see previous messages and maintain a coherent conversation.
Security Recommendations
When using GPT API in production projects, it is recommended to:
- Never store API keys in publicly accessible locations;
- Use environment variables instead of hardcoding keys in source code;
- Restrict access to your server;
- Monitor token usage regularly;
- Rotate API keys periodically.
FAQ
Can I use OpenAI libraries?
Yes. Serverspace GPT API uses an OpenAI-compatible request format, so most existing integrations can be adapted with minimal changes.
Which endpoint should I use?
Use the following endpoint for sending requests:
How does authorization work?
Authorization is performed using a Bearer Token:
Can I use different models?
Yes. The Serverspace control panel provides access to various AI models from multiple providers.
Does the API automatically store conversation history?
No. GPT API is a stateless service and does not store conversation history. If conversation context is required, your application must send previous messages with every request.
Can I host the bot on a VPS?
Yes. A Telegram bot can run on a Serverspace cloud server, a local machine, a VPS, a Docker container, or any other environment that supports Python.
What types of projects is this integration suitable for?
Using GPT API, you can build:
- AI chatbots;
- Online consultants;
- Customer support systems;
- Content generators;
- Translation tools;
- Educational services;
- Internal corporate automation tools.
Conclusion
Integrating Serverspace GPT API with a Telegram bot allows you to quickly create an intelligent assistant for communicating with users. Thanks to the simple REST API, API key authorization, and OpenAI-compatible request format, the integration process takes only a few minutes and is suitable for both test projects and production environments.