ChatGPTとSlack上で会話するアプリを10分で構築しよう（前編：ローカル環境）

# 使用するOSS
https://github.com/seratch/ChatGPT-in-Slack
- 言語：Python

# ゴール
AWS LambdaでChatGPTと連続的な会話を実現するSlackアプリを構築する

# ローカルで動かす
まずはREADMEに従ってローカルで動かしてみましょう

## Slack API
### Slack APIで新しいAppを作成
- Slack ApiでCreate New Appをクリックします
  - https://api.slack.com/apps

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/b8bfb280-69d2-8d76-ba23-ede43e231cdd.png)

- From an app manifestを選択します
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/21ccaaa4-c077-64a6-0072-ee9939f9c2cb.png)

- Enter app manifest bellowを入力します
  - manifest-dev.ymlの内容をコピペします
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/8f62640a-0ac9-1f78-09fc-797d34cc5a4b.png)

### Appで必要な設定を行います
- Install to Workspaceします
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/9c4b0f7d-d016-1d16-8a66-182184876edb.png)

- App-Level Tokenをconnections:write scopeで作成します
  - Tokenを控えます
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/6f58f01a-1ffb-11e4-b04b-4b45a0732b89.png)

![FireShot Capture 013 - Slack API_ Applications - Sample Slack - api.slack.com.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/ec590f7c-55d3-d11e-3fb5-c0844bb7102a.png)

- Install AppからBot User OAuth Tokenを控えます
![FireShot Capture 014 - Slack API_ Applications - Sample Slack - api.slack.com.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/0f88553a-92ba-e288-a4a3-60a336570a71.png)

## OpenAI API
- OpenAI API keyを作成し、控えます
https://platform.openai.com/account/api-keys

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/09ba8898-40fe-139b-94c0-d763c6510d22.png)

## Appを立ち上げます
- READMEに記載の通り、実行します
  - 環境変数は前段階で控えたものに置き換えてください
  - OPENAI_MODELは`gpt-3.5-turbo`としてください

```sh
# Create an app-level token with connections:write scope
export SLACK_APP_TOKEN=xapp-1-...
# Install the app into your workspace to grab this token
export SLACK_BOT_TOKEN=xoxb-...
# Visit https://platform.openai.com/account/api-keys for this token
export OPENAI_API_KEY=sk-...

# Optional: gpt-3.5-turbo and gpt-4 are currently supported (default: gpt-3.5-turbo)
export OPENAI_MODEL=gpt-3.5-turbo
# Optional: You can adjust the timeout seconds for OpenAI calls (default: 30)
export OPENAI_TIMEOUT_SECONDS=60
# Optional: You can include priming instructions for ChatGPT to fine tune the bot purpose
export OPENAI_SYSTEM_TEXT="You proofread text. When you receive a message, you will check
for mistakes and make suggestion to improve the language of the given text"
# Optional: When the string is "true", this app translates ChatGPT prompts into a user's preferred language (default: true)
export USE_SLACK_LANGUAGE=true
# Optional: Adjust the app's logging level (default: DEBUG)
export SLACK_APP_LOG_LEVEL=INFO
# Optional: When the string is "true", translate between OpenAI markdown and Slack mrkdwn format (default: false)
export TRANSLATE_MARKDOWN=true

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python main.py
```

## Slackで会話してみましょう
- 普通にWebで会話するように会話してみましょう
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/643664/0af6b034-29db-681d-9b50-a94be6aa7341.png)

# 後編
次回はこのアプリをAWS Lambdaに構築します

https://qiita.com/rubita/items/f8d08552f98cc3b25a1a