Created
May 1, 2018 05:52
-
-
Save sh11mo/5a7e78f377d9a2e51af652af6c85031f to your computer and use it in GitHub Desktop.
slack-kabutan-bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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