-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelegramManager-working.jsx
198 lines (186 loc) · 7.24 KB
/
TelegramManager-working.jsx
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import React, { useState, useEffect } from 'react'
import { useRouter } from 'next/navigation'
import { Loader2 } from 'lucide-react'
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
export default function TelegramManager() {
const router = useRouter()
const [apiId, setApiId] = useState('')
const [apiHash, setApiHash] = useState('')
const [phoneNumber, setPhoneNumber] = useState('')
const [extractType, setExtractType] = useState('groups')
const [validationCode, setValidationCode] = useState('')
const [showValidationInput, setShowValidationInput] = useState(false)
const [error, setError] = useState(null)
const [isLoading, setIsLoading] = useState(false)
const [codeRequestTime, setCodeRequestTime] = useState(null)
const [isAuthenticated, setIsAuthenticated] = useState(false)
useEffect(() => {
if (showValidationInput && codeRequestTime) {
const timer = setTimeout(() => {
setError('Code expired. Please request a new one.')
setShowValidationInput(false)
setValidationCode('')
setCodeRequestTime(null)
}, 120000) // 2 minutes expiration
return () => clearTimeout(timer)
}
}, [showValidationInput, codeRequestTime])
const validateInputs = () => {
if (!apiId || isNaN(apiId) || parseInt(apiId) <= 0) {
setError('API ID must be a valid positive number')
return false
}
if (!apiHash || !/^[a-f0-9]{32}$/.test(apiHash)) {
setError('API Hash should be a 32-character hexadecimal string')
return false
}
if (!phoneNumber || phoneNumber.trim() === '') {
setError('Please enter a valid phone number')
return false
}
return true
}
const handleSubmit = async (e) => {
e.preventDefault()
setError(null)
setIsLoading(true)
if (!validateInputs()) {
setIsLoading(false)
return
}
try {
const payload = {
apiId: parseInt(apiId),
apiHash,
phoneNumber: phoneNumber.trim(),
extractType,
validationCode: showValidationInput ? validationCode : undefined,
}
console.log('[DEBUG]: Submitting request with:', {
...payload,
apiHash: '******',
phoneNumber: '*******' + payload.phoneNumber.slice(-4),
validationCode: payload.validationCode ? '******' : undefined,
})
const response = await fetch('/api/telegram-extract', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
})
const data = await response.json()
console.log('[DEBUG]: Received response:', data)
if (!response.ok) {
throw new Error(data.error || 'Failed to process request')
}
if (data.requiresValidation) {
setShowValidationInput(true)
setCodeRequestTime(new Date())
setError(null)
alert('Please enter the validation code sent to your Telegram app.')
} else if (data.success) {
setIsAuthenticated(true)
setShowValidationInput(false)
if (data.data) {
alert(`Extracted ${data.data.length} ${extractType}`)
// Here you might want to save the data or redirect to a results page
router.push(`/${extractType}-list`)
} else {
alert('Authentication successful. You can now extract data.')
}
} else {
setError('An unexpected error occurred. Please try again.')
}
} catch (error) {
console.error('[ERROR]: Submit failed:', error)
setError(error.message)
} finally {
setIsLoading(false)
}
}
return (
<div className="min-h-screen bg-gray-100 flex flex-col items-center justify-center p-4">
<h1 className="text-4xl font-bold mb-8">Telegram Extractor</h1>
<Card className="w-full max-w-md">
<CardHeader>
<CardTitle>Telegram Extractor</CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="api-id">API ID</Label>
<Input
id="api-id"
value={apiId}
onChange={(e) => setApiId(e.target.value)}
required
disabled={isLoading || showValidationInput || isAuthenticated}
placeholder="Enter your API ID"
/>
</div>
<div className="space-y-2">
<Label htmlFor="api-hash">API Hash</Label>
<Input
id="api-hash"
value={apiHash}
onChange={(e) => setApiHash(e.target.value)}
required
disabled={isLoading || showValidationInput || isAuthenticated}
placeholder="Enter your API Hash"
/>
</div>
<div className="space-y-2">
<Label htmlFor="phone-number">Phone Number</Label>
<Input
id="phone-number"
value={phoneNumber}
onChange={(e) => setPhoneNumber(e.target.value)}
required
disabled={isLoading || showValidationInput || isAuthenticated}
placeholder="Enter your phone number (with country code)"
/>
</div>
<RadioGroup value={extractType} onValueChange={setExtractType} className="flex flex-col space-y-1">
<div className="flex items-center space-x-2">
<RadioGroupItem value="groups" id="groups" disabled={isLoading || !isAuthenticated} />
<Label htmlFor="groups">Extract Groups</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="contacts" id="contacts" disabled={isLoading || !isAuthenticated} />
<Label htmlFor="contacts">Extract Contacts</Label>
</div>
</RadioGroup>
{showValidationInput && (
<div className="space-y-2">
<Label htmlFor="validation-code">Validation Code</Label>
<Input
id="validation-code"
value={validationCode}
onChange={(e) => setValidationCode(e.target.value)}
required
disabled={isLoading}
placeholder="Enter the code sent to your Telegram app"
/>
</div>
)}
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> :
(isAuthenticated ? 'Extract Data' :
(showValidationInput ? 'Verify Code' : 'Request Code'))}
</Button>
</form>
{error && (
<Alert variant="destructive" className="mt-4">
<AlertTitle>Error</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
</CardContent>
</Card>
</div>
)
}