Skip to content

Instantly share code, notes, and snippets.

@ninoseki
Created September 5, 2018 23:08
Show Gist options
  • Save ninoseki/a454e6b5570b977f4eb9571da5430363 to your computer and use it in GitHub Desktop.
Save ninoseki/a454e6b5570b977f4eb9571da5430363 to your computer and use it in GitHub Desktop.
Send a screenshot (by using puppeteer) to Slack
import request from "request";
import puppeteer from "puppeteer";
const API_URL = "https://slack.com/api/files.upload";
const SLACK_API_TOKEN = process.env.SLACK_API_TOKEN as string;
const CHANNEL = "general";
(async () => {
const url = "http://neverssl.com"
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const b64string: string = await page.screenshot({ encoding: "base64" }) as string;
const buffer: Buffer = Buffer.from(b64string, "base64");
await browser.close();
const data = {
url: API_URL,
formData: {
channels: CHANNEL,
file: {
value: buffer,
options: {
filename: "test.png",
}
},
filename: "test.png",
filetype: "image/png",
token: SLACK_API_TOKEN,
}
};
request.post(data, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
} else {
console.log(error);
}
});
})();
@blackcodefan
Copy link

great work. thank you so much.

@IgnacioUTN
Copy link

this is great! just thought about this 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment