Skip to content

Commit f599e27

Browse files
committed
API Endpoint
1 parent 6cba4c9 commit f599e27

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/app/api/telegram/route.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path";
2-
import { promises as fs } from "fs";
2+
import { promises as fs, readFile } from "fs";
33

44
const channelDataPath = path.join(process.cwd(), "src/lib/formData.json");
55
const helperPath = path.join(process.cwd(), "src/lib/telegramHelper.json");
@@ -34,6 +34,13 @@ const updateId = async () => {
3434

3535
//Fetch For New Chats
3636
const getUpdates = async () => {
37+
const helperExists = await fs
38+
.stat(helperPath)
39+
.then(() => true)
40+
.catch(() => false);
41+
if (!helperExists)
42+
await fs.writeFile(helperPath, JSON.stringify({ update_id: 0 }));
43+
3744
var helperDataString = await fs.readFile(helperPath, "utf-8");
3845
const helperData: HelperData = JSON.parse(helperDataString);
3946

@@ -66,26 +73,14 @@ const getUpdates = async () => {
6673
body: JSON.stringify(feeds.result[i].channel_post.text),
6774
});
6875
}
69-
await updateId();
76+
return await updateId();
7077
};
7178

72-
let intervalId: NodeJS.Timeout;
73-
74-
const Listen = async (value: Boolean) => {
75-
if (value) {
76-
try {
77-
await fs.access(helperPath);
78-
intervalId = setInterval(getUpdates, 3000);
79-
// await getUpdates();
80-
} catch (error) {
81-
await updateId();
82-
}
83-
} else clearInterval(intervalId);
79+
const Listen = async () => {
80+
await getUpdates();
81+
setTimeout(Listen, 3000);
8482
};
85-
8683
export const POST = async (request: Request) => {
87-
const { value }: { value: Boolean } = await request.json();
88-
Listen(value);
89-
84+
await Listen();
9085
return new Response("Telegram Bot Running", { status: 200 });
9186
};

src/lib/telegramHelper.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"update_id": 19846249
2+
"update_id": 19846254
33
}

0 commit comments

Comments
 (0)