|
1 | 1 | import path from "path";
|
2 |
| -import { promises as fs } from "fs"; |
| 2 | +import { promises as fs, readFile } from "fs"; |
3 | 3 |
|
4 | 4 | const channelDataPath = path.join(process.cwd(), "src/lib/formData.json");
|
5 | 5 | const helperPath = path.join(process.cwd(), "src/lib/telegramHelper.json");
|
@@ -34,6 +34,13 @@ const updateId = async () => {
|
34 | 34 |
|
35 | 35 | //Fetch For New Chats
|
36 | 36 | 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 | + |
37 | 44 | var helperDataString = await fs.readFile(helperPath, "utf-8");
|
38 | 45 | const helperData: HelperData = JSON.parse(helperDataString);
|
39 | 46 |
|
@@ -66,26 +73,14 @@ const getUpdates = async () => {
|
66 | 73 | body: JSON.stringify(feeds.result[i].channel_post.text),
|
67 | 74 | });
|
68 | 75 | }
|
69 |
| - await updateId(); |
| 76 | + return await updateId(); |
70 | 77 | };
|
71 | 78 |
|
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); |
84 | 82 | };
|
85 |
| - |
86 | 83 | export const POST = async (request: Request) => {
|
87 |
| - const { value }: { value: Boolean } = await request.json(); |
88 |
| - Listen(value); |
89 |
| - |
| 84 | + await Listen(); |
90 | 85 | return new Response("Telegram Bot Running", { status: 200 });
|
91 | 86 | };
|
0 commit comments