6
6
from threading import Thread , Event
7
7
from concurrent .futures import ThreadPoolExecutor , TimeoutError
8
8
9
- # Import your SemanticSearchService
10
9
from generate_full_response import SemanticSearchService
11
10
import logging
11
+ import re
12
+
12
13
logging .basicConfig (level = logging .INFO )
13
14
logging .basicConfig (level = logging .DEBUG )
14
15
logger = logging .getLogger (__name__ )
15
16
app = App (token = os .environ ["SLACK_BOT_TOKEN" ])
16
17
17
- # # Initialize the SemanticSearchService
18
18
weaviate_url = os .environ ["WEAVIATE_URL" ]
19
- service = SemanticSearchService (weaviate_url )
20
19
21
- # # Conversation history (we'll keep this for future use)
22
- # conversation_history = {}
20
+ service = SemanticSearchService (weaviate_url )
23
21
24
22
def update_message (client , channel , timestamp , text , blocks = None ):
25
23
client .chat_update (
@@ -29,24 +27,19 @@ def update_message(client, channel, timestamp, text, blocks=None):
29
27
blocks = blocks
30
28
)
31
29
32
- def generate_response_with_animation (event , say , client ):
33
- user_id = event ['user' ]
34
- channel = event ['channel' ]
35
- text = event ['text' ].split ('>' )[1 ].strip ()
36
-
37
- # Initialize or update conversation history
38
- # if user_id not in conversation_history:
39
- # conversation_history[user_id] = []
40
- # conversation_history[user_id].append(f"Human: {text}")
41
-
30
+ def generate_response_with_animation (text , channel , thread_ts , client ):
42
31
# Send an initial message with a random thinking phrase
43
32
thinking_phrases = [
44
33
"Thinking..." ,
45
34
"Processing your request..." ,
46
35
"Hmmmmm..."
47
36
]
48
37
initial_message = random .choice (thinking_phrases )
49
- result = say (initial_message )
38
+ result = client .chat_postMessage (
39
+ channel = channel ,
40
+ text = initial_message ,
41
+ thread_ts = thread_ts # This ensures the message is posted in the thread if there is one
42
+ )
50
43
message_ts = result ['ts' ]
51
44
52
45
# Start the loading animation
@@ -82,60 +75,35 @@ def animate():
82
75
stop_event .set ()
83
76
animation_thread .join ()
84
77
85
- # Update conversation history
86
- # conversation_history[user_id].append(f"Assistant: {response}")
87
-
88
- # # Truncate conversation history if it gets too long
89
- # if len(conversation_history[user_id]) > 10:
90
- # conversation_history[user_id] = conversation_history[user_id][-10:]
91
-
92
- # Create a formatted response with markdown
93
78
formatted_response = f"*Here's what I found:*\n \n { response } "
94
79
95
- # Update the message with the final response
96
- # blocks = [
97
- # {
98
- # "type": "section",
99
- # "text": {"type": "mrkdwn", "text": formatted_response}
100
- # },
101
- # {
102
- # "type": "context",
103
- # "elements": [
104
- # {
105
- # "type": "mrkdwn",
106
- # "text": "If you found this helpful, react with :thumbsup:. If not, react with :thumbsdown:."
107
- # }
108
- # ]
109
- # }
110
- # ]
111
80
update_message (client , channel , message_ts , formatted_response )
112
81
113
- @app .event ("app_mention" )
114
- def handle_mention (event , say , client ):
115
- Thread (target = generate_response_with_animation , args = (event , say , client )).start ()
116
-
117
- # @app.event("reaction_added")
118
- # def handle_reaction(event, say):
119
- # logger.debug(f"Reaction event received: {event}")
82
+ def process_message (event , client ):
83
+ text = event ['text' ]
84
+ channel = event ['channel' ]
85
+ thread_ts = event .get ('thread_ts' , event ['ts' ])
120
86
121
- # reaction = event.get("reaction")
122
- # user = event.get("user")
123
- # item = event.get("item", {})
124
- # ts = item.get("ts")
125
- # channel = item.get("channel")
126
-
127
- # logger.info(f"Reaction: {reaction}, User: {user}, Timestamp: {ts}, Channel: {channel}")
87
+ # Remove the bot mention if it exists
88
+ bot_id = client .auth_test ()["user_id" ]
89
+ text = re .sub (f'<@{ bot_id } >' , '' , text ).strip ()
90
+
91
+ Thread (target = generate_response_with_animation , args = (text , channel , thread_ts , client )).start ()
128
92
129
- # if reaction == "thumbsup":
130
- # say(text="Thank you for the positive feedback! I'm glad I could help.", channel=channel, thread_ts=ts)
131
- # logger.info("Positive feedback received")
132
- # elif reaction == "thumbsdown":
133
- # say(text="I'm sorry my response wasn't helpful. Could you provide more details about what you're looking for?", channel=channel, thread_ts=ts)
134
- # logger.info("Negative feedback received")
135
- # else:
136
- # logger.info(f"Reaction {reaction} received but not handled")
93
+ @app .event ("app_mention" )
94
+ def handle_mention (event , client ):
95
+ process_message (event , client )
137
96
97
+ @app .event ("message" )
98
+ def handle_message (event , client ):
99
+ # Ignore messages from bots
100
+ if "bot_id" in event :
101
+ return
102
+ # Process messages in DMs
103
+ logger .info (event )
104
+ if event ['channel_type' ] == 'im' :
105
+ process_message (event , client )
138
106
139
107
if __name__ == "__main__" :
140
108
handler = SocketModeHandler (app , str (os .environ ["SLACK_APP_TOKEN" ]))
141
- handler .start ()
109
+ handler .start ()
0 commit comments