Skip to content

Instantly share code, notes, and snippets.

@teidesu
Created June 22, 2021 13:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teidesu/0b77f714fb5468a4d1d675c951970a1b to your computer and use it in GitHub Desktop.
Save teidesu/0b77f714fb5468a4d1d675c951970a1b to your computer and use it in GitHub Desktop.
Really small and simple script for downloading sticker sets from telegram
const axios = require('axios')
const fs = require('fs')
const path = require('path')
const token = 'YOUR_BOT_TOKEN'
// vvv UNCOMMENT TO USE PROXY vvv
// const SocksProxyAgent = require('socks-proxy-agent')
// const agent = new SocksProxyAgent('socks5://127.0.0.1:9150')
// axios.defaults.httpAgent = agent
// axios.defaults.httpsAgent = agent
const sid = process.argv[2]
let out = process.argv[3]
if (!sid) {
console.log('usage: ' + process.argv[1] + ' <pack name> [output dir]')
process.exit(0)
}
const endpoint = `https://api.telegram.org/bot${token}`
const fileendpoint = `https://api.telegram.org/file/bot${token}/`
const downloadFile = async (fileid, filename) => {
const { data } = await axios.get(endpoint + '/getFile?file_id=' + fileid)
const { data: stream } = await axios.get(fileendpoint + data.result.file_path, {
responseType: 'stream'
})
const outs = fs.createWriteStream(path.join(out, filename))
const proc = stream.pipe(outs)
return new Promise((res, rej) => {
proc.on('error', e => rej(e.stack || e))
proc.on('finish', res)
})
}
const main = async () => {
console.log('[i] Retrieving sticker set')
const { data } = await axios.get(endpoint + `/getStickerSet?name=` + sid)
if (!data.ok) {
console.error(data.error)
return
}
if (!out) {
out = `${data.result.title} (${data.result.name})`
}
try {
fs.mkdirSync(out)
} catch (e) {
if (e.code !== 'EEXIST') {
console.error('Failed to create output directory:\n' + e.stack || e)
}
}
console.log('[i] Downloading ' + data.result.stickers.length + ' stickers..')
const ext = data.result.is_animated ? 'json' : 'webp'
const proms = data.result.stickers.map((ii, i) => downloadFile(ii.file_id, `${i}.${ext}`))
await Promise.all(proms)
console.log('[v] Finished!')
}
main().catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment