Skip to content

Commit

Permalink
[WIP] Add search summary function
Browse files Browse the repository at this point in the history
  • Loading branch information
Michihiro Nakamura committed Feb 5, 2023
1 parent 173cdf7 commit e85a38a
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ After the first deployment, properly configure the settings below. Follow [the o
- `app_mention`
- `message.channels`
- `file_shared`
- OAuth scops
- OAuth scopes
- `chat:write`
- `files:write`
- `files:read`
- `search:read`
2 changes: 2 additions & 0 deletions app/.chalice/config.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"automatic_layer": true,
"environment_variables": {
"SLACK_BOT_TOKEN": "{{ SLACK_BOT_TOKEN_DEV }}",
"SLACK_USER_TOKEN": "{{ SLACK_USER_TOKEN_DEV }}",
"SLACK_SIGNING_SECRET": "{{ SLACK_SIGNING_SECRET_DEV }}",
"OPENAI_ORGANIZATION": "{{ OPENAI_ORGANIZATION }}",
"OPENAI_API_KEY": "{{ OPENAI_API_KEY }}",
Expand All @@ -20,6 +21,7 @@
"automatic_layer": true,
"environment_variables": {
"SLACK_BOT_TOKEN": "{{ SLACK_BOT_TOKEN }}",
"SLACK_USER_TOKEN": "{{ SLACK_USER_TOKEN }}",
"SLACK_SIGNING_SECRET": "{{ SLACK_SIGNING_SECRET }}",
"OPENAI_ORGANIZATION": "{{ OPENAI_ORGANIZATION }}",
"OPENAI_API_KEY": "{{ OPENAI_API_KEY }}",
Expand Down
9 changes: 8 additions & 1 deletion app/chalicelib/service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Services are responsible for generating the reply message and additional content (e.g. images).
# They must be easily testable and shoud not depend on Slack SDK.

import json
import tempfile
from typing import Optional
from typing import Optional, Sequence

import langchain
from langchain import LLMChain, PromptTemplate
Expand Down Expand Up @@ -162,3 +163,9 @@ def generate_image_variation(image_file: str) -> tuple[str, Optional[list[str]]]
f.write(image)
image_files.append(image_file)
return reply, image_files


def summarize_slack_messages(messages: Sequence[dict]) -> str:
# TODO: implement it
reply = "```\n" + json.dumps(messages, indent=2, ensure_ascii=False) + "\n```"
return reply
23 changes: 20 additions & 3 deletions app/chalicelib/slack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@
command_help,
command_image,
command_image_variation,
command_slacksearch,
command_text,
command_textedit,
command_textinsert,
parse,
)
from .matcher import match_file_share, match_message_replied

SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN")
SLACK_USER_TOKEN = os.environ.get("SLACK_USER_TOKEN")
SLACK_SIGNING_SECRET = os.environ.get("SLACK_SIGNING_SECRET")

bolt_app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
token=SLACK_BOT_TOKEN,
signing_secret=SLACK_SIGNING_SECRET,
process_before_response=True,
)


@bolt_app.event("app_mention")
def reply_mention(event, say):
def reply_mention(event, context, say):
ts = event["ts"]
channel = event["channel"]
user = event["user"]
Expand Down Expand Up @@ -78,6 +83,18 @@ def reply_mention(event, say):
)
return

if command == "slacksearch":
command_slacksearch(
query=args[0],
count=10,
client=bolt_app.client,
user=user,
channel=channel,
say=say,
user_token=SLACK_USER_TOKEN,
)
return

# must be unreachable
reply = "私、バグっているみたいです!"
say(f"<@{user}> {reply}")
Expand Down
35 changes: 34 additions & 1 deletion app/chalicelib/slack/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
generate_text_completion,
generate_text_edit,
generate_text_insertion,
summarize_slack_messages,
)
from ..util import download_file

Expand All @@ -34,6 +35,7 @@
"codeedit",
"codeinsert",
"image",
"slacksearch",
)

Command = Literal[
Expand All @@ -46,6 +48,7 @@
"codeedit",
"codeinsert",
"image",
"slacksearch",
]
Args = Sequence[str]

Expand Down Expand Up @@ -108,7 +111,8 @@ def command_help(say: Say):
"codeedit: コードを編集します\n"
"codeinsert: コードを挿入します\n"
"image: 文章から画像を生成します\n"
"画像アップロード: 画像のバリエーションを生成します"
"画像アップロード: 画像のバリエーションを生成します\n"
"slacksearch: Slackの検索結果を要約します\n"
"```"
)

Expand Down Expand Up @@ -205,3 +209,32 @@ def command_image_variation(
os.remove(file)
else:
say(f"<@{user}> {reply}")


def command_slacksearch(
query: str,
count: int,
client: WebClient,
user: str,
channel: str,
say: Say,
user_token: str,
):
resp = client.search_messages(
query=query,
count=count,
token=user_token,
)
messages = [
{
"channel": m["channel"]["id"],
"channel_name": m["channel"]["name"],
"user": m["user"],
"text": m["text"],
"url": m["permalink"],
}
for m in resp["messages"]["matches"]
if m["channel"]["is_private"] is False
]
reply = summarize_slack_messages(messages)
say(f"<@{user}> {reply}")
2 changes: 2 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ stage="${1:-dev}" # dev or prod
poetry export -f requirements.txt --output requirements.txt --only main
jinja2 .chalice/config.template.json \
-D SLACK_BOT_TOKEN="${SLACK_BOT_TOKEN}" \
-D SLACK_USER_TOKEN="${SLACK_USER_TOKEN}" \
-D SLACK_SIGNING_SECRET="${SLACK_SIGNING_SECRET}" \
-D SLACK_BOT_TOKEN_DEV="${SLACK_BOT_TOKEN_DEV}" \
-D SLACK_USER_TOKEN_DEV="${SLACK_USER_TOKEN_DEV}" \
-D SLACK_SIGNING_SECRET_DEV="${SLACK_SIGNING_SECRET_DEV}" \
-D OPENAI_ORGANIZATION="${OPENAI_ORGANIZATION}" \
-D OPENAI_API_KEY="${OPENAI_API_KEY}" \
Expand Down
Loading

0 comments on commit e85a38a

Please sign in to comment.