Skip to main content

Overview

The Messages API allows administrators to proactively send messages to Telegram conversations. This is useful for:
  • Broadcasting announcements to users
  • Initiating conversations without waiting for user input
  • Sending notifications or updates
  • Testing bot responses
This endpoint requires authentication. Include a valid JWT token in the Authorization header.

Send Message to Conversation

curl -X POST http://localhost:8080/api/conversations/123456789/messages \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "Hello! This is a message from the admin panel."
  }'
Sends a message from the admin to a specific Telegram conversation.

Endpoint

POST /api/conversations/{id}/messages

Path Parameters

id
string
required
The conversation ID (Telegram chat ID) to send the message toExample: 123456789

Headers

Authorization
string
required
Bearer token for authentication
Authorization: Bearer <your-jwt-token>
Content-Type
string
required
Must be application/json

Request Body

content
string
required
The message text to send to the Telegram conversation

Request Example

{
  "content": "Hello! This is a proactive message from the admin panel. How can we help you today?"
}

Response

On success, returns an empty response body with HTTP 200 status.

Success Response (200 OK)

HTTP/1.1 200 OK
No response body is returned. The message has been successfully sent to the Telegram chat.
The message will be delivered to the Telegram user in real-time via the bot. The message is also stored in the database and will appear in the conversation history with direction OUTGOING.

Error Responses

Returned when the message content is empty or invalid.
{
  "message": "Validation Failed",
  "details": {
    "content": "Content cannot be blank"
  }
}
Returned when the request body is not valid JSON.
{
  "message": "Malformed Request",
  "details": "Required request body is missing or unreadable."
}
Returned when authentication token is missing or expired.
{
  "message": "Authentication Failed",
  "details": "JWT token is missing or invalid"
}
Returned when the specified conversation ID doesn’t exist.
{
  "message": "Resource Not Found",
  "details": "Conversation with ID '123456789' could not be found."
}

Message Flow

When you send a message via this endpoint:
1

API Receives Request

Your authenticated request is validated and processed
2

Message Stored

The message is saved to the database with direction OUTGOING
3

Telegram Delivery

The message is sent to the Telegram user via the Bot API
4

User Receives Message

The Telegram user sees your message in their chat

Complete Example: Send Message Workflow

Here’s a complete workflow showing how to authenticate and send a message:
#!/bin/bash

# Step 1: Register (if needed)
curl -X POST http://localhost:8080/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Admin User",
    "email": "[email protected]",
    "password": "securepass123"
  }'

# Step 2: Login and capture token
TOKEN=$(curl -s -X POST http://localhost:8080/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "securepass123"
  }' | jq -r '.token')

echo "Token: $TOKEN"

# Step 3: List conversations to get IDs
echo "\nAvailable conversations:"
curl -s -X GET http://localhost:8080/api/conversations \
  -H "Authorization: Bearer $TOKEN" \
  | jq '.'

# Step 4: Send message to specific conversation
CONV_ID="123456789"  # Replace with actual conversation ID

curl -X POST http://localhost:8080/api/conversations/$CONV_ID/messages \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "Hello from the admin panel!"
  }'

echo "\nMessage sent successfully!"

Use Cases

Broadcast Announcements

Send important updates to all or specific users

Proactive Support

Reach out to users without waiting for them to message first

Notifications

Alert users about events, deadlines, or changes

Testing

Test bot responses and conversation flows

Best Practices

Keep messages concise: While Telegram supports messages up to 4096 characters, shorter messages tend to have better engagement.
Avoid spam: Sending too many unsolicited messages may lead to users blocking your bot. Use this feature responsibly.
Message delivery: Messages are sent asynchronously to Telegram. A successful API response (200 OK) means the message was accepted and queued for delivery, not that it was necessarily delivered to the user.

Source Code References

  • Controller: ConversationController.java:72 (send message endpoint)
  • DTO: SendMessageRequest.java:5 (request structure)
  • Domain Model: Message.java:23 (message creation logic)

Next Steps

View Conversations

List conversations and view message history

Authentication

Learn about JWT token authentication

Build docs developers (and LLMs) love