telegram-setup-helper.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use client';
  2. import type { ReactNode } from 'react';
  3. import { useState } from 'react';
  4. import {
  5. validateBotToken,
  6. parseAdminIds,
  7. validateRunTime,
  8. telegramApiBase,
  9. buildBotConfigSummary,
  10. } from '@/lib/xray/telegram';
  11. import { ToolFrame } from './tool-frame';
  12. import { TextField } from './shared/fields';
  13. import { OutputBlock } from './shared/output-block';
  14. function Status({ ok, children }: { ok: boolean; children: ReactNode }) {
  15. return (
  16. <p
  17. className={`text-xs ${ok ? 'text-emerald-600 dark:text-emerald-400' : 'text-red-600 dark:text-red-400'}`}
  18. >
  19. {children}
  20. </p>
  21. );
  22. }
  23. export function TelegramSetupHelper() {
  24. const [token, setToken] = useState('');
  25. const [adminIds, setAdminIds] = useState('');
  26. const [runTime, setRunTime] = useState('@daily');
  27. const tokenV = validateBotToken(token);
  28. const idsV = parseAdminIds(adminIds);
  29. const cronV = validateRunTime(runTime);
  30. const summary = buildBotConfigSummary({ token, adminIds, runTime });
  31. const settingsText = [
  32. `tgBotEnable = true`,
  33. `tgBotToken = ${summary.tgBotToken || '<token>'}`,
  34. `tgBotChatId = ${summary.tgBotChatId || '<admin ids>'}`,
  35. `tgRunTime = ${summary.tgRunTime || '@daily'}`,
  36. ].join('\n');
  37. function reset() {
  38. setToken('');
  39. setAdminIds('');
  40. setRunTime('@daily');
  41. }
  42. return (
  43. <ToolFrame
  44. title="Telegram bot setup helper"
  45. description="Validate your bot token, admin IDs, and report schedule, then copy the panel settings."
  46. onReset={reset}
  47. >
  48. <div className="grid grid-cols-1 gap-4">
  49. <div>
  50. <TextField
  51. label="Bot token (from @BotFather)"
  52. value={token}
  53. onChange={setToken}
  54. placeholder="123456789:AA..."
  55. />
  56. {token ? (
  57. tokenV.valid ? (
  58. <Status ok>Valid — bot id {tokenV.botId}</Status>
  59. ) : (
  60. <Status ok={false}>{tokenV.error}</Status>
  61. )
  62. ) : null}
  63. </div>
  64. <div>
  65. <TextField
  66. label="Admin chat IDs (comma-separated)"
  67. value={adminIds}
  68. onChange={setAdminIds}
  69. placeholder="111111111, 222222222"
  70. />
  71. {adminIds ? (
  72. idsV.invalid.length > 0 ? (
  73. <Status ok={false}>Not numeric: {idsV.invalid.join(', ')}</Status>
  74. ) : (
  75. <Status ok>
  76. {idsV.ids.length} admin id{idsV.ids.length === 1 ? '' : 's'}
  77. </Status>
  78. )
  79. ) : null}
  80. </div>
  81. <div>
  82. <TextField
  83. label="Report schedule (tgRunTime)"
  84. value={runTime}
  85. onChange={setRunTime}
  86. placeholder="@daily, @every 8h, or a cron expression"
  87. />
  88. {runTime ? (
  89. cronV.valid ? (
  90. <Status ok>Valid ({cronV.kind})</Status>
  91. ) : (
  92. <Status ok={false}>{cronV.error}</Status>
  93. )
  94. ) : null}
  95. </div>
  96. </div>
  97. <div className="mt-4 grid grid-cols-1 gap-4">
  98. <OutputBlock label="Panel settings" value={settingsText} />
  99. <OutputBlock
  100. label="Bot API base (keep secret)"
  101. value={tokenV.valid ? telegramApiBase(token) : 'https://api.telegram.org/bot<token>'}
  102. />
  103. </div>
  104. </ToolFrame>
  105. );
  106. }