From b5c63c2438dce21b68c7360c5f4d911743765256 Mon Sep 17 00:00:00 2001 From: Timur Valeev <76071256+PriestFaria@users.noreply.github.com> Date: Mon, 30 Dec 2024 21:22:09 +0300 Subject: [PATCH] register user on start (#19) Co-authored-by: timur --- src/api/createUser.ts | 13 +++++++++++++ src/api/interfaces/User.ts | 4 ++++ src/commands/start.ts | 9 ++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/api/createUser.ts create mode 100644 src/api/interfaces/User.ts diff --git a/src/api/createUser.ts b/src/api/createUser.ts new file mode 100644 index 0000000..673f69a --- /dev/null +++ b/src/api/createUser.ts @@ -0,0 +1,13 @@ +import axios from "axios"; +import { API_BASE_URL } from "src/config"; +import User from "./interfaces/User"; + +export const createUser = async (user: User): Promise => { + try { + const res = await axios.post(`${API_BASE_URL}/users`, user); + return res.data; + } catch (err) { + console.error(err); + return null; + } +}; diff --git a/src/api/interfaces/User.ts b/src/api/interfaces/User.ts new file mode 100644 index 0000000..890248e --- /dev/null +++ b/src/api/interfaces/User.ts @@ -0,0 +1,4 @@ +export default interface User { + name: string; + telegram: number; +} diff --git a/src/commands/start.ts b/src/commands/start.ts index eecc033..049095e 100644 --- a/src/commands/start.ts +++ b/src/commands/start.ts @@ -3,7 +3,8 @@ import { BOT_USERNAME } from "src/config"; import { IMetricService } from "src/services/metricService"; import logger from "src/utils/logger"; import { Telegraf } from "telegraf"; - +import { createUser } from "src/api/createUser"; +import User from "src/api/interfaces/User"; export function setupStartCommand( bot: Telegraf, metricService: IMetricService, @@ -12,6 +13,12 @@ export function setupStartCommand( const userId = ctx.from.id; metricService.sendTagEvent(userId, ctx.payload); + const user: User = { + name: ctx.from.first_name, + telegram: userId, + }; + + await createUser(user); const paramsPayload = ctx.payload; if (paramsPayload === "feedback") {