Skip to content

Commit 6049f09

Browse files
committed
Switch to webhook
1 parent 0542f40 commit 6049f09

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

bot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ const track = async () => {
289289
await new Promise(resolve => setTimeout(resolve, 1000))
290290
} catch (e) {
291291
console.log(`🚀 ~ file: bot.js:260 ~ temp.map ~ e:`, e)
292-
bot.start()
292+
// bot.start()
293293
// wait for 5 sec
294294
await new Promise(resolve => setTimeout(resolve, 5000))
295295
}
@@ -318,7 +318,7 @@ bot.catch((err) => {
318318
const e = err.error;
319319
console.error("Error: ", e.description);
320320

321-
bot.start();
321+
// bot.start();
322322
});
323323

324324
setInterval(track, 3600000); //Track every hr.

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { manageProducts, manageUsers } from "./db.js"
33
import {API_KEY} from './config.js'
44
import express from 'express'
55
import bot from './bot.js'
6+
import { webhookCallback } from "grammy"
67

78
//Globals
89
const port = process.env.PORT || 3000;
@@ -81,5 +82,21 @@ app.get('/info', async(req, res) => {
8182
res.send(JSON.stringify({error: 'Invalid API key'}))
8283
})
8384

85+
// use bot webhook path
86+
app.use('/bot', webhookCallback(bot, 'express'));
87+
88+
// set bot webhook, use req.url as webhook path
89+
app.get('/setup', async (req, res) => {
90+
// get host name from req
91+
try {
92+
const host = req.hostname;
93+
bot.api.setWebhook(`https://${host}/bot`);
94+
res.send('ok');
95+
} catch (e) {
96+
console.log(e);
97+
res.send('error');
98+
};
99+
})
100+
84101
app.listen(port, async () => console.log('listening to port ' + port));
85-
bot.start().then(() => console.log('Bot launched!'));
102+
// bot.start().then(() => console.log('Bot launched!'));

0 commit comments

Comments
 (0)