-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_handler.py
More file actions
75 lines (68 loc) · 3.41 KB
/
message_handler.py
File metadata and controls
75 lines (68 loc) · 3.41 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
import logging
from copy import deepcopy
from slack_sdk.errors import SlackApiError
from slack_sdk.web.async_client import AsyncWebClient
from nlp.message_processing import Message
from nlp.model import Model
from nlp.question_detector import is_question
from utils.message_filters import get_thread_ts
from utils.block_generator import button_block
template = {
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "We found a similar <{}|message>, please rate!"
}
},
{
"type": "actions",
"elements": [
{"type": "button", "text": {"type": "plain_text", "text": "Revelant"},
"style": "primary", "action_id": "review-positive", "value": ""
},
{
"type": "button", "text": {"type": "plain_text", "text": "Useless"},
"style": "danger", "action_id": "review-negative", "value": ""
}
]
}
]
}
async def send_answers(client: AsyncWebClient, model: Model, event: dict, message: dict):
answers = []
channel, thread_ts = message['channel'], get_thread_ts(event)
if is_question(message):
message = Message.from_dict(message)
messages = model.get_similar_messages(message)
logging.info(f"Founded {len(messages)} similar messages")
if messages:
for sim_msg, sim in messages:
logging.info(f'Create permalink for: {sim_msg.channel_id}-{sim_msg.ts}')
try:
link_response = await client.chat_getPermalink(channel=sim_msg.channel_id, message_ts=sim_msg.ts)
except SlackApiError as e:
logging.warning(f"Cannot create link for similar message: {str(e)}")
link_response = {'ok': False}
if link_response['ok']:
new_answer = deepcopy(template)
temp = new_answer['blocks'][0]['text']['text']
new_answer['blocks'][0]['text']['text'] = temp.format(link_response['permalink'])
new_answer['blocks'][1]['elements'][0]['value'] = f"{message.get_key()}/{sim_msg.get_key()}"
new_answer['blocks'][1]['elements'][1]['value'] = f"{message.get_key()}/{sim_msg.get_key()}"
await client.chat_postMessage(channel=channel, thread_ts=thread_ts, unfurl_links=True,
text=f"<{link_response['permalink']}>", blocks=new_answer['blocks'])
answers.append(new_answer)
else:
answers.append({'blocks': [], 'text': 'No similar messages.'})
await client.chat_postMessage(channel=channel, thread_ts=thread_ts,
text='No similar messages.')
else:
answers.append({'blocks': [], 'text': "Question is not detected. Try add '?'"})
await client.chat_postMessage(channel=channel, thread_ts=thread_ts,
text="Question is not detected. Try add '?'")
return answers, thread_ts
async def forward_btn_show(client: AsyncWebClient, direct_id: str, msg_ts: str, thread_ts: str):
button = button_block("Forward message to channel", f"{direct_id}-{msg_ts}", "forward-message")
await client.chat_postMessage(channel=direct_id, thread_ts=thread_ts, blocks=[button])