Skip to content

Instantly share code, notes, and snippets.

@hypernova7
Last active September 19, 2023 23:34
Show Gist options
  • Save hypernova7/2354d52c91ac04c3451965466baf424f to your computer and use it in GitHub Desktop.
Save hypernova7/2354d52c91ac04c3451965466baf424f to your computer and use it in GitHub Desktop.
telegraf + webhook + ngrok + express
import bot from './setup-bot'
bot.hears('Hi', ctx => {
ctx.reply('Hi there!')
})
export default bot
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);
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