Skip to content

Instantly share code, notes, and snippets.

@brumm
Created October 25, 2017 11:54
Show Gist options
  • Save brumm/c6d32f7b832453254308121c9d7146b0 to your computer and use it in GitHub Desktop.
Save brumm/c6d32f7b832453254308121c9d7146b0 to your computer and use it in GitHub Desktop.
const Telegraf = require('telegraf')
const webshot = require('webshot')
const fetch = require('node-fetch')
const app = new Telegraf('YOUR-KEY')
const markup = fragment => `
<!DOCTYPE html>
<html>
<head>
<link href="https://unpkg.com/github-syntax-light@0.5.0/lib/github-light.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Fira+Mono" rel="stylesheet">
<style>
* {
font-family: 'Fira Mono', monospace;
font-size: 30px;
}
.highlight {
display: inline-block;
padding: 10px;
line-height: 1.4em;
}
pre {
margin: 0;
}
</style>
</head>
<body>
${fragment}
</body>
</html>
`
const codeBlock = (code, language) => `
\`\`\`${language}
${code}
\`\`\`
`
app.command('code', ({ replyWithChatAction, replyWithPhoto, update: { message } }) => {
const [command] = message.entities
const argument = message.text.substr(command.length + 1)
fetch('https://api.github.com/markdown/raw', {
method: 'POST',
body: codeBlock(argument, 'javascript'),
headers: { 'Content-Type': 'text/plain' },
})
.then(res => res.text())
.then(fragment => {
replyWithChatAction('upload_photo')
replyWithPhoto({
source: webshot(markup(fragment), { siteType: 'html', captureSelector: '.highlight' }),
})
})
})
app.startPolling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment