Skip to content

Instantly share code, notes, and snippets.

@nrtkbb
Last active May 14, 2023 11:22
Show Gist options
  • Save nrtkbb/e408a05969934e2291784fc87ba774a3 to your computer and use it in GitHub Desktop.
Save nrtkbb/e408a05969934e2291784fc87ba774a3 to your computer and use it in GitHub Desktop.
pomodoro_slack_app.py
from flask import Flask
from flask import request
from flask import jsonify
from celery import Celery
app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
SLACK_BOT_TOKEN = 'token'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
@celery.task
def post_message_after_delay(channel, text, delay):
import time
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
time.sleep(delay)
client = WebClient(token=SLACK_BOT_TOKEN)
try:
response = client.chat_postMessage(channel=channel, text=text)
if response.status_code != 200:
print(str(response))
except SlackApiError as e:
print(f"Error: {e.response['error']}")
@app.route('/pomo', methods=['POST'])
def post_message():
# Replace with actual token and channel
channel = request.form['channel_id']
user_id = request.form['user_id']
# Queue tasks with 25 and 30 minute delays
post_message_after_delay.delay(
channel,
f'<@{user_id}>のポモドーロ終了',
25 * 60
)
post_message_after_delay.delay(
channel,
f'<@{user_id}>の休憩終了',
30 * 60
)
return jsonify(response_type='in_channel',
text=f'<@{user_id}>のポモドーロタイマーが開始されました')
if __name__ == '__main__':
app.run(debug=True, port=8080)
celery -A app.celery worker --loglevel=info
python3 ./app.py
ngrok http 8080
amqp==5.1.1
asgiref==3.6.0
billiard==3.6.4.0
blinker==1.6.2
celery==5.2.7
click==8.1.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
Flask==2.3.2
itsdangerous==2.1.2
Jinja2==3.1.2
kombu==5.2.4
MarkupSafe==2.1.2
prompt-toolkit==3.0.38
pytz==2023.3
redis==4.5.5
six==1.16.0
slack-sdk==3.21.3
uWSGI==2.0.21
vine==5.0.0
wcwidth==0.2.6
Werkzeug==2.3.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment