Files
marketplaces/src/app/services/telegram.service.ts
2026-02-26 23:09:20 +04:00

40 lines
931 B
TypeScript

import { Injectable } from '@angular/core';
import type { } from '../types/telegram.types';
@Injectable({
providedIn: 'root'
})
export class TelegramService {
private tg = typeof window !== 'undefined' ? window.Telegram?.WebApp : null;
isTelegramApp(): boolean {
return !!this.tg;
}
getUser() {
return this.tg?.initDataUnsafe?.user || null;
}
getUserId(): number | null {
return this.tg?.initDataUnsafe?.user?.id || null;
}
getUsername(): string | null {
return this.tg?.initDataUnsafe?.user?.username || null;
}
getFirstName(): string | null {
return this.tg?.initDataUnsafe?.user?.first_name || null;
}
getFullName(): string | null {
const user = this.getUser();
if (!user) return null;
return `${user.first_name}${user.last_name ? ' ' + user.last_name : ''}`;
}
getDisplayName(): string {
return this.getUsername() || this.getFullName() || 'User';
}
}