-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresetWebhook.js
More file actions
47 lines (42 loc) · 1.79 KB
/
resetWebhook.js
File metadata and controls
47 lines (42 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Використовуйте динамічний імпорт для node-fetch
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
require('dotenv').config(); // Завантаження змінних середовища з файлу .env
const TELEGRAM_API_URL = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}`;
const SERVER_URL = process.env.SERVER_IP; // Додайте ваш NGROK URL до .env файлу
// Функція для видалення вебхука
async function deleteWebhook() {
const response = await fetch(`${TELEGRAM_API_URL}/deleteWebhook`, {
method: 'POST'
});
const data = await response.json();
console.log('Delete Webhook:', data);
return data;
}
// Функція для отримання оновлень (очищення черги повідомлень)
async function getUpdates() {
const response = await fetch(`${TELEGRAM_API_URL}/getUpdates`, {
method: 'GET'
});
const data = await response.json();
console.log('Get Updates:', data);
return data;
}
// Функція для налаштування нового вебхука
async function setWebhook() {
const response = await fetch(`${TELEGRAM_API_URL}/setWebhook`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: `${SERVER_URL}/telegram/webhook` })
});
const data = await response.json();
console.log('Set Webhook:', data);
return data;
}
// Головна функція для виконання всіх кроків
async function resetWebhook() {
await deleteWebhook();
await getUpdates();
await setWebhook();
}
// Виконання головної функції
resetWebhook().catch(console.error);