Skip to content

Commit

Permalink
Build poll bot
Browse files Browse the repository at this point in the history
  • Loading branch information
flemming-pr committed Mar 4, 2024
0 parents commit 5da1b0e
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env.example
.devcontainer
.github
docker-compose.yml
.gitignore
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SLACK_TOKEN = 'YOUR_SLACK_TOKEN'
CHANNEL_ID = 'YOUR_CHANNEL_ID'
MESSAGE = 'Hello, World!'
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -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/#-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"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
DS_Store
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3.1'

services:
app:
build: .
env_file:
- .env
39 changes: 39 additions & 0 deletions index.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
slack_sdk>=3.27
python-dotenv>=1.0
schedule>=1.2

0 comments on commit 5da1b0e

Please # to comment.