Act

drobnikj/stackoverflow-search-to-slack

  • Builds
  • latest 0.0.14 / 2018-04-19
  • Created 2017-12-18
  • Last modified 2018-04-19
  • grade 1

Description

This act search on stackoverflow site for definied query and send them to your slack channel. Example of input: { "searchStrings": ["created:1d apify","created:1d crawler javascript"], "slackBotToken": "dhU6k07zhfFhBk0Kh" } Easy slack bot set up: - Go to https://api.slack.com/slack-apps and click on Create Slack app - Set up App name and slack channel as workspace - Go to your created app then go to Bot Users and set up name for your slack bot - Go to OAuth & Permissions and copy your OAuth Access Token and put in into input of this act (slackBotToken)


API

To run the act, send a HTTP POST request to:

https://api.apify.com/v2/acts/drobnikj~stackoverflow-search-to-slack/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the act. For more information, read the docs.


Example input

Content type: application/json; charset=utf-8

{
  "searchStrings": ["created:1d apify","created:1d crawler javascript"],
  "slackBotToken": "dhU6k07zhfFhBk0Kh"
}

Source code

Based on the apify/actor-node-puppeteer Docker image (see docs).

const Apify = require('apify');
const Slack = require('slack');

Apify.main(async () => {
    // Get input of your act
    const input = await Apify.getValue('INPUT');
    console.log('My input:');
    console.dir(input);
    
    const slackBotToken = input.slackBotToken || process.env.SLACK_BOT_TOKEN;
   
    if (!input) throw new Error('Invalid input, must be JSON object!');
    if (!slackBotToken) throw new Error('Invalid input, slackBotToken is missing in input or env var!');
    if (!input.searchStrings) throw new Error('Invalid input, searchStrings is missing!');
    
    // Crawl pages for data
    console.log('Launching Puppeteer...');
    const browser = await Apify.launchPuppeteer();
    const results = [];

    for (const search of input.searchStrings) {
        const url = `https://stackoverflow.com/search?q=${encodeURIComponent(search)}`;
        console.log(`Opening URL: ${url}`);
        const page = await browser.newPage();
        await page.goto(url);
        const questions = await page.$$eval('.search-result', (results) => {
            return results.map(el => `- <${el.querySelector('.result-link a').href}|${el.querySelector('.result-link a').innerText}>`).join('\n');
        });
        if (questions) results.push({ title: `search: ${search}`, text: questions })
        await new Promise(resolve => setTimeout(resolve, 5000))
    }
    
    // Send results to slack
    if (results.length) {
        const bot = new Slack({ token: slackBotToken });
        await bot.chat.postMessage({
            channel: 'C5RMPH401',
            response_type: 'in_channel',
            text: 'Interesting stackoverflow questions:',
            attachments: results,
        });
    }
    
    console.log('My output:');
    console.dir(results);
    await Apify.setValue('OUTPUT', results);
});