-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (31 loc) · 998 Bytes
/
index.js
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
require('dotenv').config();
const { Telegraf } = require('telegraf')
const token = process.env.BOT_TOKEN
const myPort = process.env.PORT || 3000
const hookUrl = process.env.HOOK_URL
const environment = process.env.NODE_ENV
const botApp = require("./bot-app")
const botLaunch = require("./bot-launch")
if (token === undefined) {
throw new Error('BOT_TOKEN must be provided!')
}
const bot = new Telegraf(token)
const app = (botUrl, botPort) => {
botApp(bot);
botLaunch(bot, botUrl, botPort);
console.log(`Environment: ${environment}\nWebhook:${botUrl}\nPort:${botPort}`)
}
if (environment === "production") {
app(hookUrl, myPort)
} else if (environment === "development") {
//run app setting a tunnel to localhost as the webhook
const ngrok = require('ngrok');
ngrok.connect(myPort)
.then(localUrl => {
app(localUrl, myPort)
})
.catch(e => {
console.log("Ngrok error")
console.log(e)
})
}