-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6845bb
commit 93b5a67
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from flask import Flask, request, jsonify | ||
from slack_sdk import WebClient | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
load_dotenv() | ||
|
||
app = Flask(__name__) | ||
client = WebClient(token=os.getenv("SLACK_BOT_TOKEN")) | ||
|
||
@app.route('/slack/events', methods=['POST']) | ||
def slack_events(): | ||
data = request.json | ||
if 'event' in data: | ||
event = data['event'] | ||
if event.get('type') == 'message' and 'subtype' not in event: | ||
handle_message(event) | ||
return jsonify({'status': 'ok'}), 200 | ||
|
||
def handle_message(event): | ||
user_id = event.get('user') | ||
text = event.get('text') | ||
|
||
if text and user_id: | ||
update_user_input(user_id) | ||
|
||
def update_user_input(user_id): | ||
from datetime import datetime | ||
import sqlite3 | ||
|
||
conn = sqlite3.connect('database/bot_db.sqlite') | ||
cursor = conn.cursor() | ||
|
||
cursor.execute(''' | ||
INSERT OR REPLACE INTO users (id, name, last_input) | ||
VALUES (?, ?, ?) | ||
''', (user_id, "User", datetime.now().date())) | ||
|
||
conn.commit() | ||
conn.close() | ||
|
||
if __name__ == '__main__': | ||
app.run(port=3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import sqlite3 | ||
from datetime import datetime, timedelta | ||
from slack_sdk.errors import SlackApiError | ||
|
||
def deduct_points(): | ||
conn = sqlite3.connect('database/bot_db.sqlite') | ||
cursor = conn.cursor() | ||
|
||
today = datetime.now().date() | ||
yesterday = today - timedelta(days=1) | ||
|
||
cursor.execute(''' | ||
UPDATE users | ||
SET points = points - 10 | ||
WHERE last_input < ? | ||
''', (yesterday,)) | ||
|
||
conn.commit() | ||
|
||
cursor.execute(''' | ||
SELECT id, points FROM users | ||
WHERE last_input < ? | ||
''', (yesterday,)) | ||
|
||
for user_id, points in cursor.fetchall(): | ||
send_message(user_id, points) | ||
|
||
conn.close() | ||
|
||
def send_message(user_id, points): | ||
from slack_sdk import WebClient | ||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
client = WebClient(token=os.getenv("SLACK_BOT_TOKEN")) | ||
|
||
try: | ||
client.chat_postMessage( | ||
channel=user_id, | ||
text=f"Hey <@{user_id}>, you missed your input yesterday! You now have {points} points left." | ||
) | ||
except SlackApiError as e: | ||
print(f"Error sending message: {e.response['error']}") | ||
|
||
if __name__ == "__main__": | ||
deduct_points() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from bot import deduct_points | ||
|
||
if __name__ == "__main__": | ||
deduct_points() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Flask==2.0.1 | ||
slack_sdk==3.15.0 | ||
python-dotenv==0.20.0 |