This project provides an HTTP server built using Flask that integrates with the Telegram API through the Telethon library. The application allows for authentication via phone number, sending messages to yourself or others, and managing Telegram sessions programmatically.
- Authentication by Phone Number: Initiates the authentication process by sending a code to the provided phone number.
- Authentication by Code: Completes the authentication process by verifying the code received on the phone.
- Send Message: Sends a message to a specified phone number.
- Send Message to Self: Sends a message to yourself on Telegram.
- Disconnect: Logs out and disconnects the Telegram client.
- Get Clients: Retrieves a list of active clients and their associated phone numbers.
- Ping: Simple endpoint to verify the server is running.
- Python 3.7+
- Telegram API credentials (
api_id
,api_hash
) - Flask
- Telethon
-
Clone this repository:
git clone https://github.com/iscritic/telegramapi.git cd telegramapi
-
Install the required packages:
pip install -r requirements.txt
-
Configure your Telegram API credentials by creating a
config.py
file:# config.py api_id = 'YOUR_API_ID' api_hash = 'YOUR_API_HASH'
-
Run the Flask application:
python app.py
Endpoint: /auth_by_phone
-
Method:
POST
-
Request Body:
{ "phone": "PHONE_NUMBER" }
-
Response:
{ "isOK": true, "client_id": "UNIQUE_CLIENT_ID" }
Endpoint: /auth_by_code
-
Method:
POST
-
Request Body:
{ "client_id": "UNIQUE_CLIENT_ID", "code": "RECEIVED_CODE" }
-
Response:
{ "isOK": true }
Endpoint: /send_message
-
Method:
POST
-
Request Body:
{ "client_id": "UNIQUE_CLIENT_ID", "to_phone": "RECIPIENT_PHONE_NUMBER", "text": "YOUR_MESSAGE" }
-
Response:
{ "isOK": true }
Endpoint: /send_message_to_self
-
Method:
POST
-
Request Body:
{ "client_id": "UNIQUE_CLIENT_ID", "text": "YOUR_MESSAGE" }
-
Response:
{ "isOK": true }
Endpoint: /get_clients
-
Method:
GET
-
Response:
{ "clients": [ { "client_id": "UNIQUE_CLIENT_ID", "phone_number": "PHONE_NUMBER" } ] }
Endpoint: /disconnect
-
Method:
POST
-
Request Body:
{ "client_id": "UNIQUE_CLIENT_ID" }
-
Response:
{ "isOK": true }
Endpoint: /ping
-
Method:
GET
-
Response:
{ "isOK": true, "message": "pong" }