Skip to content

Commit 2b73dc8

Browse files
committed
feat: add method selection for Telegram bot with conditional webhook fields
1 parent c6cc6c7 commit 2b73dc8

File tree

5 files changed

+123
-50
lines changed

5 files changed

+123
-50
lines changed

dashboard/public/statics/locales/en.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@
245245
"webhookSecret": "Webhook Secret",
246246
"webhookSecretPlaceholder": "Enter webhook secret",
247247
"webhookSecretDescription": "Secret token for webhook security",
248+
"method": "Connection Method",
249+
"methodPlaceholder": "Select connection method",
250+
"methodDescription": "Choose how the bot receives updates from Telegram",
251+
"webhook": "Webhook",
252+
"longPolling": "Long Polling",
248253
"proxyUrl": "Proxy URL",
249254
"proxyUrlPlaceholder": "socks5://proxy.example.com:1080",
250255
"proxyUrlDescription": "Proxy URL for Telegram API connections (optional)"

dashboard/public/statics/locales/fa.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@
158158
"webhookSecret": "رمز Webhook",
159159
"webhookSecretPlaceholder": "رمز webhook را وارد کنید",
160160
"webhookSecretDescription": "توکن مخفی برای امنیت webhook",
161+
"method": "روش اتصال",
162+
"methodPlaceholder": "روش اتصال را انتخاب کنید",
163+
"methodDescription": "انتخاب کنید که ربات چگونه به‌روزرسانی‌ها را از تلگرام دریافت کند",
164+
"webhook": "Webhook",
165+
"longPolling": "Long Polling",
161166
"proxyUrl": "آدرس پروکسی",
162167
"proxyUrlPlaceholder": "socks5://proxy.example.com:1080",
163168
"proxyUrlDescription": "آدرس پروکسی برای اتصالات API تلگرام (اختیاری)"

dashboard/public/statics/locales/ru.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@
245245
"webhookSecret": "Секрет Webhook",
246246
"webhookSecretPlaceholder": "Введите секрет webhook",
247247
"webhookSecretDescription": "Секретный токен для безопасности webhook",
248+
"method": "Метод подключения",
249+
"methodPlaceholder": "Выберите метод подключения",
250+
"methodDescription": "Выберите, как бот получает обновления от Telegram",
251+
"webhook": "Webhook",
252+
"longPolling": "Long Polling",
248253
"proxyUrl": "URL прокси",
249254
"proxyUrlPlaceholder": "socks5://proxy.example.com:1080",
250255
"proxyUrlDescription": "URL прокси для подключений к API Telegram (необязательно)"

dashboard/public/statics/locales/zh.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@
223223
"webhookSecret": "Webhook 密钥",
224224
"webhookSecretPlaceholder": "输入 webhook 密钥",
225225
"webhookSecretDescription": "用于 webhook 安全的秘密令牌",
226+
"method": "连接方式",
227+
"methodPlaceholder": "选择连接方式",
228+
"methodDescription": "选择机器人如何从 Telegram 接收更新",
229+
"webhook": "Webhook",
230+
"longPolling": "长轮询",
226231
"proxyUrl": "代理 URL",
227232
"proxyUrlPlaceholder": "socks5://proxy.example.com:1080",
228233
"proxyUrlDescription": "Telegram API 连接的代理 URL(可选)",

dashboard/src/pages/_dashboard.settings.telegram.tsx

Lines changed: 103 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import { Input } from '@/components/ui/input'
99
import { PasswordInput } from '@/components/ui/password-input'
1010
import { Switch } from '@/components/ui/switch'
1111
import { Separator } from '@/components/ui/separator'
12-
import { Bot, Webhook, Shield, Globe, Smartphone, Send, Users } from 'lucide-react'
12+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
13+
import { Bot, Webhook, Shield, Globe, Smartphone, Send, Users, Settings } from 'lucide-react'
1314
import { useSettingsContext } from './_dashboard.settings'
1415
import { toast } from 'sonner'
1516

1617
// Telegram settings validation schema
1718
const telegramSettingsSchema = z.object({
1819
enable: z.boolean().default(false),
1920
token: z.string().optional(),
21+
method: z.enum(['webhook', 'long-polling']).default('webhook'),
2022
webhook_url: z.string().url('Please enter a valid URL').optional().or(z.literal('')).refine((url) => {
2123
if (!url || url === '') return true; // Allow empty URLs
2224
try {
@@ -69,6 +71,7 @@ export default function TelegramSettings() {
6971
defaultValues: {
7072
enable: false,
7173
token: '',
74+
method: 'webhook',
7275
webhook_url: '',
7376
webhook_secret: '',
7477
proxy_url: '',
@@ -78,8 +81,9 @@ export default function TelegramSettings() {
7881
}
7982
})
8083

81-
// Watch the enable and mini_app_login fields for conditional rendering
84+
// Watch the enable, method, and mini_app_login fields for conditional rendering
8285
const enableTelegram = form.watch('enable')
86+
const method = form.watch('method')
8387

8488
// Update form when settings are loaded
8589
useEffect(() => {
@@ -88,6 +92,7 @@ export default function TelegramSettings() {
8892
form.reset({
8993
enable: telegramData.enable || false,
9094
token: telegramData.token || '',
95+
method: telegramData.method || 'webhook',
9196
webhook_url: telegramData.webhook_url || '',
9297
webhook_secret: telegramData.webhook_secret || '',
9398
proxy_url: telegramData.proxy_url || '',
@@ -114,6 +119,7 @@ export default function TelegramSettings() {
114119
form.reset({
115120
enable: telegramData.enable || false,
116121
token: telegramData.token || '',
122+
method: telegramData.method || 'webhook',
117123
webhook_url: telegramData.webhook_url || '',
118124
webhook_secret: telegramData.webhook_secret || '',
119125
proxy_url: telegramData.proxy_url || '',
@@ -215,6 +221,47 @@ export default function TelegramSettings() {
215221
)}
216222
/>
217223

224+
{/* Method Selection - Only show when Telegram is enabled */}
225+
{enableTelegram && (
226+
<FormField
227+
control={form.control}
228+
name="method"
229+
render={({ field }) => (
230+
<FormItem className="space-y-2">
231+
<FormLabel className="text-sm font-medium flex items-center gap-2">
232+
<Settings className="h-4 w-4" />
233+
{t('settings.telegram.general.method')}
234+
</FormLabel>
235+
<Select onValueChange={field.onChange} value={field.value}>
236+
<FormControl>
237+
<SelectTrigger className="w-full">
238+
<SelectValue placeholder={t('settings.telegram.general.methodPlaceholder')} />
239+
</SelectTrigger>
240+
</FormControl>
241+
<SelectContent>
242+
<SelectItem value="webhook">
243+
<div className="flex items-center gap-2">
244+
<Webhook className="h-4 w-4" />
245+
{t('settings.telegram.general.webhook')}
246+
</div>
247+
</SelectItem>
248+
<SelectItem value="long-polling">
249+
<div className="flex items-center gap-2">
250+
<Send className="h-4 w-4" />
251+
{t('settings.telegram.general.longPolling')}
252+
</div>
253+
</SelectItem>
254+
</SelectContent>
255+
</Select>
256+
<FormDescription className="text-sm text-muted-foreground">
257+
{t('settings.telegram.general.methodDescription')}
258+
</FormDescription>
259+
<FormMessage />
260+
</FormItem>
261+
)}
262+
/>
263+
)}
264+
218265
{/* Configuration Fields - Only show when Telegram is enabled */}
219266
{enableTelegram && (
220267
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
@@ -242,55 +289,61 @@ export default function TelegramSettings() {
242289
)}
243290
/>
244291

245-
<FormField
246-
control={form.control}
247-
name="webhook_url"
248-
render={({ field }) => (
249-
<FormItem className="space-y-2">
250-
<FormLabel className="text-sm font-medium flex items-center gap-2">
251-
<Webhook className="h-4 w-4" />
252-
{t('settings.telegram.general.webhookUrl')}
253-
</FormLabel>
254-
<FormControl>
255-
<Input
256-
type="url"
257-
placeholder={t('settings.telegram.general.webhookUrlPlaceholder')}
258-
{...field}
259-
className="font-mono"
260-
/>
261-
</FormControl>
262-
<FormDescription className="text-sm text-muted-foreground">
263-
{t('settings.telegram.general.webhookUrlDescription')}
264-
<br />
265-
</FormDescription>
266-
<FormMessage />
267-
</FormItem>
268-
)}
269-
/>
292+
{/* Webhook URL - Only show when method is webhook */}
293+
{method === 'webhook' && (
294+
<FormField
295+
control={form.control}
296+
name="webhook_url"
297+
render={({ field }) => (
298+
<FormItem className="space-y-2">
299+
<FormLabel className="text-sm font-medium flex items-center gap-2">
300+
<Webhook className="h-4 w-4" />
301+
{t('settings.telegram.general.webhookUrl')}
302+
</FormLabel>
303+
<FormControl>
304+
<Input
305+
type="url"
306+
placeholder={t('settings.telegram.general.webhookUrlPlaceholder')}
307+
{...field}
308+
className="font-mono"
309+
/>
310+
</FormControl>
311+
<FormDescription className="text-sm text-muted-foreground">
312+
{t('settings.telegram.general.webhookUrlDescription')}
313+
<br />
314+
</FormDescription>
315+
<FormMessage />
316+
</FormItem>
317+
)}
318+
/>
319+
)}
270320

271-
<FormField
272-
control={form.control}
273-
name="webhook_secret"
274-
render={({ field }) => (
275-
<FormItem className="space-y-2">
276-
<FormLabel className="text-sm font-medium flex items-center gap-2">
277-
<Shield className="h-4 w-4" />
278-
{t('settings.telegram.general.webhookSecret')}
279-
</FormLabel>
280-
<FormControl>
281-
<PasswordInput
282-
placeholder={t('settings.telegram.general.webhookSecretPlaceholder')}
283-
{...field}
284-
className="font-mono"
285-
/>
286-
</FormControl>
287-
<FormDescription className="text-sm text-muted-foreground">
288-
{t('settings.telegram.general.webhookSecretDescription')}
289-
</FormDescription>
290-
<FormMessage />
291-
</FormItem>
292-
)}
293-
/>
321+
{/* Webhook Secret - Only show when method is webhook */}
322+
{method === 'webhook' && (
323+
<FormField
324+
control={form.control}
325+
name="webhook_secret"
326+
render={({ field }) => (
327+
<FormItem className="space-y-2">
328+
<FormLabel className="text-sm font-medium flex items-center gap-2">
329+
<Shield className="h-4 w-4" />
330+
{t('settings.telegram.general.webhookSecret')}
331+
</FormLabel>
332+
<FormControl>
333+
<PasswordInput
334+
placeholder={t('settings.telegram.general.webhookSecretPlaceholder')}
335+
{...field}
336+
className="font-mono"
337+
/>
338+
</FormControl>
339+
<FormDescription className="text-sm text-muted-foreground">
340+
{t('settings.telegram.general.webhookSecretDescription')}
341+
</FormDescription>
342+
<FormMessage />
343+
</FormItem>
344+
)}
345+
/>
346+
)}
294347

295348
<FormField
296349
control={form.control}

0 commit comments

Comments
 (0)