forked from telegraf/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkoa-webhook-bot.js
More file actions
22 lines (19 loc) · 815 Bytes
/
koa-webhook-bot.js
File metadata and controls
22 lines (19 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const Telegraf = require('telegraf')
const Koa = require('koa')
const koaBody = require('koa-body')
const bot = new Telegraf(process.env.BOT_TOKEN)
// First reply will be served via webhook response,
// but messages order not guaranteed due to `koa` pipeline design.
// Details: https://github.com/telegraf/telegraf/issues/294
bot.command('image', (ctx) => ctx.replyWithPhoto({ url: 'https://picsum.photos/200/300/?random' }))
bot.on('text', ({ reply }) => reply('Hello'))
// Set telegram webhook
// npm install -g localtunnel && lt --port 3000
bot.telegram.setWebhook('https://-----.localtunnel.me/secret-path')
const app = new Koa()
app.use(koaBody())
app.use((ctx, next) => ctx.method === 'POST' || ctx.url === '/secret-path'
? bot.handleUpdate(ctx.request.body, ctx.response)
: next()
)
app.listen(3000)