Skip to content

Instantly share code, notes, and snippets.

@sh11mo
Created May 1, 2018 05:52
Show Gist options
  • Select an option

  • Save sh11mo/5a7e78f377d9a2e51af652af6c85031f to your computer and use it in GitHub Desktop.

Select an option

Save sh11mo/5a7e78f377d9a2e51af652af6c85031f to your computer and use it in GitHub Desktop.
slack-kabutan-bot
const Botkit = require('botkit');
const puppeteer = require('puppeteer');
const fs = require('fs');
const capture = async keyword => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://kabutan.jp/');
await page.type('#input_id', keyword);
await page.click('#kensaku_btn');
await page.waitForSelector('#main');
const main = await page.$('#main');
const elementBounds = await main.boundingBox();
elementBounds.y += 10;
elementBounds.height -= 32;
await page.screenshot({ path: 'tmp.png', clip: elementBounds });
await browser.close();
};
const controller = Botkit.slackbot({
debug: false,
});
controller.spawn({ token: TOKEN }).startRTM();
controller.hears(
['(.*)'],
['direct_message', 'direct_mention', 'mention'],
async (bot, message) => {
bot.reply(message, 'wait for capturing kabutan...');
await capture(message.text);
bot.api.files.upload(
{
file: fs.createReadStream('tmp.png'),
filename: 'stock-chart.png',
channels: message.channel,
},
(err, res) => {
if (err) console.log(err);
},
);
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment