Skip to content

Commit

Permalink
register user on start (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: timur <[email protected]>
  • Loading branch information
PriestFaria and timur authored Dec 30, 2024
1 parent bc2eddc commit b5c63c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/api/createUser.ts
Original file line number Diff line number Diff line change
@@ -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<User | null> => {
try {
const res = await axios.post<User>(`${API_BASE_URL}/users`, user);
return res.data;
} catch (err) {
console.error(err);
return null;
}
};
4 changes: 4 additions & 0 deletions src/api/interfaces/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface User {
name: string;
telegram: number;
}
9 changes: 8 additions & 1 deletion src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyContext>,
metricService: IMetricService,
Expand All @@ -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") {
Expand Down

0 comments on commit b5c63c2

Please sign in to comment.