-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_client.py
More file actions
165 lines (140 loc) · 5.39 KB
/
example_client.py
File metadata and controls
165 lines (140 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Example client for testing Telegram Service API
"""
import httpx
import asyncio
import json
from typing import Optional
class TelegramServiceClient:
"""Client for Telegram Service API"""
def __init__(self, base_url: str, api_key: str):
self.base_url = base_url
self.api_key = api_key
self.headers = {
"X-API-Key": api_key,
"Content-Type": "application/json"
}
async def request_qr(self, agent_id: int) -> dict:
"""Request QR code for login"""
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/api/telegram/request-qr",
json={"agent_id": agent_id},
headers=self.headers
)
return response.json()
async def verify_code(self, session_id: str, code: str) -> dict:
"""Verify phone code"""
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/api/telegram/verify-code",
json={
"session_id": session_id,
"code": code
},
headers=self.headers
)
return response.json()
async def verify_password(self, session_id: str, password: str) -> dict:
"""Verify 2FA password"""
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/api/telegram/verify-password",
json={
"session_id": session_id,
"password": password
},
headers=self.headers
)
return response.json()
async def get_status(self, session_id: str) -> dict:
"""Get session status"""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/api/telegram/status/{session_id}",
headers=self.headers
)
return response.json()
async def send_message(
self,
session_id: str,
chat_id: int,
message: str,
reply_to: Optional[int] = None
) -> dict:
"""Send a message"""
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/api/telegram/send-message",
json={
"session_id": session_id,
"chat_id": chat_id,
"message": message,
"reply_to": reply_to
},
headers=self.headers
)
return response.json()
async def disconnect(self, session_id: str) -> dict:
"""Disconnect session"""
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/api/telegram/disconnect",
json={"session_id": session_id},
headers=self.headers
)
return response.json()
async def main():
"""Example usage"""
# Configuration
BASE_URL = "http://localhost:8000"
API_KEY = "your-api-secret-key"
AGENT_ID = 123
# Create client
client = TelegramServiceClient(BASE_URL, API_KEY)
print("🚀 Telegram Service Client Example")
print("=" * 50)
print()
# Step 1: Request QR Code
print("📱 Step 1: Requesting QR code...")
qr_response = await client.request_qr(AGENT_ID)
if qr_response.get("success"):
session_id = qr_response["session_id"]
print(f"✓ Session ID: {session_id}")
print(f"✓ QR Code (base64): {qr_response['qr_code'][:50]}...")
print(f"✓ Expires in: {qr_response['expires_in']} seconds")
print()
# You would display the QR code to the user here
# They scan it with their Telegram app
# Step 2: After QR scan or phone code entry
print("📝 Step 2: Waiting for verification...")
print("(In real scenario, user scans QR or enters code)")
print()
# If using phone code instead:
# code = input("Enter verification code: ")
# verify_response = await client.verify_code(session_id, code)
# print(json.dumps(verify_response, indent=2))
# Step 3: Check status
print("📊 Step 3: Checking session status...")
status = await client.get_status(session_id)
print(json.dumps(status, indent=2))
print()
# Step 4: Send message (if connected)
if status.get("connected"):
print("💬 Step 4: Sending test message...")
message_response = await client.send_message(
session_id=session_id,
chat_id=123456789, # Replace with actual chat ID
message="سلام، این یک پیام تست است"
)
print(json.dumps(message_response, indent=2))
print()
# Step 5: Disconnect (optional)
# print("🔌 Step 5: Disconnecting...")
# disconnect_response = await client.disconnect(session_id)
# print(json.dumps(disconnect_response, indent=2))
else:
print("❌ Failed to request QR code")
print(json.dumps(qr_response, indent=2))
if __name__ == "__main__":
asyncio.run(main())