name: Slack RSS Subscription
on:
  workflow_dispatch:
    inputs:
      rss_feed_url:
        description: 'RSS Feed URL'
        required: true

jobs:
  send_url_to_slack:
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 14

      - name: Subscribe to RSS feed
        env:
          SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
          SLACK_CHANNEL: rladiesblogs
          RSS_FEED_URL: ${{ github.event.inputs.rss_feed_url }}
        run: |
          npm install @slack/web-api
          node -e '
          const { WebClient } = require("@slack/web-api");

          const slackToken = process.env.SLACK_TOKEN;
          const channel = process.env.SLACK_CHANNEL;
          const url = process.env.RSS_FEED_URL;

          (async () => {
            const web = new WebClient(slackToken);
            const response = await web.chat.postMessage({
              channel,
              text: `/feed subscribe ${url}`
            });

            console.log("Response:", response);
          })().catch(error => {
            console.error("Error:", error);
            process.exit(1);
          });
          '
