-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
37 lines (32 loc) · 1.33 KB
/
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
36
37
const { Composer, fork } = require('micro-bot')
const ratelimit = require('telegraf-ratelimit')
const algorithmia = require('algorithmia')
const client = algorithmia.client(process.env.ALGORITMIA_TOKEN)
async function colorize (image) {
const response = await client
.algo('deeplearning/ColorfulImageColorization/1.1.7')
.pipe({ image })
return new Promise((resolve, reject) =>
client.file(response.get().output).get(
(err, result) => err ? reject(err) : resolve(result)
)
)
}
const limiterConfig = {
window: 1000 * 60 * 5,
limit: 1,
onLimitExceeded: Composer.reply('😭 Rate limit exceeded: 1 photo per 5 minutes')
}
const bot = new Composer()
bot.start(({ reply }) => reply('Hey there!\nSend me B&W photo'))
bot.command(['help', 'about'], ({ replyWithMarkdown }) => replyWithMarkdown('[🤖 Bot source code](https://github.com/telegraf/colorizer-bot)'))
bot.on('photo', ratelimit(limiterConfig), fork(async (ctx) => {
const { telegram, message, reply, replyWithPhoto, replyWithChatAction } = ctx
await reply('🌈 Colorizing...')
const photo = message.photo[2] || message.photo[1]
const photoLink = await telegram.getFileLink(photo.file_id)
const coloredImage = await colorize(photoLink)
await replyWithChatAction('upload_photo')
await replyWithPhoto({ source: coloredImage })
}))
module.exports = bot