-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.ts
More file actions
58 lines (47 loc) · 1.62 KB
/
dev.ts
File metadata and controls
58 lines (47 loc) · 1.62 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
48
49
50
51
52
53
54
55
56
57
58
// This script sets up a dev environment for a Telegram bot using Next.js and ngrok.
// It starts an ngrok tunnel, sets a Telegram webhook using the ngrok URL, and launches
// the Next.js development server. The main function orchestrates these steps, enabling local
// development and testing of the Telegram bot.
const TELEGRAM_TOKEN = process.env.TELEGRAM_TOKEN ?? ''
import { spawn } from 'child_process'
import ngrok from 'ngrok'
async function startNgrok(): Promise<string> {
const url = await ngrok.connect(3000)
console.log(`ngrok tunnel opened at ${url}`)
return url
}
async function setTelegramWebhook(url: string): Promise<void> {
const webhookUrl = `${url}/api/telegram/hook`
const setWebhookUrl = `https://api.telegram.org/bot${TELEGRAM_TOKEN}/setWebhook`
try {
const response = await fetch(setWebhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ url: webhookUrl })
})
const data = await response.json()
console.log('telegram webhook set')
} catch (error) {
console.error('Error setting telegram webhook:', error)
}
}
function startNextDev(): void {
const nextProcess = spawn('npx', ['next', 'dev'])
nextProcess.stdout?.on('data', (data) => {
console.log(data.toString())
})
nextProcess.stderr?.on('data', (data) => {
console.error(data.toString())
})
nextProcess.on('close', (code) => {
console.log(`Next.js process exited with code ${code}`)
})
}
async function main(): Promise<void> {
const ngrokUrl = await startNgrok()
await setTelegramWebhook(ngrokUrl)
startNextDev()
}
main()