1
1
# Standard library imports
2
2
import logging
3
+ from typing import Optional
3
4
4
5
# Third-party imports
5
6
from slack_sdk .socket_mode import SocketModeClient
@@ -21,57 +22,57 @@ def __call__(self, client: SocketModeClient, req: SocketModeRequest) -> None:
21
22
return None
22
23
23
24
# Acknowledge the request
25
+ logging .info (f"Received an events_api request" )
24
26
response = SocketModeResponse (envelope_id = req .envelope_id )
25
27
client .send_socket_mode_response (response )
26
28
27
29
try :
28
- # Extract user and message information
30
+ # Extract event from payload
29
31
event = req .payload ["event" ]
32
+ sender_is_bot = "bot_id" in event
33
+
34
+ # Ignore messages from bots
35
+ if sender_is_bot :
36
+ logging .info (f"Ignoring an event triggered by a bot." )
37
+ return None
38
+
39
+ # Extract user and message information
30
40
message = event ["text" ]
31
41
user_id = event ["user" ]
32
42
event_type = event ["type" ]
33
43
event_subtype = event .get ("subtype" , None )
34
- sender_is_bot = "bot_id" in event
35
44
36
45
# Ignore changes to messages.
37
46
if event_type == "message" and event_subtype == "message_changed" :
38
- logging .info (f"Ignoring changes to messages ." )
47
+ logging .info (f"Ignoring a change to a message ." )
39
48
return None
40
49
41
- logging .info (f"Received message '{ message } ' from user '{ user_id } '" )
42
-
43
- # Ignore messages from bots
44
- if sender_is_bot :
45
- logging .info (f"Ignoring messages from bots." )
46
- return None
50
+ # Start processing the message
51
+ logging .info (f"Processing message '{ message } ' from user '{ user_id } '." )
47
52
48
53
# If this is a direct message to REGinald...
49
54
if event_type == "message" and event_subtype is None :
55
+ self .react (client , event ["channel" ], event ["ts" ])
50
56
model_response = self .model .direct_message (message , user_id )
51
57
52
58
# If @REGinald is mentioned in a channel
53
59
elif event_type == "app_mention" :
60
+ self .react (client , event ["channel" ], event ["ts" ])
54
61
model_response = self .model .channel_mention (message , user_id )
55
62
56
63
# Otherwise
57
64
else :
58
- logging .info (f"Received unexpected event of type '{ event ['type' ]} '" )
65
+ logging .info (f"Received unexpected event of type '{ event ['type' ]} '. " )
59
66
return None
60
67
61
68
# Add an emoji and a reply as required
62
- if model_response :
63
- if model_response .emoji :
64
- logging .info (f"Applying emoji { model_response .emoji } " )
65
- client .web_client .reactions_add (
66
- name = model_response .emoji ,
67
- channel = event ["channel" ],
68
- timestamp = event ["ts" ],
69
- )
70
- if model_response .message :
71
- logging .info (f"Posting reply { model_response .message } " )
72
- client .web_client .chat_postMessage (
73
- channel = event ["channel" ], text = model_response .message
74
- )
69
+ if model_response and model_response .message :
70
+ logging .info (f"Posting reply { model_response .message } ." )
71
+ client .web_client .chat_postMessage (
72
+ channel = event ["channel" ], text = model_response .message
73
+ )
74
+ else :
75
+ logging .info (f"No reply was generated." )
75
76
76
77
except KeyError as exc :
77
78
logging .warning (f"Attempted to access key that does not exist.\n { str (exc )} " )
@@ -81,3 +82,15 @@ def __call__(self, client: SocketModeClient, req: SocketModeRequest) -> None:
81
82
f"Something went wrong in processing a Slack request.\n Payload: { req .payload } .\n { str (exc )} "
82
83
)
83
84
raise
85
+
86
+ def react (self , client : SocketModeClient , channel : str , timestamp : str ) -> None :
87
+ """Emoji react to the input message"""
88
+ if self .model .emoji :
89
+ logging .info (f"Reacting with emoji { self .model .emoji } ." )
90
+ client .web_client .reactions_add (
91
+ name = self .model .emoji ,
92
+ channel = channel ,
93
+ timestamp = timestamp ,
94
+ )
95
+ else :
96
+ logging .info (f"No emoji defined for this model." )
0 commit comments