Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slack関連の処理をslack_main関数に集約する #3543

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 92 additions & 91 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@

app = Flask(__name__)

slack_app = slack_bolt.App(
token=conf.SLACK_API_TOKEN, signing_secret=conf.SLACK_SIGNING_SECRET
)
slack_handler = SlackRequestHandler(slack_app)


def __init__():
log_format_config = {
Expand All @@ -51,93 +46,99 @@ def __init__():
)


@slack_app.event("app_mention")
def on_app_mention(body):
channel = body["event"]["channel"]
blocks = body["event"]["blocks"]
authed_users = body["authed_users"]
client_msg_id = body["event"]["client_msg_id"]

print(f"body: {body}")
print(f"channel: {channel}")
print(f"blocks: {blocks}")
print(f"authed_users: {authed_users}")
print(f"client_msg_id: {client_msg_id}")

with Database() as _db, _db.conn.cursor() as cursor:
cursor.execute(
"SELECT client_msg_id FROM slack_client_msg_id WHERE client_msg_id = %s LIMIT 1",
(client_msg_id,),
)

if cursor.fetchone():
print("skip")
return

cursor.execute(
"DELETE FROM slack_client_msg_id "
"WHERE created_at < CURRENT_TIMESTAMP - interval '10 minutes'",
(client_msg_id,),
)
cursor.execute(
"INSERT INTO slack_client_msg_id(client_msg_id, created_at) "
"VALUES(%s, CURRENT_TIMESTAMP)",
(client_msg_id,),
)
_db.conn.commit()

with ThreadPoolExecutor(max_workers=3) as tpe:
for block in blocks:
if block["type"] == "rich_text":
block_elements = block["elements"]
for block_element in block_elements:
if block_element["type"] == "rich_text_section":
block_element_elements = block_element["elements"]
if (
len(block_element_elements) > 0
and block_element_elements[0]["type"] == "user"
and block_element_elements[0]["user_id"] in authed_users
):
tpe.submit(
analyze.analyze_slack_message(
block_element_elements[1:]
),
SlackClient(
slack_app.client,
channel,
block_element_elements[0]["user_id"],
),
)


@app.route("/slack/events", methods=["POST"])
def slack_events():
"""
appにメンションが送られたらここが呼ばれる
"""
return slack_handler.handle(request)


@app.route("/", methods=["GET", "POST"])
def http_app():
"""
localでテストできます

<コマンド例>
curl -XPOST -d '{"message": "鳩", "channel": "C0123A4B5C6", "user": "U012A34BCDE"}' \
-H "Content-Type: application/json" http://localhost:3000/
def slack_main():
slack_app = slack_bolt.App(
token=conf.SLACK_API_TOKEN, signing_secret=conf.SLACK_SIGNING_SECRET
)
slack_handler = SlackRequestHandler(slack_app)

@slack_app.event("app_mention")
def on_app_mention(body):
channel = body["event"]["channel"]
blocks = body["event"]["blocks"]
authed_users = body["authed_users"]
client_msg_id = body["event"]["client_msg_id"]

print(f"body: {body}")
print(f"channel: {channel}")
print(f"blocks: {blocks}")
print(f"authed_users: {authed_users}")
print(f"client_msg_id: {client_msg_id}")

with Database() as _db, _db.conn.cursor() as cursor:
cursor.execute(
"SELECT client_msg_id FROM slack_client_msg_id WHERE client_msg_id = %s LIMIT 1",
(client_msg_id,),
)

or
if cursor.fetchone():
print("skip")
return

pipenv run python post_command.py --channel C0123A4B5C6 --user U012A34BCDE "鳩"
"""
msg = request.json["message"]
channel = request.json["channel"]
user = request.json["user"]
client = SlackClient(slack_app.client, channel, user)
client.post(f"コマンド: {msg}")
analyze.analyze_message(msg)(client)
return "success"
cursor.execute(
"DELETE FROM slack_client_msg_id "
"WHERE created_at < CURRENT_TIMESTAMP - interval '10 minutes'",
(client_msg_id,),
)
cursor.execute(
"INSERT INTO slack_client_msg_id(client_msg_id, created_at) "
"VALUES(%s, CURRENT_TIMESTAMP)",
(client_msg_id,),
)
_db.conn.commit()

with ThreadPoolExecutor(max_workers=3) as tpe:
for block in blocks:
if block["type"] == "rich_text":
block_elements = block["elements"]
for block_element in block_elements:
if block_element["type"] == "rich_text_section":
block_element_elements = block_element["elements"]
if (
len(block_element_elements) > 0
and block_element_elements[0]["type"] == "user"
and block_element_elements[0]["user_id"] in authed_users
):
tpe.submit(
analyze.analyze_slack_message(
block_element_elements[1:]
),
SlackClient(
slack_app.client,
channel,
block_element_elements[0]["user_id"],
),
)

@app.route("/slack/events", methods=["POST"])
def slack_events():
"""
appにメンションが送られたらここが呼ばれる
"""
return slack_handler.handle(request)

@app.route("/", methods=["GET", "POST"])
def http_app():
"""
localでテストできます

<コマンド例>
curl -XPOST -d '{"message": "鳩", "channel": "C0123A4B5C6", "user": "U012A34BCDE"}' \
-H "Content-Type: application/json" http://localhost:3000/

or

pipenv run python post_command.py --channel C0123A4B5C6 --user U012A34BCDE "鳩"
"""
msg = request.json["message"]
channel = request.json["channel"]
user = request.json["user"]
client = SlackClient(slack_app.client, channel, user)
client.post(f"コマンド: {msg}")
analyze.analyze_message(msg)(client)
return "success"

app.run(host="0.0.0.0", port=conf.PORT)


@app.route("/healthcheck", methods=["GET", "POST"])
Expand Down Expand Up @@ -261,7 +262,7 @@ async def misskey_runner():
else:
raise e
else:
app.run(host="0.0.0.0", port=conf.PORT)
slack_main()


if __name__ == "__main__":
Expand Down
Loading