Skip to content

Commit

Permalink
feat: phoenix-chan (#38)
Browse files Browse the repository at this point in the history
* chore: add bot api token in .env.example

* feat(post): add notify bot himarpl request

* feat(common): add bot card component for phoenix-chan
  • Loading branch information
DikDns authored Jul 26, 2024
1 parent 84e64b3 commit 29e8a46
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ NEXT_PUBLIC_POSTHOG_KEY=
NEXT_PUBLIC_POSTHOG_HOST=

UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
UPSTASH_REDIS_REST_TOKEN=

BOT_API_TOKEN=
45 changes: 45 additions & 0 deletions src/components/common/bot-card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use client";

import Image from "next/image";
import Link from "next/link";

import { Button } from "@/components/ui/button";
import {
Card,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";

export function BotCard() {
return (
<Card>
<CardHeader>
<CardTitle className="text-lg font-medium">
Minta Phonix-chan untuk ngingetin postingan terbaru
</CardTitle>
<CardDescription>
Phoenix-chan adalah bot telegram official HIMARPL yang siap memberi
notifikasi terbaru terkait postingan artikel secara real-time!
</CardDescription>
</CardHeader>
<CardFooter>
<Button asChild variant="secondary" className="w-full">
<Link href="https://t.me/himarpl_bot" target="_blank">
<Image
src={
"https://cdn.jsdelivr.net/gh/himarplupi/assets-himarpl@v1.3.6/images/phoenix-chan.png"
}
alt="Foto profil phoenix-chan: sang phoenix yang lemah lembut"
className="mr-2 rounded-full"
width={32}
height={32}
/>
Chat Phoenix-chan
</Link>
</Button>
</CardFooter>
</Card>
);
}
3 changes: 3 additions & 0 deletions src/components/home/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Link from "next/link";
import { ReactLenis } from "lenis/react";
import { useQueryState } from "nuqs";

import { BotCard } from "@/components/common/bot-card";
import { Footer } from "@/components/home/footer";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
Expand Down Expand Up @@ -89,6 +90,8 @@ export function Sidebar({
))}
</div>

<BotCard />

<Footer />
</div>
</ReactLenis>
Expand Down
2 changes: 2 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const env = createEnv({
.default("development"),
BASE_URL: z.string().url(),
BASE_PATH: z.string().optional(),
BOT_API_TOKEN: z.string(),
},

/**
Expand Down Expand Up @@ -62,6 +63,7 @@ export const env = createEnv({
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
BOT_API_TOKEN: process.env.BOT_API_TOKEN,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
27 changes: 27 additions & 0 deletions src/server/api/routers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { revalidatePath } from "next/cache";
import GithubSlugger from "github-slugger";
import { z } from "zod";

import { env } from "@/env";
import { parseMetaTitle } from "@/lib/utils";
import {
createTRPCRouter,
Expand Down Expand Up @@ -101,6 +102,32 @@ export const postRouter = createTRPCRouter({
},
});

const response = await fetch(
"https://bot.himarpl.com/api/telegram/notify",
{
method: "POST",
body: JSON.stringify({
title: post.title,
slug: post.slug,
author: {
username: currentUser.username,
name: currentUser.name,
},
}),
headers: {
"Content-Type": "application/json",
"x-api-token": env.BOT_API_TOKEN,
},
},
);

if (!response.ok) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to notify telegram HIMARPL bot",
});
}

slugger.reset();
revalidatePath("/me/posts", "layout");

Expand Down

0 comments on commit 29e8a46

Please sign in to comment.