-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
35 lines (28 loc) · 913 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
require('dotenv').config()
const express = require('express')
const axios = require('axios')
const bodyParser = require('body-parser')
const { TOKEN, SERVER_URL } = process.env
const TELEGRAM_API = `https://api.telegram.org/bot${TOKEN}`
const URI = `/webhook/${TOKEN}`
const WEBHOOK_URL = SERVER_URL + URI
const app = express()
app.use(bodyParser.json())
const init = async () => {
const res = await axios.get(`${TELEGRAM_API}/setWebhook?url=${WEBHOOK_URL}`)
console.log(res.data)
}
app.post(URI, async (req, res) => {
console.log(req.body)
const chatId = req.body.message.chat.id
const text = req.body.message.text
await axios.post(`${TELEGRAM_API}/sendMessage`, {
chat_id: chatId,
text: text
})
return res.send()
})
app.listen(process.env.PORT || 5000, async () => {
console.log('🚀 app running on port', process.env.PORT || 5000)
await init()
})