Skip to content

Commit

Permalink
allow using the default bot set by an env var
Browse files Browse the repository at this point in the history
  • Loading branch information
dreth committed Sep 28, 2024
1 parent 680a8f6 commit a2e3ea5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 49 deletions.
1 change: 1 addition & 0 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
- ENVIRONMENT=production
- CUSTOM_DOMAIN=https://hbd.lotiguere.com
- GIN_MODE=release
- HBD_DEFAULT_BOT_API_KEY=${HBD_DEFAULT_BOT_API_KEY}
# Optionally for backups of the birthday database to S3 or S3-compatible services
- HBD_ENABLE_BACKUP=true
- HBD_USER_ACCESS_KEY_ID=${HBD_USER_ACCESS_KEY_ID}
Expand Down
53 changes: 4 additions & 49 deletions frontend/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
PopoverTrigger,
} from "@/components/ui/popover";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import TelegramApiKeyInput from "@/components/ui/telegram-api-key-input";

export default function Home() {
const [email, setEmail] = useState("");
Expand Down Expand Up @@ -373,55 +374,7 @@ export default function Home() {
</Select>
</div>
</div>{" "}
<div>
<label
htmlFor="telegram-api-key"
className="text-sm font-medium text-primary flex items-center"
>
Telegram Bot API Key
<Popover>
<PopoverTrigger>
<CircleHelp className="ml-2 text-secondary-foreground w-4 h-4" />
</PopoverTrigger>
<PopoverContent>
<p className="text-primary text-lg">
Need help finding your API key?
</p>
<p>Follow these steps to get your Telegram API key:</p>
<ol className="list-decimal ml-4">
<li>Open Telegram.</li>
<li>
Search for <b>BotFather</b>.
</li>
<li>
Start a chat with <b>BotFather</b>.
</li>
<li>
Use the{" "}
<code className="bg-blue-100 dark:bg-primary p-0.5 rounded-md">
/newbot
</code>{" "}
command to create a new bot.
</li>
<li>Follow the instructions to create a new bot.</li>
<li>Copy the API key.</li>
</ol>
<p>
This API key allows the application to send messages
to you through the bot.
</p>
</PopoverContent>
</Popover>
</label>
<Input
id="telegram-api-key"
type="text"
placeholder="Telegram Bot API Key"
value={telegramApiKey}
onChange={(e) => setTelegramApiKey(e.target.value)}
className="mt-1 block w-full bg-primary-foreground dark:bg-background"
/>
</div>
<TelegramApiKeyInput></TelegramApiKeyInput>
<div>
<label
htmlFor="telegram-user"
Expand Down Expand Up @@ -453,6 +406,8 @@ export default function Home() {
numeric ID in several places of the JSON response.
That&apos;s your ID!
</p>
<br />
<p>If you use the default bot, message the @RawDataBot on Telegram and it will return you a JSON response with your chat ID under &quot;id&quot; in several document fields, it should be a string of numbers like 637278744</p>
</PopoverContent>
</Popover>
</label>
Expand Down
91 changes: 91 additions & 0 deletions frontend/components/ui/telegram-api-key-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as React from "react";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { CircleHelp } from "lucide-react";
import { Input } from "@/components/ui/input";

function TelegramApiKeyInput() {
const [telegramApiKey, setTelegramApiKey] = React.useState("");
const [useDefaultBot, setUseDefaultBot] = React.useState(false);

// Retrieve the default bot API key from the environment variable
const defaultBotApiKey = process.env.HBD_DEFAULT_BOT_API_KEY || "";

// Handle checkbox change
const handleCheckboxChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setUseDefaultBot(e.target.checked);
if (e.target.checked) {
setTelegramApiKey(defaultBotApiKey);
} else {
setTelegramApiKey("");
}
};

return (
<div>
<label
htmlFor="telegram-api-key"
className="text-sm font-medium text-primary flex items-center"
>
Telegram Bot API Key
<Popover>
<PopoverTrigger>
<CircleHelp className="ml-2 text-secondary-foreground w-4 h-4" />
</PopoverTrigger>
<PopoverContent>
<p className="text-primary text-lg">Need help finding your API key?</p>
<p>Follow these steps to get your Telegram API key:</p>
<ol className="list-decimal ml-4">
<li>Open Telegram.</li>
<li>Search for <b>BotFather</b>.</li>
<li>Start a chat with <b>BotFather</b>.</li>
<li>
Use the{" "}
<code className="bg-blue-100 dark:bg-primary p-0.5 rounded-md">
/newbot
</code>{" "}
command to create a new bot.
</li>
<li>Follow the instructions to create a new bot.</li>
<li>Copy the API key.</li>
</ol>
<p>This API key allows the application to send messages to you through the bot.</p>
<br />
<p>Alternatively, you can check the &quot;Use default bot&quot; checkbox and the default bot will be used (if configured by the host)</p>
</PopoverContent>
</Popover>
</label>

{/* Checkbox to toggle between using default bot and custom API key */}
<div className="mt-2 flex items-center">
<input
id="use-default-bot"
type="checkbox"
checked={useDefaultBot}
onChange={handleCheckboxChange}
className="mr-2"
/>
<label htmlFor="use-default-bot" className="text-sm text-primary">
Use the default bot
</label>
</div>

{/* Conditionally render the input field based on the checkbox state */}
{!useDefaultBot && (
<Input
id="telegram-api-key"
type="text"
placeholder="Telegram Bot API Key"
value={telegramApiKey}
onChange={(e) => setTelegramApiKey(e.target.value)}
className="mt-1 block w-full bg-primary-foreground dark:bg-background"
/>
)}
</div>
);
}

export default TelegramApiKeyInput;

0 comments on commit a2e3ea5

Please sign in to comment.