Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 30 additions & 2 deletions declaration.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {InvoicePayload} from "./src/declarations/invoice-payload.interface";

export interface WebAppUser {
id: number;
is_bot?: boolean;
Expand Down Expand Up @@ -268,8 +266,38 @@ export interface Telegram {
WebView: any;
}

export interface InvoicePayload {
slug: string;
business_connection_id?: string;
title: string;
description: string;
payload: string;
provider_token?: string;
currency: string;
prices: {
label: string;
amount: number;
}[];
subscription_period?: number;
max_tip_amount?: number;
suggested_tip_amounts?: number[];
provider_data?: string;
photo_url?: string;
photo_size?: number;
photo_width?: number;
photo_height?: number;
need_name?: boolean;
need_phone_number?: boolean;
need_email?: boolean;
need_shipping_address?: boolean;
send_phone_number_to_provider?: boolean;
send_email_to_provider?: boolean;
is_flexible?: boolean;
}

declare global {
interface Window {
__TELEGRAM_APPS_ANALYTICS: 1 | undefined,
Telegram: Telegram,
TelegramGameProxy: any,
TelegramGameProxy_receiveEvent: (eventType: string, eventData: unknown) => void,
Expand Down
12 changes: 2 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"wasm-pack": "^0.13.1"
},
"dependencies": {
"@telegram-apps/sdk": "^1.1.0",
"@tonconnect/ui": "2.0.5",
"http-server": "14.1.1"
},
Expand Down
20 changes: 9 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NetworkController } from './controllers/Network.controller'
import { SessionController } from './controllers/Session.controller'
import { BatchService } from "./services/Batch.service";
import { HumanProofService } from "./services/HumanProof.service";
import {InvoicePayload} from "./declarations/invoice-payload.interface";
import {InvoicePayload} from "../declaration";
import {Events} from "./constants";

export class App {
Expand All @@ -16,8 +16,8 @@ export class App {
private readonly apiToken: string;
private readonly appName: string;

public taskParams: string;
public taskSolution: string | undefined;
public taskParams: string | undefined | null = null;

public env: 'STG' | 'PROD';

constructor(apiToken: string, appName: string, env: 'STG' | 'PROD') {
Expand All @@ -35,9 +35,7 @@ export class App {
public async init() {
this.sessionController.init();
await this.analyticsController.init();
await this.humanProofService.init().then(() => {
this.solveTask();
}).catch(e => console.error(e));
await this.humanProofService.init().catch(e => console.error(e));
this.networkController.init();
this.batchService.init();
}
Expand Down Expand Up @@ -82,11 +80,11 @@ export class App {
return this.appName;
}

public solveTask() {
this.humanProofService.solveTask();
}
public async solveHumanProofTask(): Promise<string | undefined> {
if (this.taskParams === null) {
this.taskParams = await this.humanProofService.getInitialParams().catch(_err => undefined);
}

public setNewArgs(data: string) {
this.humanProofService.setNewArgs(data);
return await this.humanProofService.solveTask(this.taskParams).catch(_err => undefined);
}
}
4 changes: 0 additions & 4 deletions src/controllers/Analytics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ export class AnalyticsController {
}
}

public recordEvent(event_name: string, data?: Record<string, any>) {
this.appModule.recordEvent(event_name, data).catch(e => console.error(e));
}

public collectEvent(event_name: string, data?: Record<string, any>) {
if (this.eventsThreshold[event_name] === 0) {
return;
Expand Down
39 changes: 5 additions & 34 deletions src/controllers/Network.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ export class NetworkController {
return res;
}

private readonly generateHeaders = (compressed: boolean) => {
this.appModule.solveTask();

private readonly generateHeaders = async (compressed: boolean) => {
const conditionHeaders = {};
const solution: string | undefined = await this.appModule.solveHumanProofTask();

if (this.appModule.taskSolution) {
conditionHeaders["Content"] = this.appModule.taskSolution;
if (solution) {
conditionHeaders["Content"] = solution;
}

if (compressed) {
Expand All @@ -58,36 +57,8 @@ export class NetworkController {
) {
return await fetch(this.BACKEND_URL + 'events',{
method: 'POST',
headers: this.generateHeaders(compressed),
headers: await this.generateHeaders(compressed),
body: compressed ? await compressData(data) : JSON.stringify(data),
}).then(this.responseToParams, this.responseToParams);
}

public async recordEvent(
event_name: string,
data?: Record<string, any>,
attributes?: Record<string, any>,
compressed: boolean = true,
) {
if (data?.custom_data) {
if (!attributes) {
attributes = data.custom_data;
} else {
attributes = Object.assign(data.custom_data, attributes);
}
}

const body = {
...data,
event_name: event_name,
custom_data: attributes,
...this.appModule.assembleEventSession(),
};

await fetch(this.BACKEND_URL + 'events',{
method: 'POST',
headers: this.generateHeaders(true),
body: compressed ? await compressData(body) : JSON.stringify(body),
}).then(this.responseToParams, this.responseToParams);
}
}
32 changes: 10 additions & 22 deletions src/controllers/Session.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { retrieveLaunchParams } from '@telegram-apps/sdk';
import { WebAppUser } from '@twa-dev/types'
import { App } from '../app'
import { Errors, throwError } from '../errors'
import { generateUUID } from '../utils/generateUUID';
import { retrieveLaunchParams } from "../utils/retrieveLaunchParams";
import { WebAppUser } from "../../declaration";

export class SessionController {
private sessionId: string;
private userId: number;
private userData: WebAppUser;
private platform: string;
private webAppStartParam: string;
private userLocale: string;

private appModule: App;

Expand All @@ -20,25 +18,15 @@ export class SessionController {

public init() {
const lp = retrieveLaunchParams();
const initData = lp.initData;
const user = lp.initData?.user;
if (!user) {
const initData = lp.tgWebAppData;

this.userData = initData?.user;

if (!this.userData) {
throwError(Errors.USER_DATA_IS_NOT_PROVIDED);
}

this.userData = {
id: user.id,
is_premium: user.isPremium,
first_name: user.firstName,
is_bot: user.isBot,
last_name: user.lastName,
language_code: user.languageCode,
photo_url: user.photoUrl,
username: user.username,
};
this.userId = user.id;
this.userLocale = user.languageCode;
this.webAppStartParam = initData.startParam;
this.webAppStartParam = initData.start_param;
this.platform = lp.platform;
this.sessionId = generateUUID(String(this.getUserId()));
}
Expand All @@ -48,7 +36,7 @@ export class SessionController {
}

public getUserId() {
return this.userId;
return this.userData.id;
}

public getWebAppStartParam() {
Expand All @@ -60,7 +48,7 @@ export class SessionController {
}

public getUserLocale() {
return this.userLocale;
return this.userData.language_code;
}

public getUserData() {
Expand Down
28 changes: 0 additions & 28 deletions src/declarations/invoice-payload.interface.ts

This file was deleted.

10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App } from './app'
import { InvoicePayload } from "./declarations/invoice-payload.interface";
import {validateInvoicePayload} from "./validators/invoice-payload.validator";
import { InvoicePayload } from "../declaration";
import { validateInvoicePayload } from "./validators/invoice-payload.validator";

let __registerInvoice: (invoicePayload: InvoicePayload) => void;

Expand All @@ -9,6 +9,12 @@ async function init({ token, appName, env = 'PROD'}: {
appName: string,
env?: 'STG' | 'PROD',
}) {
if (window.__TELEGRAM_APPS_ANALYTICS) {
return;
}

window.__TELEGRAM_APPS_ANALYTICS = 1;

const app = new App(token, appName, env);

__registerInvoice = (invoicePayload: InvoicePayload) => {
Expand Down
Loading