Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Cdaprod/3f8bcbd22b2cf996e6ba2ce7a0545120 to your computer and use it in GitHub Desktop.
Save Cdaprod/3f8bcbd22b2cf996e6ba2ce7a0545120 to your computer and use it in GitHub Desktop.

Batch Subscribing to Slack GitHub Intergration Over Webhook

Prerequisites

  • Slack Workspace: You need access to a Slack workspace where you can add apps.
  • Python Environment: Ensure you have Python installed on your machine. This guide assumes you are using Python 3.x.
  • iOS Device: An iPhone or iPad with the Shortcuts app and Working Copy app installed.

Step 1: Create a Slack App and Incoming Webhook

  1. Create a Slack App:
    • Go to the Slack API page and click "Create New App".
    • Choose "From scratch", name your app, select your workspace, and create the app.
  2. Enable Incoming Webhooks:
    • In your app’s settings, navigate to "Incoming Webhooks" and enable them.
  3. Create an Incoming Webhook:
    • Click "Add New Webhook to Workspace".
    • Choose the channel where messages will be posted and allow the app permissions.
    • Copy the Webhook URL provided.

Step 2: Obtain Repository Names with iOS Shortcuts

  1. Open the Shortcuts app on your iOS device.
  2. Create a new shortcut for listing repositories from Working Copy.
  3. Add the "List repositories" action from Working Copy.
  4. Add the "Combine Text" action to format the repository list for use as a payload.

Step 3: Write the Python Script

Create a Python script (send_to_slack.py) with the following content:

import requests
import json
import os

# Fetching the Slack webhook URL from an environment variable
webhook_url = os.environ['SLACK_WEBHOOK']

# Define your message payload
payload = """
Your message or command here.
""".strip()

# Sending the message via POST request to the Slack webhook URL
response = requests.post(webhook_url, data=json.dumps({"text": payload}), headers={'Content-Type': 'application/json'})

# Check the response
if response.status_code == 200:
    print("Message sent successfully")
else:
    print(f"Failed to send message. Status code: {response.status_code} - Response: {response.text}")

Step 4: Execute the Script

  • Run the script using your command line or terminal: python send_to_slack.py.
  • Check your Slack channel to see the message posted.

Tips and Best Practices

  • Security: Keep your webhook URL secure. Do not hard-code it in your scripts or share it in public repositories.
  • Rate Limits: Be mindful of Slack's rate limits for incoming webhooks to avoid disruptions.
  • Customizing Messages: Customize your Slack messages using the formatting options provided by Slack.

Remember to replace the placeholder text "Your message or command here." in the Python script with the actual payload of repository names you want to send to Slack. If you plan to automate this process further, you could use additional scripts or automation tools to directly connect the output of your iOS Shortcuts to the Python script, thereby streamlining the entire workflow from repository listing to Slack notification.

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