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)