From 5da1b0e8a4fed4704fefdf4582bb3ed52db363ed Mon Sep 17 00:00:00 2001 From: Flemming Petersen Date: Mon, 4 Mar 2024 19:28:59 +0100 Subject: [PATCH] Build poll bot --- .devcontainer/devcontainer.json | 22 +++++++++++++++++++ .dockerignore | 5 +++++ .env.example | 3 +++ .github/dependabot.yml | 12 ++++++++++ .github/workflows/deploy.yml | 38 ++++++++++++++++++++++++++++++++ .gitignore | 2 ++ Dockerfile | 17 ++++++++++++++ docker-compose.yml | 7 ++++++ index.py | 39 +++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 10 files changed, 148 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .dockerignore create mode 100644 .env.example create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/deploy.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 index.py create mode 100644 requirements.txt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..8c6f3fb --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,22 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/python +{ + "name": "Python 3", + // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + "image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye" + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "pip3 install --user -r requirements.txt", + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c150d98 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.env.example +.devcontainer +.github +docker-compose.yml +.gitignore diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..8b5a7d3 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +SLACK_TOKEN = 'YOUR_SLACK_TOKEN' +CHANNEL_ID = 'YOUR_CHANNEL_ID' +MESSAGE = 'Hello, World!' diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..f33a02c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for more information: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates +# https://containers.dev/guide/dependabot + +version: 2 +updates: + - package-ecosystem: "devcontainers" + directory: "/" + schedule: + interval: weekly diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..0efbb69 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,38 @@ +name: Build and deploy website + +on: + push: + branches: ["main"] + +concurrency: + group: ${{ github.ref }} + cancel-in-progress: true + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Docker Setup Buildx + uses: docker/setup-buildx-action@v2.2.1 + + - name: Log in to the Container registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build + uses: docker/build-push-action@v3 + with: + context: . + file: Dockerfile + push: true + tags: "ghcr.io/${{ github.repository }}:latest" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afb6ade --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +DS_Store diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4c16edc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# Use the official Python base image +FROM python:3.12-alpine3.19 + +# Set the working directory in the container +WORKDIR /app + +# Copy the requirements.txt file to the container +COPY requirements.txt . + +# Install the required packages +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application code to the container +COPY . . + +# Run the index.py file +CMD ["python", "index.py"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2a24354 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +version: '3.1' + +services: + app: + build: . + env_file: + - .env diff --git a/index.py b/index.py new file mode 100644 index 0000000..3619390 --- /dev/null +++ b/index.py @@ -0,0 +1,39 @@ +import os +import schedule +import time +from datetime import datetime +from slack_sdk import WebClient +from slack_sdk.errors import SlackApiError +from dotenv import load_dotenv + +load_dotenv() + +SLACK_TOKEN = os.getenv('SLACK_TOKEN') +CHANNEL_ID = os.getenv('CHANNEL_ID') +MESSAGE = os.getenv('MESSAGE') + +def send_poll_message(): + client = WebClient(token=SLACK_TOKEN) + + try: + response = client.chat_postMessage( + channel=CHANNEL_ID, + text=MESSAGE + ) + print("Message sent successfully:", response['ts']) + # Add a reaction to the message + client.reactions_add( + channel=CHANNEL_ID, + name='white_check_mark', + timestamp=response['ts'] + ) + except SlackApiError as e: + print(f"Error sending message: {e.response['error']}") + +print("Poll bot is running...") + +schedule.every().thursday.at("14:00").do(send_poll_message) + +while True: + schedule.run_pending() + time.sleep(60) # Check every minute if it's time to send the message diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3164811 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +slack_sdk>=3.27 +python-dotenv>=1.0 +schedule>=1.2