-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack_socket.py
More file actions
156 lines (117 loc) · 4.8 KB
/
Copy pathslack_socket.py
File metadata and controls
156 lines (117 loc) · 4.8 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
import os
import re
from slack import RTMClient
import ssl
import certifi
import re
import sqlalchemy
from time import sleep
from controllers.main import dialog_flow_engine
@RTMClient.run_on(event="message")
def slack_socket(**payload):
data = payload['data']
web_client = payload['web_client']
if data.get('upload',None) is None and data.get('text',None) is not None and data.get('bot_id',None) is None:
channel_id = data['channel']
query = data['text']
user = data["user"]
user_id = "".join(str(ord(x)) for x in user)#re.sub("[^0-9]", "",user )
response = dialog_flow_engine(int(user_id[0:9]),user_message=query)
reply_markup = response['reply_markup']
attachments = None
username = response['bot_name'] if "Bot" in response['bot_name'] else "The Popbots"
emoji_name = username.split(" ")[0].lower()
len_text_response_no_image = len([value for value in response['response_list'] if not re.match('image',value)])
i=0
for res in response['response_list']:
i+=1
try:
if reply_markup is not None:
if reply_markup['type'] == 'inlineButton' and i == len_text_response_no_image:
buttons = reply_markup['text'].split("|")
actions = []
for button in buttons:
actions.append({
"name": f"{button}",
"text": f"{button}",
"type": "button",
"value": f"{button}"
})
attachments= [{
"text" : "Choose or type an answer",
"fallback" : "You can still type an answer",
"callback_id" : "popbots",
"color" : "#3AA3E3",
"attachment_type" : "default",
"actions" : actions
}
]
if response['img'] is not None and res=="image":
#web_client.files_upload(
# channels=channel_id,
# file=response['img'],
# username = username,
# title="img",
#
#)
pass
else:
web_client.chat_postMessage(
channel=channel_id,
text=res,
#icon_url='https://ibb.co/HhVt0cc',
icon_emoji = ":{}:".format(emoji_name),
username = username,
as_user = False,
attachments = attachments
)
if not i == len_text_response_no_image:
if len(res)<25:
sleep(1)
elif len(res) > 25 and len(res)<75:
sleep(2)
elif len(res) > 75 and len(res)<125:
sleep(3)
else:
sleep(4)
except BaseException as error:
print("[ERROR] The message is empty or invalid")
else:
channel_id = data['channel']
print(data)
if __name__ == "__main__":
SLACK_API_TOKEN = os.getenv("SLACK_API_TOKEN")
ssl_context = ssl.create_default_context(cafile=certifi.where())
slack_token = SLACK_API_TOKEN
if slack_token is None:
print("[ERROR] Slack Token not found")
raise ValueError
rtm_client = RTMClient(token=slack_token,ssl=ssl_context)
rtm_client.start()
""",
#thread_ts=thread_ts
attachments= [
{
"text" : "An answer",
"fallback" : "You can still type an answer",
"callback_id" : "",
"color" : "#3AA3E3",
"attachment_type" : "default",
"actions" : [
{
"name": "game",
"text": "Thermonuclear War",
"style": "danger",
"type": "button",
"value": "war",
"confirm": {
"title": "Are you sure?",
"text": "Wouldn't you prefer a good game of chess?",
"ok_text": "Yes",
"dismiss_text": "No"
}
}
]
}
]
"""