Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
226 commits
Select commit Hold shift + click to select a range
3bc96e6
Update README.md
gerryhubai Sep 8, 2024
ae4c6da
attempt to create separate friends page and home page
gerryhubai Sep 8, 2024
b814591
added state variables
gerryhubai Sep 8, 2024
5cc886d
added other pages
gerryhubai Sep 8, 2024
a134b9f
corrected link import
gerryhubai Sep 8, 2024
9f116be
closed import to prevent error
gerryhubai Sep 8, 2024
3029a92
removed parenthesis
gerryhubai Sep 8, 2024
15bd6f5
made background more beautiful
gerryhubai Sep 8, 2024
5c22f1c
added use client
gerryhubai Sep 8, 2024
861286d
created card component
gerryhubai Sep 8, 2024
84a9fc0
attempt to fix the error
gerryhubai Sep 8, 2024
61299c0
attempt
gerryhubai Sep 8, 2024
bff1d65
added use client
gerryhubai Sep 8, 2024
4154155
removed card import
gerryhubai Sep 8, 2024
892166e
Attempt to fix card props
gerryhubai Sep 8, 2024
f3d6d2a
rerun code
gerryhubai Sep 8, 2024
acea01c
attempt to add christmas tree to bg
gerryhubai Sep 8, 2024
839b45d
attempt to fix way of rotation
gerryhubai Sep 8, 2024
0bb8064
changed image to png
gerryhubai Sep 8, 2024
f8023e5
renamed image correctly
gerryhubai Sep 8, 2024
851bd1d
attempt to have correct rotation
gerryhubai Sep 9, 2024
7a4fc89
changed file paths well
gerryhubai Sep 9, 2024
2705adb
adding footer and changing background
gerryhubai Sep 9, 2024
5465d30
rerun
gerryhubai Sep 9, 2024
340f963
try to let tree show
gerryhubai Sep 9, 2024
84af1fc
changing game logic to falling snow flakes
gerryhubai Sep 9, 2024
009237c
new tailwindcss features
gerryhubai Sep 9, 2024
c07d532
remove unnecessary snowflake import
gerryhubai Sep 9, 2024
ef077c7
fixing small errors
gerryhubai Sep 9, 2024
3382d23
still trying to improve
gerryhubai Sep 9, 2024
3288878
Added footer in layout.tsx
gerryhubai Sep 9, 2024
50249be
Update footer and Navlink
gerryhubai Sep 9, 2024
eb97332
Update footer and Navlink
gerryhubai Sep 9, 2024
37e6da5
Update footer and Navlink
gerryhubai Sep 9, 2024
488cbf9
Update footer and Navlink
gerryhubai Sep 9, 2024
15137a1
Update layout.tsx to update footer and use NavLink
gerryhubai Sep 9, 2024
4c0d976
Update page.tsx to improve snowflakes falling and implement local sto…
gerryhubai Sep 9, 2024
e2470f5
added use client
gerryhubai Sep 9, 2024
2c5b617
Update layout.tsx
gerryhubai Sep 9, 2024
35f2264
Created ClientLayout.tsx
gerryhubai Sep 9, 2024
5f637d2
Update layout.tsx to use clientLayout.tsx
gerryhubai Sep 9, 2024
a6712e8
Update page.tsx
gerryhubai Sep 9, 2024
14a0a7e
Update layout.tsx
gerryhubai Sep 9, 2024
9e6b1b3
Update layout.tsx to use real code
gerryhubai Sep 9, 2024
be01321
Update ClientLayout.tsx
gerryhubai Sep 9, 2024
cef3e84
created tasks component
gerryhubai Sep 9, 2024
125748f
Created friends route
gerryhubai Sep 9, 2024
ce26235
Update page.tsx
gerryhubai Sep 9, 2024
8c072d1
Update page.tsx
gerryhubai Sep 9, 2024
6b2d5ee
Update page.tsx
gerryhubai Sep 9, 2024
0b8df66
removed christmas tree to trees
gerryhubai Sep 9, 2024
c9b882a
coded referral aspect
gerryhubai Sep 9, 2024
65deb64
Made merry christmas clickable
gerryhubai Sep 9, 2024
3edfdf0
Update page.tsx to attempt to fix abnormal snowflake generation
gerryhubai Sep 9, 2024
4b6b566
Update tailwind.config.ts to attempt to fix abnormal generation errors
gerryhubai Sep 9, 2024
e02a3df
Update page.tsx to eliminate abnormal generation of emojis
gerryhubai Sep 9, 2024
d1bcb86
added required packages to package.json
gerryhubai Sep 9, 2024
ee550e5
Created user route
gerryhubai Sep 9, 2024
6574336
added geoip lite
gerryhubai Sep 9, 2024
83ee4ed
removed additional @
gerryhubai Sep 9, 2024
d8a79ef
Updated user route
gerryhubai Sep 9, 2024
78b5517
Update route.ts again
gerryhubai Sep 9, 2024
72317fe
attemp
gerryhubai Sep 9, 2024
d5a5295
stopped using geoip and started using ipapi.co api
gerryhubai Sep 9, 2024
c79d941
updated page.tsx to work with backend
gerryhubai Sep 9, 2024
36a636e
modified friends to work with backend and count number of friends
gerryhubai Sep 9, 2024
2324544
called chistmas tree the right way
gerryhubai Sep 9, 2024
27ce0ed
updated to keep coinbalance synchrony and prevent overflow:hidden acr…
gerryhubai Sep 9, 2024
0c0b603
attempt to fix errors
gerryhubai Sep 9, 2024
5b7c9b7
updating localStorage each time we get coins
gerryhubai Sep 9, 2024
21c221a
fix coin balance rendering issues and enhance footer behavious
gerryhubai Sep 9, 2024
f006aac
fixing balance fetching error and overflow on main page
gerryhubai Sep 9, 2024
84d022a
store and retrieve coin balance from local storage only to save mysel…
gerryhubai Sep 9, 2024
6f2f0a0
insert all missing elements
gerryhubai Sep 9, 2024
9cff6c7
retrieves data from localstorage immediately component mounts
gerryhubai Sep 9, 2024
8d2273e
enhanced thet task ui
gerryhubai Sep 9, 2024
b5216aa
modified lucide-react tree import to trees
gerryhubai Sep 9, 2024
1caf34a
attempt to fetch tasks more appropriately
gerryhubai Sep 9, 2024
8830ca3
added react-icons to package json
gerryhubai Sep 9, 2024
4f2cd02
used ' instead of '
gerryhubai Sep 9, 2024
d91bd72
implement addition of rewards to localstorage coins value and used ho…
gerryhubai Sep 9, 2024
7108acc
fixed jsx syntax and used santa's head as home also adding task rewar…
gerryhubai Sep 9, 2024
3d4b8a7
fixing localstorage issues
gerryhubai Sep 9, 2024
8755bb0
fixing local storage code at right place
gerryhubai Sep 9, 2024
9f7460b
fixing things around
gerryhubai Sep 9, 2024
37aa0fc
upgraded task completed ui
gerryhubai Sep 9, 2024
e5437c7
task completed design improve
gerryhubai Sep 9, 2024
283a1e4
Update the code to prevent users not opening from mobile on telegram
gerryhubai Sep 9, 2024
b43caf9
update clientLayout
gerryhubai Sep 9, 2024
35f8ece
changed way of checking whether app is from telegram
gerryhubai Sep 9, 2024
dc49e06
removed telegram check
gerryhubai Sep 9, 2024
16749a3
prevent premature access denied message render
gerryhubai Sep 9, 2024
5372956
changed threshhold to be between 9 and 12
gerryhubai Sep 9, 2024
46fd4fb
Create gamemusic folder
gerryhubai Sep 11, 2024
754cf6b
Add files via upload
gerryhubai Sep 11, 2024
d3fdd2e
added sound effects
gerryhubai Sep 11, 2024
506c76f
inccreased tap threshold from 9-12 to 12-20
gerryhubai Sep 11, 2024
2627b5c
added treasure chest image
gerryhubai Sep 11, 2024
fda81fe
changed bell icon to chest image
gerryhubai Sep 11, 2024
c6c0259
restore to working code
gerryhubai Sep 11, 2024
82b08e3
updated image to use navlink
gerryhubai Sep 12, 2024
6318b0f
corrected image file path
gerryhubai Sep 12, 2024
a8527d5
added treasure chest png
gerryhubai Sep 12, 2024
2288e6e
changed jpg to png
gerryhubai Sep 12, 2024
736583e
attempt to let the snow flakes fall below the coin balance and flakes…
gerryhubai Sep 12, 2024
dd39410
added z-30 index to the footer
gerryhubai Sep 12, 2024
ca79fdb
created chest page
gerryhubai Sep 12, 2024
adc0c91
added framer-motion
gerryhubai Sep 12, 2024
34244f4
added canvas-convetti dependency
gerryhubai Sep 12, 2024
cd10ac7
added ^ to dep version
gerryhubai Sep 12, 2024
73fcf94
attempt to fix type erros
gerryhubai Sep 12, 2024
36ab927
attempt another implementation
gerryhubai Sep 12, 2024
5cc2fba
another
gerryhubai Sep 12, 2024
e8aad35
usinig css rather for animation
gerryhubai Sep 12, 2024
393d070
created dailychest route
gerryhubai Sep 12, 2024
f2be01b
modified to claim up to 10 times
gerryhubai Sep 12, 2024
0d55508
attempt again
gerryhubai Sep 12, 2024
efa9b44
telemas
gerryhubai Sep 12, 2024
2e5207a
Attempt to make daily chest more dynamic
gerryhubai Sep 12, 2024
11e0a85
Correct types in code
gerryhubai Sep 12, 2024
2834c45
Update daily chest page
gerryhubai Sep 12, 2024
9f01a1e
added congratulations and goodresult music
gerryhubai Sep 13, 2024
24fe616
enhanced quest page
gerryhubai Sep 13, 2024
e26313c
Created giftcards folder
gerryhubai Sep 13, 2024
ee07c4d
added gift card images
gerryhubai Sep 13, 2024
feb6033
Enhance quest page to include gift cards
gerryhubai Sep 13, 2024
76b2b73
Enhance quest page to include gift cards
gerryhubai Sep 13, 2024
fc42566
Enhancing gift card on smaller screens
gerryhubai Sep 13, 2024
1e68828
reset to opencount to 0 for testing\
gerryhubai Sep 13, 2024
2bcfa4d
Update testing
gerryhubai Sep 13, 2024
841883b
Updated tasks
gerryhubai Sep 13, 2024
7ec627b
Place image stack button at top the page and track gift cards correctly
gerryhubai Sep 13, 2024
a0ae11f
removed overflow hidden\
gerryhubai Sep 13, 2024
b1cba00
render number of cards properly
gerryhubai Sep 13, 2024
a583de6
modified friends/page.tsx to attempt to correctly render number of fr…
gerryhubai Sep 13, 2024
af06227
Update FRIENDS PAGE
gerryhubai Sep 13, 2024
4b8837a
Update friends page
gerryhubai Sep 13, 2024
ad35aca
Update friends/page.tsx
gerryhubai Sep 13, 2024
52fe84b
Update page.tsx in friends
gerryhubai Sep 13, 2024
382a434
go back to old code
gerryhubai Sep 13, 2024
fb4a9a8
Update ReferralSystem.tsx
gerryhubai Sep 13, 2024
b94f98d
Update ReferralSystem.tsx
gerryhubai Sep 13, 2024
66a4642
Create useAdsgram.ts
gerryhubai Sep 14, 2024
49fb56e
added adsgram cdn code to layout.tsx
gerryhubai Sep 14, 2024
912974b
ATTEMPT TO INTEGRATE ADSGRAM
gerryhubai Sep 14, 2024
617bef0
integrating adsgram
gerryhubai Sep 14, 2024
4b4b568
correctly defined giftCard
gerryhubai Sep 14, 2024
f3a5a8e
Update random ad triggers type
gerryhubai Sep 14, 2024
11b67a2
restore chest page to adless code
gerryhubai Sep 14, 2024
5d5286b
Modify user endpoint to add reward to users for adwatch
gerryhubai Sep 14, 2024
2dbdcdd
Update get function logic
gerryhubai Sep 14, 2024
8dd422b
Create adsButton.tsx
gerryhubai Sep 14, 2024
0919286
Removed Adwatch button
gerryhubai Sep 14, 2024
5f396b0
attempt to remove ads button properly
gerryhubai Sep 14, 2024
ea73da4
commenting out unused code
gerryhubai Sep 14, 2024
b1a7322
Go back to old code
gerryhubai Sep 14, 2024
0452b0d
Remove everything about Adsgram or ads
gerryhubai Sep 14, 2024
b4990e5
Remove everything about Adsgram or ads
gerryhubai Sep 14, 2024
b7c6424
empty adsbutton,tsx\
gerryhubai Sep 14, 2024
ee50702
Restore page.tsx
gerryhubai Sep 14, 2024
4abb549
Add rocket to footer
gerryhubai Sep 14, 2024
e5865e3
Update ClientLayout.tsx to restore code
gerryhubai Sep 14, 2024
5d34bfa
Attempt to implement buying cards with ads
gerryhubai Sep 14, 2024
7473a2b
attempt to create card market
gerryhubai Sep 14, 2024
82e8713
Update page.tsx to check adwatch
gerryhubai Sep 14, 2024
fc46cb4
Attempt to add database implementation
gerryhubai Sep 14, 2024
e3b20c8
Fixed useEffect error
gerryhubai Sep 14, 2024
b34de95
Fix motion error
gerryhubai Sep 14, 2024
2cd4d7d
Create adsgram.d.ts
gerryhubai Sep 15, 2024
fef18fc
Return to old code
gerryhubai Sep 15, 2024
b49511a
Use Adsgram Adcontroller for purchase
gerryhubai Sep 15, 2024
e50e97b
added use client
gerryhubai Sep 15, 2024
d8d5dc1
attempt ad watch again
gerryhubai Sep 15, 2024
c3294b6
returning again
gerryhubai Sep 15, 2024
954c12b
corrected adsgram sdk code
gerryhubai Sep 15, 2024
4cdaddd
Removed debug params
gerryhubai Sep 15, 2024
12a75a5
set debug to false
gerryhubai Sep 15, 2024
2d44929
modify handleBuyCard function to ensure ads are watched before cards …
gerryhubai Sep 15, 2024
9302477
Update useAdsgram.ts to trigger callback when an ad is watched fully
gerryhubai Sep 15, 2024
06eac17
modify handleBuyCard function to use onReward callback
gerryhubai Sep 15, 2024
6afb6f8
imported show promise result
gerryhubai Sep 15, 2024
6651e57
removed typo
gerryhubai Sep 15, 2024
aaefa4f
removed unecessary parameters
gerryhubai Sep 15, 2024
69771b5
attempt to fix multiple ad card glitch
gerryhubai Sep 15, 2024
27ea460
still fixing card purchase glitches
gerryhubai Sep 15, 2024
b3e1287
still fixing
gerryhubai Sep 15, 2024
76af4ac
still fixing
gerryhubai Sep 15, 2024
d5ae848
changed all pricies to 1 for now
gerryhubai Sep 15, 2024
937afd3
attempt to insert new users into databaes
gerryhubai Sep 15, 2024
380445c
Update page.tsx
gerryhubai Sep 15, 2024
1797af3
fix useeffect mismatch
gerryhubai Sep 15, 2024
2121bf1
Update page.tsx
gerryhubai Sep 15, 2024
1774f82
attempt error fixes
gerryhubai Sep 15, 2024
8165431
Update page.tsx
gerryhubai Sep 15, 2024
8efda37
commented out referral id code
gerryhubai Sep 15, 2024
5c5cc8f
Update useAdsgram.ts to mirror other states
gerryhubai Sep 15, 2024
8b91085
Update useAdsgram.ts to mirror other states
gerryhubai Sep 15, 2024
a948e2e
Update useAdsgram.ts
gerryhubai Sep 15, 2024
6e34325
Update useAdsgram.ts
gerryhubai Sep 15, 2024
e3b5b55
Update page.tsx to mirror other states
gerryhubai Sep 15, 2024
9906e1c
let's see
gerryhubai Sep 15, 2024
2f65d37
Update page.tsx
gerryhubai Sep 15, 2024
12d0d81
add a 16 second delay after showAd is called
gerryhubai Sep 15, 2024
f62bb2d
attempt to add 3 second delay
gerryhubai Sep 15, 2024
670620e
restore
gerryhubai Sep 15, 2024
819d5a9
Update useAdsgram.ts
gerryhubai Sep 15, 2024
6680290
add 16 sec delay
gerryhubai Sep 15, 2024
4115950
Update page.tsx
gerryhubai Sep 15, 2024
80554db
added dappfarm to telemas
gerryhubai Sep 15, 2024
410d4e5
Update route.ts
gerryhubai Sep 15, 2024
9c796a9
Update package.json to include vercel analytics
gerryhubai Sep 15, 2024
77408cb
Update page.tsx to add analytics
gerryhubai Sep 15, 2024
70e0fbf
Update page.tsx to add analytics
gerryhubai Sep 15, 2024
59277d7
attempt to capture user data in database
gerryhubai Sep 16, 2024
3f53f5a
Removed multiple state declarations
gerryhubai Sep 16, 2024
e9798ec
added dynamic import of webapp
gerryhubai Sep 16, 2024
f61e264
changed ids to string
gerryhubai Sep 16, 2024
84ca849
storing userOpenedBefore in localstorage
gerryhubai Sep 16, 2024
8b38b9a
update telemas gold twitter url
gerryhubai Sep 24, 2024
504e1d4
Update page.tsx
gerryhubai Dec 15, 2024
1c3d6e7
Update page.tsx to add daily. check-in
gerryhubai Dec 15, 2024
fe7ccc3
redeploy
gerryhubai Dec 15, 2024
f2c1aeb
redeploy
gerryhubai Dec 15, 2024
a349c4a
Update page.tsx to remove last export
gerryhubai Dec 15, 2024
6b670e6
Update ClientLayout.tsxyo set is allowed to true
gerryhubai Dec 15, 2024
fb5bf65
Update ClientLayout.tsx to set isallowed to ismobile
gerryhubai Dec 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,69 +1 @@
# Make TON Telegram Mini App 3: Referral System, Invite Friend Button, Invite Link

Welcome to the third guide in the **Make TON Telegram Mini App** series! This project demonstrates how to implement a referral system, an invite friend button, and an invite link in a Telegram Mini App using Next.js 14.

## Project Overview

This Telegram Mini App showcases:
- Setting up a Next.js 14 project with TypeScript and Tailwind CSS
- Implementing a simple referral system using file-based storage
- Creating an invite friend button that opens the Telegram share dialog
- Generating and copying invite links
- Displaying referral information and referred users
- Basic TypeScript usage for type safety

## Prerequisites

- Node.js (version 14 or higher)
- npm (comes with Node.js)
- A Telegram account
- A Telegram Bot Token
- GitHub account
- Vercel account

## Getting Started

1. Clone the repository:
```
git clone https://github.com/your-username/Make-TON-Telegram-Mini-App-3.git
cd Make-TON-Telegram-Mini-App-3
```

2. Install dependencies:
```
npm install
```

## Deployment and Usage

As this is a Telegram Mini App, you can't see the result directly in development mode. Follow these steps to deploy and use the app:

1. Push your code to a GitHub repository.
2. Sign up for a Vercel account if you haven't already.
3. Connect your GitHub repository to Vercel and deploy the app.
4. Once deployed, Vercel will provide you with a URL for your app.
5. Use this URL to set up your Telegram Mini App:
- Go to [@BotFather](https://t.me/BotFather) on Telegram
- Send the command `/newapp` or choose to edit an existing bot
- Follow the prompts to set up your Mini App, using the Vercel URL as the Web App URL
6. Once set up, you can access your Mini App through Telegram on mobile devices or in the Web version of Telegram.

## Project Structure

- `app/page.tsx`: Main page component
- `components/ReferralSystem.tsx`: Referral system component
- `app/api/referrals/route.ts`: API route for handling referrals
- `lib/storage.ts`: In-memory storage utility functions

## Note on Data Storage

This demo uses an in-memory storage solution for simplicity and to avoid file system permission issues in serverless environments. In a production application, you should use a database or other persistent storage solution to maintain referral data across server restarts and multiple instances.

## YouTube Channel

For video tutorials and more in-depth explanations, check out my YouTube channel:
[Nikandr Surkov](https://www.youtube.com/@NikandrSurkov)

## Next Steps

Stay tuned for the next guide in the **Make TON Telegram Mini App** series, where we'll explore more advanced features and TON integration!
91 changes: 91 additions & 0 deletions app/ClientLayout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use client';

import React, { useEffect, useState } from 'react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { Bell, Users, CheckSquare } from 'lucide-react';

const NavLink = ({ href, children }: { href: string; children: React.ReactNode }) => {
const pathname = usePathname();
const isActive = pathname === href;
return (
<Link href={href} className={`flex items-center space-x-2 ${isActive ? 'text-yellow-400' : 'text-white hover:text-yellow-200'}`}>
{children}
</Link>
);
};

const ClientLayout = ({ children }: { children: React.ReactNode }) => {
const pathname = usePathname();
const isMainPage = pathname === '/';

// Track loading state to avoid showing access denied prematurely
const [isLoading, setIsLoading] = useState(true);
const [isAllowed, setIsAllowed] = useState(false);

useEffect(() => {
const userAgent = navigator.userAgent || navigator.vendor;

// Check if the user is on a mobile device
const isMobile = /android|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent);

// Set access permission based on mobile detection
setIsAllowed(isMobile);

// Once the check is done, stop loading
setIsLoading(false);
}, []);

// While checking, show a loading screen to avoid flashing Access Denied
if (isLoading) {
return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<div className="text-2xl font-bold">Loading...</div>
</div>
);
}

// If the user is not allowed (not on mobile), show Access Denied
if (!isAllowed) {
return (
<div className="flex items-center justify-center min-h-screen bg-gray-100 text-center">
<div className="p-4 bg-red-800 text-white rounded-lg">
<h1 className="text-2xl font-bold mb-2">Access Denied</h1>
<p>This web app can only be accessed on a mobile device.</p>
</div>
</div>
);
}

// If the user is on mobile, render the app
return (
<div className="flex flex-col min-h-screen">
<main className={`flex-grow ${isMainPage ? '' : 'overflow-auto pb-20'}`}>
{children}
</main>
<footer className="bg-red-900 text-white py-4 px-6 flex justify-between items-center fixed bottom-0 left-0 right-0 z-30">
<div className="flex items-center space-x-4">
<NavLink href="/dailychest">
<img src="telemas-treasure-chest.png" alt="Quest Chest" className="w-8 h-8" />
</NavLink>
<Link href="/">
<span className="font-bold text-4xl cursor-pointer">🎅</span>
</Link>
</div>
<nav className="flex space-x-8">
<NavLink href="/friends">
<Users size={24} />
<span className="text-lg">Friends</span>
</NavLink>
<NavLink href="/tasks">
<CheckSquare size={24} />
<span className="text-lg">Tasks</span>
</NavLink>
</nav>
</footer>

</div>
);
};

export default ClientLayout;
1 change: 1 addition & 0 deletions app/adsButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

40 changes: 40 additions & 0 deletions app/api/tasks/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NextResponse } from 'next/server';

const tasks = [
{
id: 0,
title: "Play Dapp Farm",
description: "Play, learn and earn with our partners",
link: "https://t.me/DappFarmBot/earn",
reward: 1500,
emoji: "🎮"
},
{
id: 7,
title: "Join our Telegram Channel",
description: "Join our Telegram channel for the latest updates!",
link: "https://t.me/telemasgold",
reward: 1000,
emoji: "📢"
},
{
id: 8,
title: "Follow us on Twitter",
description: "Follow us on Twitter for quick updates and news!",
link: "https://twitter.com/@TelemasGold",
reward: 1500,
emoji: "🐦"
},
{
id: 9,
title: "Subscribe to our YouTube",
description: "Subscribe to our YouTube channel for exciting videos!",
link: "https://www.youtube.com/@telemasgold",
reward: 1500,
emoji: "🎥"
},
];

export async function GET() {
return NextResponse.json(tasks);
}
72 changes: 72 additions & 0 deletions app/api/user/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { NextResponse } from 'next/server';
import { sql } from '@vercel/postgres';
import fetch from 'node-fetch';

async function getCountryFromIP(ip: string): Promise<string> {
try {
const response = await fetch(`https://ipapi.co/${ip}/country_name/`);
return await response.text() || 'Unknown';
} catch (error) {
console.error('Error fetching country:', error);
return 'Unknown';
}
}

export async function POST(request: Request) {
const { telegram_id, telegram_username, referrer_id, coin_balance } = await request.json();
const ip = request.headers.get('x-forwarded-for')?.split(',')[0] || '0.0.0.0';
const country = await getCountryFromIP(ip);

try {
await sql`
INSERT INTO users (telegram_id, telegram_username, country, referrer_id, coin_balance)
VALUES (${telegram_id}, ${telegram_username}, ${country}, ${referrer_id}, ${coin_balance})
ON CONFLICT (telegram_id)
DO UPDATE SET
telegram_username = EXCLUDED.telegram_username,
country = EXCLUDED.country,
coin_balance = EXCLUDED.coin_balance,
last_update = CURRENT_TIMESTAMP
`;

return NextResponse.json({ success: true });
} catch (error) {
return NextResponse.json({ success: false, error: (error as Error).message });
}
}

export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const telegram_id = searchParams.get('telegram_id');
const rewardEvent = searchParams.get('reward');

if (rewardEvent && rewardEvent === 'true' && telegram_id) {
// Handle the reward event
try {
// Simulate reward logic (update coin balance for example)
await sql`
UPDATE users
SET coin_balance = coin_balance + 500
WHERE telegram_id = ${telegram_id}
`;

return NextResponse.json({ success: true, message: `Reward applied to user ${telegram_id}` });
} catch (error) {
return NextResponse.json({ success: false, error: (error as Error).message });
}
}

if (!telegram_id) {
return NextResponse.json({ success: false, error: 'Telegram ID is required' });
}

try {
const referralCount = await sql`
SELECT COUNT(*) FROM users WHERE referrer_id = ${telegram_id}
`;

return NextResponse.json({ success: true, referralCount: referralCount.rows[0].count });
} catch (error) {
return NextResponse.json({ success: false, error: (error as Error).message });
}
}
Loading