Skip to content

Commit

Permalink
Merge pull request #14 from UpMortem/dm_support
Browse files Browse the repository at this point in the history
Support for DMs
  • Loading branch information
joelbandi authored Feb 6, 2023
2 parents 959988d + 1527320 commit 95b6a2a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
49 changes: 2 additions & 47 deletions src/controllers/slack_controller.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,14 @@
from flask import jsonify
import time
from services.slack_service import (
send_message,
get_thread_messages,
get_thread_messages_with_usernames,
process_event_payload
)
from services.openai_service import respond_to_user
from threading import Thread

# POST /slack/events
def post_event(request):
if request.get("type") == "url_verification":
return request.get("challenge")

print(request)
Thread(target=process_event_payload, args=(request,)).start()
return jsonify({"message": "success"}), 200


def find_bot_id(payload):
for auth in payload["authorizations"]:
# Check if the current authorization is a bot
if auth["is_bot"]:
return auth["user_id"]

return None


def process_event_payload(payload):
event = payload.get("event")
channel = event.get("channel")
text = event.get("text")
thread_ts = event.get("thread_ts")
ts = event.get("ts")
bot_id = find_bot_id(payload)
if not bot_id:
print("botId not found")
return
try:
thread_to_reply = thread_ts
if thread_ts != ts:
thread_to_reply = ts
messages = f"USER: {text.replace(f'<@{bot_id}>','').strip()}"
if thread_ts:
messages = (
get_thread_messages_with_usernames(channel, thread_ts, bot_id)
or messages
)

start_time = time.perf_counter()
response = respond_to_user(messages)
end_time = time.perf_counter()
response = response + f"\n\n\n_response generated in {round(end_time - start_time, 2)}s_ :hourglass:"

return send_message(channel, thread_to_reply, response)
except Exception as error:
# Improve error handling
print(error)
return
2 changes: 0 additions & 2 deletions src/services/openai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ def run_completion(text):
)
return completion.choices[0].text


def respond_to_user(text):
prompt = f"{base_prompt}{text}\nHaly:"
response = run_completion(prompt)
return response


def get_conversation_summary(thread_messages):
filtered_thread_messages = "\n".join(
filter(lambda m: m["bot_id"] is None, thread_messages)
Expand Down
57 changes: 56 additions & 1 deletion src/services/slack_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from slack_bolt import App
import time
import os
from services.openai_service import respond_to_user

# grabs the credentials from .env directly
slack_app = App()
users_map = {}


def send_message(channel: str, thread_ts: str, text: str):
try:
slack_app.client.chat_postMessage(
Expand Down Expand Up @@ -54,3 +55,57 @@ def get_username(user_id: str):
user = find_user_by_id(user_id)
users_map[user_id] = user["user"]["name"]
return users_map[user_id]

def find_bot_id(payload):
for auth in payload["authorizations"]:
# Check if the current authorization is a bot
if auth["is_bot"]:
return auth["user_id"]

return None

def get_sender(payload):
return payload.get("event").get("user")


def process_event_payload(payload):
sender = get_sender(payload)
if sender is None:
print("sender not found")
return

bot_id = find_bot_id(payload)
if not bot_id:
print("botId not found")
return

if sender == bot_id:
return

event = payload.get("event")
channel = event.get("channel")
text = event.get("text")
thread_ts = event.get("thread_ts")
ts = event.get("ts")

try:
thread_to_reply = thread_ts
if thread_ts != ts:
thread_to_reply = ts
messages = f"USER: {text.replace(f'<@{bot_id}>','').strip()}"
if thread_ts:
messages = (
get_thread_messages_with_usernames(channel, thread_ts, bot_id)
or messages
)

start_time = time.perf_counter()
response = respond_to_user(messages)
end_time = time.perf_counter()
print(f"response generated in {round(end_time - start_time, 2)}s")

return send_message(channel, thread_to_reply, response)
except Exception as error:
# Improve error handling
print(error)
return

0 comments on commit 95b6a2a

Please sign in to comment.