-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
178 lines (156 loc) · 6.65 KB
/
bot.py
File metadata and controls
178 lines (156 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import ssl
import logging
from threading import Event
from slack_sdk.web import WebClient
from slack_sdk.socket_mode import SocketModeClient
from slack_sdk.socket_mode.response import SocketModeResponse
from slack_sdk.socket_mode.request import SocketModeRequest
from config import conf
from datetime import datetime
from schema import AccessData
import crud
import json
import api
import requests
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
BOT_TOKEN = conf['bot_token']
SOCKET_TOKEN = conf['bot_socket']
BOTNAME = 'bot'
ALLOW_USERS = ['U05K140HSUQ','']
SLACK_CLIENT = SocketModeClient(
# This app-level token will be used only for establishing a connection
app_token=SOCKET_TOKEN, # xapp-A111-222-xyz
# You will be using this AsyncWebClient for performing Web API calls in listeners
web_client=WebClient(token=BOT_TOKEN, ssl=ssl_context) # xoxb-111-222-xyz
)
from slack_sdk.socket_mode.response import SocketModeResponse
from slack_sdk.socket_mode.request import SocketModeRequest
def process(client: SocketModeClient, req: SocketModeRequest):
if req.type == "events_api":
# Acknowledge the request anyway
response = SocketModeResponse(envelope_id=req.envelope_id)
client.send_socket_mode_response(response)
if req.payload["event"]["type"] == "message" \
and req.payload["event"].get("subtype") is None:
access_user_id = req.payload['event']['user']
message_text = req.payload['event']['text']
access_time = req.payload['event']['ts']
channel_id = req.payload['event']['channel']
ts = float(access_time)
convert_time = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S.000000+00:00')
access_data = {
"user_id": access_user_id,
"channel_id": channel_id,
"access_time": convert_time,
"access_id": access_user_id
}
try:
access_user_id = requests.post(
'http://localhost:8080/access',
json=access_data,
verify=False
)
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
# allow api check
if access_user_id:
message_list = message_text.split(' ')
if message_list[0] == 'ioc':
ioc_item = message_list[1]
ioc_type = message_list[2]
ioc_input = {
"ioc_item": ioc_item,
"ioc_type": ioc_type
}
virus_ioc_data = requests.post(
'http://localhost:8080/ioc/virustotal',
json=ioc_input,
verify=False
)
ctx_ioc_data = requests.post(
'http://localhost:8080/ioc/ctx',
json=ioc_input,
verify=False
)
client.web_client.chat_postMessage(
text=virus_ioc_data.text,
channel=req.payload["event"]["channel"],
timestamp=req.payload["event"]["ts"]
)
client.web_client.chat_postMessage(
text=ctx_ioc_data.text,
channel=req.payload["event"]["channel"],
timestamp=req.payload["event"]["ts"]
)
elif message_list[0] == 'bob':
bob_wiki_data = requests.get(
'http://localhost:8080/bob-wiki',
verify=False
)
bob_wiki_list = json.loads(bob_wiki_data.text)
post_messages = []
for bob_wiki in bob_wiki_list:
name = bob_wiki['user_name']
age = bob_wiki['age']
hometown = bob_wiki['hometown']
contents = bob_wiki['contents']
post_messages.append(f'교육생 {name}의 나이는 {age}살이며 {hometown}에 거주한다. {contents}')
client.web_client.chat_postMessage(
text="\n".join(post_messages),
channel=req.payload["event"]["channel"],
timestamp=req.payload["event"]["ts"]
)
else:
pass
if req.type == "interactive" \
and req.payload.get("type") == "shortcut":
if req.payload["callback_id"] == "hello-shortcut":
# Acknowledge the request
response = SocketModeResponse(envelope_id=req.envelope_id)
client.send_socket_mode_response(response)
# Open a welcome modal
client.web_client.views_open(
trigger_id=req.payload["trigger_id"],
view={
"type": "modal",
"callback_id": "hello-modal",
"title": {
"type": "plain_text",
"text": "Greetings!"
},
"submit": {
"type": "plain_text",
"text": "Good Bye"
},
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hello!"
}
}
]
}
)
if req.type == "interactive" \
and req.payload.get("type") == "view_submission":
if req.payload["view"]["callback_id"] == "hello-modal":
# Acknowledge the request and close the modal
response = SocketModeResponse(envelope_id=req.envelope_id)
client.send_socket_mode_response(response)
if __name__ == "__main__":
try:
#rtm.start()
# Add a new listener to receive messages from Slack
# You can add more listeners like this
SLACK_CLIENT.socket_mode_request_listeners.append(process)
# Establish a WebSocket connection to the Socket Mode servers
SLACK_CLIENT.connect()
# Just not to stop this process
Event().wait()
except Exception as main_e:
error = str(main_e)
logging.warning('main func: %s', error)