diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e943ff7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# JetBrains IDE +.idea/ +*.iml diff --git a/Dockerfile b/Dockerfile index 077645b..7fef936 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,10 +3,11 @@ FROM python WORKDIR /app RUN curl https://sh.rustup.rs -sSf | bash -s -- -y ENV PATH="/root/.cargo/bin:${PATH}" -COPY app.py /app COPY requirements.txt /app RUN pip install -r requirements.txt +COPY app.py /app + ENTRYPOINT ["python"] CMD ["app.py"] diff --git a/README.md b/README.md index e4fe2ac..49b6298 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ The `revChatGPT` package is used to handle authentication and communication with ## Usage -To use the bot, you will need to install the dependencies and set the `CHATGPT_EMAIL` and `CHATGPT_PASSWORD` environment variables with your OpenAI credentials. You also need to set `SLACK_SIGNING_SECRET` and `SLACK_BOT_TOKEN` environment variables after creating the Slack App. You can then run the `app.py` script to start the bot. +To use the bot, you will need to install the dependencies and set the `OPENAI_API_KEY` environment variable that you can get from https://platform.openai.com/account/api-keys. You also need to set `SLACK_SIGNING_SECRET` and `SLACK_BOT_TOKEN` environment variables after creating the Slack App. You can then run the `app.py` script to start the bot. -```python +```bash pip install -r requirements.txt export SLACK_SIGNING_SECRET=slack_signing_secret export SLACK_BOT_TOKEN=slack_bot_token @@ -30,7 +30,7 @@ Once the bot is running, you can mention it in a Slack channel to send it a mess ## ChatGPT Configuration -The `ChatGPTConfig` dictionary at the top of the `app.py` script contains the configuration for the `revChatGPT` package. It specifies the email and password for the OpenAI account that the bot will use to authenticate with the GPT-3 API. These values are read from the `CHATGPT_EMAIL` and `CHATGPT_PASSWORD` environment variables. +The `ChatGPTConfig` dictionary at the top of the `app.py` script contains the configuration for the `revChatGPT` package. It specifies the API token for the OpenAI account that the bot will use to authenticate with the GPT-3 API. The value is read from the `OPENAI_API_KEY` environment variable. The `conversation_id` parameter in the `Chatbot` constructor is used to specify the ID of the conversation that the bot should use. If this parameter is not provided, the `revChatGPT` package will generate a new conversation ID for each message that the bot receives. @@ -45,13 +45,15 @@ For this bot, the required Scopes in `OAuth & Permissions` are: * chat:write * im:write +For direct message required & config : +* `Enable bot direct messages` go to : Features -> App Home -> (Enable) Messages Tab +* `OAuth & Permissions` im:history and message:im + In the `Event Subscriptions`, you need to subscribe to the `app_mention` event. ## Limitations -The bot uses a pre-trained GPT-3 model, which means that its responses are limited to the information that is contained in the model. It may not be able to respond accurately to messages that are outside of the scope of the pre-trained model. - -Additionally, the bot is only designed to handle `app_mention` events, so it will not respond to other types of messages. This can be easily extended by adding more event handlers to the `app.py` script. +The bot uses a pre-trained GPT-3 model, which means that its responses are limited to the information that is contained in the model. It may not be able to respond accurately to messages that are outside the scope of the pre-trained model. ## Notes diff --git a/app.py b/app.py index 1cf6fd7..2f91715 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ import os import re +import sys import time from threading import Thread @@ -7,41 +8,61 @@ from slack_bolt import App ChatGPTConfig = { - "api_key": os.getenv("OPENAI_API_KEY"), - } + "api_key": os.getenv("OPENAI_API_KEY"), +} + if os.getenv("OPENAI_ENGINE"): ChatGPTConfig["engine"] = os.getenv("OPENAI_ENGINE") app = App() chatbot = Chatbot(**ChatGPTConfig) -# Listen for an event from the Events API -@app.event("app_mention") -def event_test(event, say): - prompt = re.sub('(?:\s)<@[^, ]*|(?:^)<@[^, ]*', '', event['text']) + +def handle_event(event, say, is_mention): + prompt = re.sub("\\s<@[^, ]*|^<@[^, ]*", "", event["text"]) + + # Each thread should be a separate conversation + convo_id = event.get("thread_ts") or event.get("ts") or "" + try: - response = chatbot.ask(prompt) - user = event['user'] - user = f"<@{user}> you asked:" - asked = ['>',prompt] - asked = "".join(asked) - send = [user,asked,response] - send = "\n".join(send) + response = chatbot.ask(prompt, convo_id=convo_id) + user = event["user"] + + if is_mention: + send = f"<@{user}> {response}" + else: + send = response except Exception as e: - print(e) - send = "We're experiencing exceptionally high demand. Please, try again." + print(e, file=sys.stderr) + send = "We are experiencing exceptionally high demand. Please, try again." - # Get the `ts` value of the original message - original_message_ts = event["ts"] + if is_mention: + # Get the `ts` value of the original message + original_message_ts = event["ts"] + else: + original_message_ts = None - # Use the `app.event` method to send a reply to the message thread + # Use the `app.event` method to send a message say(send, thread_ts=original_message_ts) + +@app.event("app_mention") +def handle_mention(event, say): + handle_event(event, say, is_mention=True) + + +@app.event("message") +def handle_message(event, say): + handle_event(event, say, is_mention=False) + + def chatgpt_refresh(): while True: time.sleep(60) + if __name__ == "__main__": + print("Bot Started!", file=sys.stderr) thread = Thread(target=chatgpt_refresh) thread.start() app.start(4000) # POST http://localhost:4000/slack/events diff --git a/docker-compose.yml b/docker-compose.yml index 3e44b54..1c5acd3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,11 +2,11 @@ version: "3.9" services: chatgptslackbot: build: - context: ./ + context: . image: chatgptslackbot restart: always container_name: chatgptslackbot env_file: - ./variables.env ports: - - 4000:4000 + - "4000:4000" diff --git a/requirements.txt b/requirements.txt index e02db2f..4bc810c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ -slack_bolt -revChatGPT>=3.0.0 +slack_bolt==1.18.0 +revChatGPT==5.0.0 +flask==2.3.2 diff --git a/variables.env b/variables.env index 9b6e442..5d2f2e1 100644 --- a/variables.env +++ b/variables.env @@ -1,4 +1,3 @@ SLACK_SIGNING_SECRET= SLACK_BOT_TOKEN= -CHATGPT_EMAIL= -CHATGPT_PASSWORD= +OPENAI_API_KEY=