Initial commit
This commit is contained in:
9
.env.example
Normal file
9
.env.example
Normal file
@ -0,0 +1,9 @@
|
||||
NEXT_PUBLIC_APP_URL=http://localhost:3000
|
||||
WHATSAPP_VERIFY_TOKEN=change-me
|
||||
WHATSAPP_ACCESS_TOKEN=change-me
|
||||
WHATSAPP_PHONE_NUMBER_ID=change-me
|
||||
WHATSAPP_API_VERSION=v23.0
|
||||
WA_TEST_NUMBER=62812xxxxxxx
|
||||
WA_TEST_LOGIN_USERNAME=admin
|
||||
WA_TEST_LOGIN_PASSWORD=admin123
|
||||
SESSION_SECRET=ganti-dengan-random-string
|
||||
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
node_modules
|
||||
.next
|
||||
.env
|
||||
npm-debug.log*
|
||||
data
|
||||
.DS_Store
|
||||
5
AGENTS.md
Normal file
5
AGENTS.md
Normal file
@ -0,0 +1,5 @@
|
||||
<!-- BEGIN:nextjs-agent-rules -->
|
||||
# This is NOT the Next.js you know
|
||||
|
||||
This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` before writing any code. Heed deprecation notices.
|
||||
<!-- END:nextjs-agent-rules -->
|
||||
27
README.md
Normal file
27
README.md
Normal file
@ -0,0 +1,27 @@
|
||||
# wa-test-nextjs
|
||||
|
||||
Mini app Next.js untuk test koneksi WhatsApp webhook ke nomor testing.
|
||||
|
||||
## Halaman
|
||||
- /login
|
||||
- /settings
|
||||
- /test
|
||||
- /webhook-logs
|
||||
|
||||
## API
|
||||
- GET /api/webhooks/whatsapp
|
||||
- POST /api/webhooks/whatsapp
|
||||
- POST /api/test/send
|
||||
|
||||
## Flow test
|
||||
- kirim `daftar`
|
||||
- app balas minta nama
|
||||
- kirim nama
|
||||
- app balas konfirmasi
|
||||
|
||||
## Jalankan
|
||||
```bash
|
||||
cp .env.example .env
|
||||
npm install
|
||||
npm run dev
|
||||
```
|
||||
18
eslint.config.mjs
Normal file
18
eslint.config.mjs
Normal file
@ -0,0 +1,18 @@
|
||||
import { defineConfig, globalIgnores } from "eslint/config";
|
||||
import nextVitals from "eslint-config-next/core-web-vitals";
|
||||
import nextTs from "eslint-config-next/typescript";
|
||||
|
||||
const eslintConfig = defineConfig([
|
||||
...nextVitals,
|
||||
...nextTs,
|
||||
// Override default ignores of eslint-config-next.
|
||||
globalIgnores([
|
||||
// Default ignores of eslint-config-next:
|
||||
".next/**",
|
||||
"out/**",
|
||||
"build/**",
|
||||
"next-env.d.ts",
|
||||
]),
|
||||
]);
|
||||
|
||||
export default eslintConfig;
|
||||
4
next-env.d.ts
vendored
Normal file
4
next-env.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
|
||||
// This file is auto-generated by Next.js
|
||||
5
next.config.ts
Normal file
5
next.config.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import type { NextConfig } from 'next'
|
||||
|
||||
const nextConfig: NextConfig = {}
|
||||
|
||||
export default nextConfig
|
||||
5947
package-lock.json
generated
Normal file
5947
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
24
package.json
Normal file
24
package.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "wa-test-nextjs",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "next lint"
|
||||
},
|
||||
"dependencies": {
|
||||
"next": "16.0.0",
|
||||
"react": "19.2.0",
|
||||
"react-dom": "19.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5",
|
||||
"@types/node": "^20",
|
||||
"@types/react": "^19",
|
||||
"@types/react-dom": "^19",
|
||||
"eslint": "^9",
|
||||
"eslint-config-next": "16.0.0"
|
||||
}
|
||||
}
|
||||
1
public/file.svg
Normal file
1
public/file.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M14.5 13.5V5.41a1 1 0 0 0-.3-.7L9.8.29A1 1 0 0 0 9.08 0H1.5v13.5A2.5 2.5 0 0 0 4 16h8a2.5 2.5 0 0 0 2.5-2.5m-1.5 0v-7H8v-5H3v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1M9.5 5V2.12L12.38 5zM5.13 5h-.62v1.25h2.12V5zm-.62 3h7.12v1.25H4.5zm.62 3h-.62v1.25h7.12V11z" clip-rule="evenodd" fill="#666" fill-rule="evenodd"/></svg>
|
||||
|
After Width: | Height: | Size: 391 B |
1
public/globe.svg
Normal file
1
public/globe.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><g clip-path="url(#a)"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.27 14.1a6.5 6.5 0 0 0 3.67-3.45q-1.24.21-2.7.34-.31 1.83-.97 3.1M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.48-1.52a7 7 0 0 1-.96 0H7.5a4 4 0 0 1-.84-1.32q-.38-.89-.63-2.08a40 40 0 0 0 3.92 0q-.25 1.2-.63 2.08a4 4 0 0 1-.84 1.31zm2.94-4.76q1.66-.15 2.95-.43a7 7 0 0 0 0-2.58q-1.3-.27-2.95-.43a18 18 0 0 1 0 3.44m-1.27-3.54a17 17 0 0 1 0 3.64 39 39 0 0 1-4.3 0 17 17 0 0 1 0-3.64 39 39 0 0 1 4.3 0m1.1-1.17q1.45.13 2.69.34a6.5 6.5 0 0 0-3.67-3.44q.65 1.26.98 3.1M8.48 1.5l.01.02q.41.37.84 1.31.38.89.63 2.08a40 40 0 0 0-3.92 0q.25-1.2.63-2.08a4 4 0 0 1 .85-1.32 7 7 0 0 1 .96 0m-2.75.4a6.5 6.5 0 0 0-3.67 3.44 29 29 0 0 1 2.7-.34q.31-1.83.97-3.1M4.58 6.28q-1.66.16-2.95.43a7 7 0 0 0 0 2.58q1.3.27 2.95.43a18 18 0 0 1 0-3.44m.17 4.71q-1.45-.12-2.69-.34a6.5 6.5 0 0 0 3.67 3.44q-.65-1.27-.98-3.1" fill="#666"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h16v16H0z"/></clipPath></defs></svg>
|
||||
|
After Width: | Height: | Size: 1.0 KiB |
1
public/next.svg
Normal file
1
public/next.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 394 80"><path fill="#000" d="M262 0h68.5v12.7h-27.2v66.6h-13.6V12.7H262V0ZM149 0v12.7H94v20.4h44.3v12.6H94v21h55v12.6H80.5V0h68.7zm34.3 0h-17.8l63.8 79.4h17.9l-32-39.7 32-39.6h-17.9l-23 28.6-23-28.6zm18.3 56.7-9-11-27.1 33.7h17.8l18.3-22.7z"/><path fill="#000" d="M81 79.3 17 0H0v79.3h13.6V17l50.2 62.3H81Zm252.6-.4c-1 0-1.8-.4-2.5-1s-1.1-1.6-1.1-2.6.3-1.8 1-2.5 1.6-1 2.6-1 1.8.3 2.5 1a3.4 3.4 0 0 1 .6 4.3 3.7 3.7 0 0 1-3 1.8zm23.2-33.5h6v23.3c0 2.1-.4 4-1.3 5.5a9.1 9.1 0 0 1-3.8 3.5c-1.6.8-3.5 1.3-5.7 1.3-2 0-3.7-.4-5.3-1s-2.8-1.8-3.7-3.2c-.9-1.3-1.4-3-1.4-5h6c.1.8.3 1.6.7 2.2s1 1.2 1.6 1.5c.7.4 1.5.5 2.4.5 1 0 1.8-.2 2.4-.6a4 4 0 0 0 1.6-1.8c.3-.8.5-1.8.5-3V45.5zm30.9 9.1a4.4 4.4 0 0 0-2-3.3 7.5 7.5 0 0 0-4.3-1.1c-1.3 0-2.4.2-3.3.5-.9.4-1.6 1-2 1.6a3.5 3.5 0 0 0-.3 4c.3.5.7.9 1.3 1.2l1.8 1 2 .5 3.2.8c1.3.3 2.5.7 3.7 1.2a13 13 0 0 1 3.2 1.8 8.1 8.1 0 0 1 3 6.5c0 2-.5 3.7-1.5 5.1a10 10 0 0 1-4.4 3.5c-1.8.8-4.1 1.2-6.8 1.2-2.6 0-4.9-.4-6.8-1.2-2-.8-3.4-2-4.5-3.5a10 10 0 0 1-1.7-5.6h6a5 5 0 0 0 3.5 4.6c1 .4 2.2.6 3.4.6 1.3 0 2.5-.2 3.5-.6 1-.4 1.8-1 2.4-1.7a4 4 0 0 0 .8-2.4c0-.9-.2-1.6-.7-2.2a11 11 0 0 0-2.1-1.4l-3.2-1-3.8-1c-2.8-.7-5-1.7-6.6-3.2a7.2 7.2 0 0 1-2.4-5.7 8 8 0 0 1 1.7-5 10 10 0 0 1 4.3-3.5c2-.8 4-1.2 6.4-1.2 2.3 0 4.4.4 6.2 1.2 1.8.8 3.2 2 4.3 3.4 1 1.4 1.5 3 1.5 5h-5.8z"/></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
1
public/vercel.svg
Normal file
1
public/vercel.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1155 1000"><path d="m577.3 0 577.4 1000H0z" fill="#fff"/></svg>
|
||||
|
After Width: | Height: | Size: 128 B |
1
public/window.svg
Normal file
1
public/window.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 2.5h13v10a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1zM0 1h16v11.5a2.5 2.5 0 0 1-2.5 2.5h-11A2.5 2.5 0 0 1 0 12.5zm3.75 4.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5M7 4.75a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0m1.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5" fill="#666"/></svg>
|
||||
|
After Width: | Height: | Size: 385 B |
18
src/app/api/login/route.ts
Normal file
18
src/app/api/login/route.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { createSession } from '@/lib/auth'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const formData = await request.formData()
|
||||
const username = formData.get('username')?.toString() || ''
|
||||
const password = formData.get('password')?.toString() || ''
|
||||
|
||||
if (
|
||||
username === (process.env.WA_TEST_LOGIN_USERNAME || 'admin') &&
|
||||
password === (process.env.WA_TEST_LOGIN_PASSWORD || 'admin123')
|
||||
) {
|
||||
await createSession()
|
||||
return NextResponse.redirect(new URL('/settings', request.url))
|
||||
}
|
||||
|
||||
return NextResponse.redirect(new URL('/login?error=Login%20gagal', request.url))
|
||||
}
|
||||
7
src/app/api/logout/route.ts
Normal file
7
src/app/api/logout/route.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { clearSession } from '@/lib/auth'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
await clearSession()
|
||||
return NextResponse.redirect(new URL('/login', request.url))
|
||||
}
|
||||
14
src/app/api/test/send/route.ts
Normal file
14
src/app/api/test/send/route.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { sendTextMessage } from '@/lib/whatsapp'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const formData = await request.formData()
|
||||
const message = formData.get('message')?.toString() || ''
|
||||
|
||||
if (!message.trim()) {
|
||||
return NextResponse.redirect(new URL('/test?status=Pesan%20kosong', request.url))
|
||||
}
|
||||
|
||||
await sendTextMessage(process.env.WA_TEST_NUMBER || '', message)
|
||||
return NextResponse.redirect(new URL('/test?status=Pesan%20berhasil%20dikirim', request.url))
|
||||
}
|
||||
21
src/app/api/webhooks/whatsapp/route.ts
Normal file
21
src/app/api/webhooks/whatsapp/route.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { handleWebhook } from '@/lib/whatsapp'
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const mode = searchParams.get('hub.mode')
|
||||
const token = searchParams.get('hub.verify_token')
|
||||
const challenge = searchParams.get('hub.challenge')
|
||||
|
||||
if (mode === 'subscribe' && token === process.env.WHATSAPP_VERIFY_TOKEN) {
|
||||
return new NextResponse(challenge || '', { status: 200 })
|
||||
}
|
||||
|
||||
return new NextResponse('Verification failed', { status: 403 })
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const payload = await request.json()
|
||||
await handleWebhook(payload)
|
||||
return NextResponse.json({ ok: true })
|
||||
}
|
||||
BIN
src/app/favicon.ico
Normal file
BIN
src/app/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
25
src/app/globals.css
Normal file
25
src/app/globals.css
Normal file
@ -0,0 +1,25 @@
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: Arial, sans-serif;
|
||||
background: #f8fafc;
|
||||
color: #0f172a;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
a { color: inherit; text-decoration: none; }
|
||||
|
||||
.wrap { max-width: 1000px; margin: 40px auto; padding: 0 20px; }
|
||||
.card { background: #fff; padding: 24px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,.06); margin-top: 20px; }
|
||||
.top { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
|
||||
.btn { color: #fff; background: #111827; padding: 10px 14px; border-radius: 10px; border: 0; cursor: pointer; display: inline-block; }
|
||||
.input, .textarea { width: 100%; padding: 12px; border: 1px solid #d0d7e2; border-radius: 10px; }
|
||||
.textarea { min-height: 110px; }
|
||||
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
|
||||
.item { background: #f8fafc; border: 1px solid #e2e8f0; padding: 14px; border-radius: 12px; }
|
||||
.label { font-size: 12px; color: #64748b; text-transform: uppercase; }
|
||||
.value { margin-top: 6px; word-break: break-all; }
|
||||
.flash { background: #dcfce7; color: #166534; padding: 12px; border-radius: 10px; margin-bottom: 16px; }
|
||||
.error { background: #fee2e2; color: #991b1b; padding: 10px; border-radius: 10px; margin-bottom: 16px; }
|
||||
pre { white-space: pre-wrap; word-break: break-word; background: #0f172a; color: #e2e8f0; padding: 14px; border-radius: 12px; overflow: auto; }
|
||||
15
src/app/layout.tsx
Normal file
15
src/app/layout.tsx
Normal file
@ -0,0 +1,15 @@
|
||||
import './globals.css'
|
||||
import type { Metadata } from 'next'
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'WA Test NextJS',
|
||||
description: 'Mini app test WhatsApp webhook',
|
||||
}
|
||||
|
||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<html lang="id">
|
||||
<body>{children}</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
27
src/app/login/page.tsx
Normal file
27
src/app/login/page.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
import { redirect } from 'next/navigation'
|
||||
import { isAuthenticated } from '@/lib/auth'
|
||||
|
||||
export default async function LoginPage({ searchParams }: { searchParams: Promise<{ error?: string }> }) {
|
||||
if (await isAuthenticated()) {
|
||||
redirect('/settings')
|
||||
}
|
||||
|
||||
const params = await searchParams
|
||||
|
||||
return (
|
||||
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
|
||||
<div className="card" style={{ width: 360 }}>
|
||||
<h2>Login WA Test</h2>
|
||||
<p>Masuk pakai akun hardcoded dulu.</p>
|
||||
{params.error ? <div className="error">{params.error}</div> : null}
|
||||
<form method="POST" action="/api/login">
|
||||
<label>Username</label>
|
||||
<input className="input" type="text" name="username" required />
|
||||
<label>Password</label>
|
||||
<input className="input" type="password" name="password" required />
|
||||
<button className="btn" type="submit" style={{ width: '100%' }}>Masuk</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
142
src/app/page.module.css
Normal file
142
src/app/page.module.css
Normal file
@ -0,0 +1,142 @@
|
||||
.page {
|
||||
--background: #fafafa;
|
||||
--foreground: #fff;
|
||||
|
||||
--text-primary: #000;
|
||||
--text-secondary: #666;
|
||||
|
||||
--button-primary-hover: #383838;
|
||||
--button-secondary-hover: #f2f2f2;
|
||||
--button-secondary-border: #ebebeb;
|
||||
|
||||
display: flex;
|
||||
flex: 1;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-family: var(--font-geist-sans);
|
||||
background-color: var(--background);
|
||||
}
|
||||
|
||||
.main {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
max-width: 800px;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
background-color: var(--foreground);
|
||||
padding: 120px 60px;
|
||||
}
|
||||
|
||||
.intro {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
text-align: left;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.intro h1 {
|
||||
max-width: 320px;
|
||||
font-size: 40px;
|
||||
font-weight: 600;
|
||||
line-height: 48px;
|
||||
letter-spacing: -2.4px;
|
||||
text-wrap: balance;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.intro p {
|
||||
max-width: 440px;
|
||||
font-size: 18px;
|
||||
line-height: 32px;
|
||||
text-wrap: balance;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.intro a {
|
||||
font-weight: 500;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.ctas {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
width: 100%;
|
||||
max-width: 440px;
|
||||
gap: 16px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.ctas a {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 40px;
|
||||
padding: 0 16px;
|
||||
border-radius: 128px;
|
||||
border: 1px solid transparent;
|
||||
transition: 0.2s;
|
||||
cursor: pointer;
|
||||
width: fit-content;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
a.primary {
|
||||
background: var(--text-primary);
|
||||
color: var(--background);
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
a.secondary {
|
||||
border-color: var(--button-secondary-border);
|
||||
}
|
||||
|
||||
/* Enable hover only on non-touch devices */
|
||||
@media (hover: hover) and (pointer: fine) {
|
||||
a.primary:hover {
|
||||
background: var(--button-primary-hover);
|
||||
border-color: transparent;
|
||||
}
|
||||
|
||||
a.secondary:hover {
|
||||
background: var(--button-secondary-hover);
|
||||
border-color: transparent;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.main {
|
||||
padding: 48px 24px;
|
||||
}
|
||||
|
||||
.intro {
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.intro h1 {
|
||||
font-size: 32px;
|
||||
line-height: 40px;
|
||||
letter-spacing: -1.92px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
.logo {
|
||||
filter: invert();
|
||||
}
|
||||
|
||||
.page {
|
||||
--background: #000;
|
||||
--foreground: #000;
|
||||
|
||||
--text-primary: #ededed;
|
||||
--text-secondary: #999;
|
||||
|
||||
--button-primary-hover: #ccc;
|
||||
--button-secondary-hover: #1a1a1a;
|
||||
--button-secondary-border: #1a1a1a;
|
||||
}
|
||||
}
|
||||
5
src/app/page.tsx
Normal file
5
src/app/page.tsx
Normal file
@ -0,0 +1,5 @@
|
||||
import { redirect } from 'next/navigation'
|
||||
|
||||
export default function HomePage() {
|
||||
redirect('/login')
|
||||
}
|
||||
38
src/app/settings/page.tsx
Normal file
38
src/app/settings/page.tsx
Normal file
@ -0,0 +1,38 @@
|
||||
import Link from 'next/link'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { isAuthenticated } from '@/lib/auth'
|
||||
|
||||
export default async function SettingsPage() {
|
||||
if (!await isAuthenticated()) {
|
||||
redirect('/login')
|
||||
}
|
||||
|
||||
const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
|
||||
|
||||
return (
|
||||
<div className="wrap">
|
||||
<div className="top">
|
||||
<div>
|
||||
<h1>Settings</h1>
|
||||
<p>Config hardcoded untuk test koneksi WhatsApp.</p>
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 10 }}>
|
||||
<Link className="btn" href="/test">Halaman Test</Link>
|
||||
<Link className="btn" href="/webhook-logs">Webhook Logs</Link>
|
||||
<form method="POST" action="/api/logout"><button className="btn" type="submit">Logout</button></form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<div className="grid">
|
||||
<div className="item"><div className="label">Webhook URL</div><div className="value">{appUrl}/api/webhooks/whatsapp</div></div>
|
||||
<div className="item"><div className="label">Verify Token</div><div className="value">{process.env.WHATSAPP_VERIFY_TOKEN}</div></div>
|
||||
<div className="item"><div className="label">Phone Number ID</div><div className="value">{process.env.WHATSAPP_PHONE_NUMBER_ID}</div></div>
|
||||
<div className="item"><div className="label">Test Number</div><div className="value">{process.env.WA_TEST_NUMBER}</div></div>
|
||||
<div className="item"><div className="label">Login Username</div><div className="value">{process.env.WA_TEST_LOGIN_USERNAME || 'admin'}</div></div>
|
||||
<div className="item"><div className="label">Flow</div><div className="value">Kirim daftar, lalu kirim nama.</div></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
50
src/app/test/page.tsx
Normal file
50
src/app/test/page.tsx
Normal file
@ -0,0 +1,50 @@
|
||||
import Link from 'next/link'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { isAuthenticated } from '@/lib/auth'
|
||||
import { readJsonFile } from '@/lib/storage'
|
||||
|
||||
export default async function TestPage({ searchParams }: { searchParams: Promise<{ status?: string }> }) {
|
||||
if (!await isAuthenticated()) {
|
||||
redirect('/login')
|
||||
}
|
||||
|
||||
const logs = readJsonFile<any[]>('webhook-logs.json', [])
|
||||
const state = readJsonFile<Record<string, unknown>>('flow-state.json', {})
|
||||
const params = await searchParams
|
||||
|
||||
return (
|
||||
<div className="wrap">
|
||||
<div className="top">
|
||||
<div>
|
||||
<h1>Halaman Test</h1>
|
||||
<p>Nomor testing aktif: <strong>{process.env.WA_TEST_NUMBER}</strong></p>
|
||||
<p>Flow: kirim <strong>daftar</strong>, lalu kirim nama.</p>
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 10 }}>
|
||||
<Link className="btn" href="/settings">Settings</Link>
|
||||
<Link className="btn" href="/webhook-logs">Webhook Logs</Link>
|
||||
<form method="POST" action="/api/logout"><button className="btn" type="submit">Logout</button></form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
{params.status ? <div className="flash">{params.status}</div> : null}
|
||||
<form method="POST" action="/api/test/send">
|
||||
<label>Kirim pesan ke nomor testing</label>
|
||||
<textarea className="textarea" name="message" placeholder="Contoh: daftar" required />
|
||||
<div style={{ marginTop: 12 }}><button className="btn" type="submit">Kirim Pesan</button></div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<h2>State Flow</h2>
|
||||
<pre>{JSON.stringify(state, null, 2)}</pre>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<h2>Ringkasan Log</h2>
|
||||
<pre>{JSON.stringify(logs.slice(-10).reverse(), null, 2)}</pre>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
31
src/app/webhook-logs/page.tsx
Normal file
31
src/app/webhook-logs/page.tsx
Normal file
@ -0,0 +1,31 @@
|
||||
import Link from 'next/link'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { isAuthenticated } from '@/lib/auth'
|
||||
import { readJsonFile } from '@/lib/storage'
|
||||
|
||||
export default async function WebhookLogsPage() {
|
||||
if (!await isAuthenticated()) {
|
||||
redirect('/login')
|
||||
}
|
||||
|
||||
const logs = readJsonFile<any[]>('webhook-logs.json', [])
|
||||
|
||||
return (
|
||||
<div className="wrap">
|
||||
<div className="top">
|
||||
<div>
|
||||
<h1>Webhook Logs</h1>
|
||||
<p>Semua log webhook masuk, outgoing, dan status ada di sini.</p>
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 10 }}>
|
||||
<Link className="btn" href="/settings">Settings</Link>
|
||||
<Link className="btn" href="/test">Halaman Test</Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<pre>{JSON.stringify(logs.slice().reverse(), null, 2)}</pre>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
29
src/lib/auth.ts
Normal file
29
src/lib/auth.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import crypto from 'crypto'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
const COOKIE_NAME = 'wa_test_session'
|
||||
|
||||
function sign(value: string) {
|
||||
const secret = process.env.SESSION_SECRET || 'dev-secret'
|
||||
return crypto.createHmac('sha256', secret).update(value).digest('hex')
|
||||
}
|
||||
|
||||
export async function createSession() {
|
||||
const value = 'logged_in'
|
||||
const signed = `${value}.${sign(value)}`
|
||||
const store = await cookies()
|
||||
store.set(COOKIE_NAME, signed, { httpOnly: true, sameSite: 'lax', path: '/' })
|
||||
}
|
||||
|
||||
export async function clearSession() {
|
||||
const store = await cookies()
|
||||
store.delete(COOKIE_NAME)
|
||||
}
|
||||
|
||||
export async function isAuthenticated() {
|
||||
const store = await cookies()
|
||||
const raw = store.get(COOKIE_NAME)?.value
|
||||
if (!raw) return false
|
||||
const [value, signature] = raw.split('.')
|
||||
return value === 'logged_in' && signature === sign(value)
|
||||
}
|
||||
31
src/lib/storage.ts
Normal file
31
src/lib/storage.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
const dataDir = path.join(process.cwd(), 'data')
|
||||
|
||||
function ensureDir() {
|
||||
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true })
|
||||
}
|
||||
|
||||
export function readJsonFile<T>(fileName: string, fallback: T): T {
|
||||
ensureDir()
|
||||
const filePath = path.join(dataDir, fileName)
|
||||
if (!fs.existsSync(filePath)) return fallback
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf8')) as T
|
||||
} catch {
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
export function writeJsonFile(fileName: string, data: unknown) {
|
||||
ensureDir()
|
||||
const filePath = path.join(dataDir, fileName)
|
||||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2))
|
||||
}
|
||||
|
||||
export function appendLog(entry: unknown) {
|
||||
const logs = readJsonFile<unknown[]>('webhook-logs.json', [])
|
||||
logs.push(entry)
|
||||
writeJsonFile('webhook-logs.json', logs)
|
||||
}
|
||||
86
src/lib/whatsapp.ts
Normal file
86
src/lib/whatsapp.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import { appendLog, readJsonFile, writeJsonFile } from './storage'
|
||||
|
||||
type FlowState = Record<string, { step: string; name?: string }>
|
||||
|
||||
function normalizeNumber(number?: string | null) {
|
||||
return (number || '').replace(/\D+/g, '')
|
||||
}
|
||||
|
||||
export async function sendTextMessage(to: string, message: string) {
|
||||
const apiVersion = process.env.WHATSAPP_API_VERSION || 'v23.0'
|
||||
const phoneNumberId = process.env.WHATSAPP_PHONE_NUMBER_ID || ''
|
||||
const accessToken = process.env.WHATSAPP_ACCESS_TOKEN || ''
|
||||
const url = `https://graph.facebook.com/${apiVersion}/${phoneNumberId}/messages`
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
messaging_product: 'whatsapp',
|
||||
to,
|
||||
type: 'text',
|
||||
text: { preview_url: false, body: message },
|
||||
}),
|
||||
})
|
||||
|
||||
const payload = await response.json().catch(async () => ({ raw: await response.text() }))
|
||||
|
||||
appendLog({ type: 'outgoing_message', to, message, response: payload, success: response.ok, created_at: new Date().toISOString() })
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Gagal kirim pesan ke WhatsApp')
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
export async function handleWebhook(payload: any) {
|
||||
appendLog({ type: 'webhook_received', payload, created_at: new Date().toISOString() })
|
||||
|
||||
for (const entry of payload.entry || []) {
|
||||
for (const change of entry.changes || []) {
|
||||
const value = change.value || {}
|
||||
|
||||
for (const message of value.messages || []) {
|
||||
await handleIncomingMessage(message)
|
||||
}
|
||||
|
||||
for (const status of value.statuses || []) {
|
||||
appendLog({ type: 'message_status', status, created_at: new Date().toISOString() })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function handleIncomingMessage(message: any) {
|
||||
const from = message.from || ''
|
||||
appendLog({ type: 'incoming_message', from, message, created_at: new Date().toISOString() })
|
||||
|
||||
if (normalizeNumber(from) !== normalizeNumber(process.env.WA_TEST_NUMBER)) {
|
||||
appendLog({ type: 'ignored_message', reason: 'sender_not_test_number', from, created_at: new Date().toISOString() })
|
||||
return
|
||||
}
|
||||
|
||||
const body = (message.text?.body || '').trim()
|
||||
const state = readJsonFile<FlowState>('flow-state.json', {})
|
||||
const step = state[from]?.step || 'idle'
|
||||
|
||||
if (body.toLowerCase() === 'daftar' && step === 'idle') {
|
||||
state[from] = { step: 'waiting_name' }
|
||||
writeJsonFile('flow-state.json', state)
|
||||
await sendTextMessage(from, 'Silakan kirim nama Anda.')
|
||||
return
|
||||
}
|
||||
|
||||
if (step === 'waiting_name' && body) {
|
||||
state[from] = { step: 'completed', name: body }
|
||||
writeJsonFile('flow-state.json', state)
|
||||
await sendTextMessage(from, `Terima kasih ${body}, data testing diterima.`)
|
||||
return
|
||||
}
|
||||
|
||||
await sendTextMessage(from, 'Kirim "daftar" untuk mulai flow testing.')
|
||||
}
|
||||
23
tsconfig.json
Normal file
23
tsconfig.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2017",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": false,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"plugins": [{ "name": "next" }],
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user