Last active
September 19, 2023 23:34
-
-
Save hypernova7/2354d52c91ac04c3451965466baf424f to your computer and use it in GitHub Desktop.
telegraf + webhook + ngrok + express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bot from './setup-bot' | |
bot.hears('Hi', ctx => { | |
ctx.reply('Hi there!') | |
}) | |
export default bot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ngrok from 'ngrok'; | |
import express from 'express'; | |
import bot from './bot' | |
const app = express(); | |
const port = parseInt(process.env.PORT) || 8080; | |
// Without top-level await | |
const launch = async () => { | |
try { | |
let url = 'https://yourdomain.com/'; | |
// Use temporary url on development mode | |
if (process.env.NODE_ENV !== 'production') { | |
url = await ngrok.connect({ | |
authToken: '...', // Your ngrok auth token | |
addr: port | |
}); | |
console.log(`Temporary url: ${url}`); | |
} | |
app.use(await bot.createWebhook({ | |
domain: url | |
})); | |
} catch (e) { | |
console.error(e) | |
} | |
}; | |
// Send POST request to: https://temporary-url.ngrok.io/my-post-route | |
app.post('/my-post-route', async (req, res) => { | |
res.send('Hi there!') | |
}); | |
launch(); | |
app.listen(port); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Telegraf, session } from 'telegraf'; | |
const bot = new Telegraf(process.env.BOT_TOKEN) | |
bot.use(session({ defaultSession: () => ({}) }) | |
export default bot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment