Skip to content

Commit 8b9bfe5

Browse files
committed
add: [bot] command
1 parent ebf5a4e commit 8b9bfe5

File tree

4 files changed

+349
-75
lines changed

4 files changed

+349
-75
lines changed

bin/case_function.py

+121-41
Original file line numberDiff line numberDiff line change
@@ -9,80 +9,160 @@
99

1010
headers={'X-API-KEY': Config.FLOWINTEL_API_KEY}
1111

12-
def request_post_flowintel(url, data, content_type = 'application/json'):
12+
def request_post_flowintel(url, data, content_type = 'application/json', matrix_id = None):
1313
headers["Content-Type"] = content_type
14+
if matrix_id:
15+
headers["MATRIX-ID"] = matrix_id
1416
res = requests.post(f"{Config.FLOWINTEL_URL}/api/case/{url}", json=data, headers=headers)
1517
return res
1618

17-
def request_get_flowintel(url):
19+
def request_get_flowintel(url, matrix_id = None):
20+
if matrix_id:
21+
headers["MATRIX-ID"] = matrix_id
1822
res = requests.get(f"{Config.FLOWINTEL_URL}/api/case/{url}", headers=headers)
1923
return res
2024

25+
async def check_format(bot, match, message, room, size):
26+
if not len(match.args()) >= size:
27+
await bot.api.send_text_message(room.room_id, "Not enough argument", reply_to=message.event_id)
28+
return False
29+
return True
2130

22-
async def modify_case(bot, match, message, room, type):
23-
"""Modify title or description of a case"""
24-
args_list = match.args()[1:]
25-
case_id = match.args()[0]
31+
async def check_matrix_id(bot, message, room):
32+
res = requests.get(f"{Config.FLOWINTEL_URL}/api/admin/user_matrix_id?matrix_id={message.sender}", headers=headers)
33+
if "message" in res.json():
34+
loc_message = res.json()["message"]
35+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
36+
return False
37+
return res
2638

27-
data = {type: " ".join(arg for arg in args_list)}
28-
res = request_post_flowintel(f"{case_id}/edit", data)
39+
40+
async def send_markdown(bot, message, room, res):
2941
if "message" in res.json():
3042
loc_message = res.json()["message"]
3143
else:
3244
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
33-
last_command = message
3445
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
35-
return last_command
46+
47+
48+
async def modify_case(bot, match, message, room, type):
49+
"""Modify title or description of a case"""
50+
args_list = match.args()[1:]
51+
case_id = match.args()[0]
52+
53+
data = {type: " ".join(arg for arg in args_list)}
54+
res = request_post_flowintel(f"{case_id}/edit", data, matrix_id=message.sender)
55+
return send_markdown(bot, message, room, res)
3656

3757

3858
async def case_check(bot, match, message, room):
3959
"""All ckeck for case part"""
4060
last_command = ""
4161
if match.command("create_case"):
42-
data = {"title": " ".join(arg for arg in match.args())}
43-
res = request_post_flowintel("create", data)
44-
if "message" in res.json():
45-
loc_message = res.json()["message"]
46-
else:
47-
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
4862
last_command = message
49-
await bot.api.send_text_message(room.room_id, loc_message, reply_to=message.event_id)
63+
if await check_format(bot, match, message, room, 1):
64+
data = {"title": " ".join(arg for arg in match.args())}
65+
res = request_post_flowintel("create", data, matrix_id=message.sender)
66+
if "message" in res.json():
67+
loc_message = res.json()["message"]
68+
else:
69+
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
70+
await bot.api.send_text_message(room.room_id, loc_message, reply_to=message.event_id)
5071

5172
if match.command("case_title"):
52-
data = {"title": " ".join(arg for arg in match.args())}
53-
res = request_post_flowintel("title", data)
54-
if "message" in res.json():
55-
loc_message = res.json()["message"]
56-
else:
57-
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
5873
last_command = message
59-
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
74+
if await check_format(bot, match, message, room, 1):
75+
data = {"title": " ".join(arg for arg in match.args())}
76+
res = request_post_flowintel("title", data)
77+
await send_markdown(bot, message, room, res)
6078

61-
if match.command("case_id"):
62-
res = request_get_flowintel(match.args()[0])
63-
if "message" in res.json():
64-
loc_message = res.json()["message"]
65-
else:
66-
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
79+
if match.command("case"):
6780
last_command = message
68-
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
81+
if await check_format(bot, match, message, room, 1):
82+
res = request_get_flowintel(match.args()[0])
83+
await send_markdown(bot, message, room, res)
84+
6985

7086
if match.command("modify_title"):
71-
last_command = modify_case(bot, match, message, room, "title")
72-
87+
last_command = message
88+
if await check_format(bot, match, message, room, 2):
89+
await modify_case(bot, match, message, room, "title")
90+
7391
if match.command("modify_description"):
74-
last_command = modify_case(bot, match, message, room, "description")
92+
if await check_format(bot, match, message, room, 2):
93+
await modify_case(bot, match, message, room, "description")
7594

7695
if match.command("complete_case"):
77-
res = request_get_flowintel(f"{match.args()[0]}/complete")
78-
if "message" in res.json():
79-
loc_message = res.json()["message"]
80-
else:
81-
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
8296
last_command = message
83-
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
97+
if await check_format(bot, match, message, room, 1):
98+
res = request_get_flowintel(f"{match.args()[0]}/complete", matrix_id=message.sender)
99+
await send_markdown(bot, message, room, res)
100+
101+
if match.command("case_tasks"):
102+
last_command = message
103+
if await check_format(bot, match, message, room, 1):
104+
res = request_get_flowintel(f"{match.args()[0]}/tasks")
105+
await send_markdown(bot, message, room, res)
106+
107+
if match.command("case_note"):
108+
last_command = message
109+
if await check_format(bot, match, message, room, 1):
110+
res = request_get_flowintel(f"{match.args()[0]}/get_note")
111+
await send_markdown(bot, message, room, res)
112+
113+
if match.command("case_modify_note"):
114+
last_command = message
115+
if await check_format(bot, match, message, room, 2):
116+
args_list = match.args()[1:]
117+
case_id = match.args()[0]
118+
data = {"note": " ".join(arg for arg in args_list)}
119+
res = request_post_flowintel(f"{case_id}/modif_case_note", data, matrix_id=message.sender)
120+
await send_markdown(bot, message, room, res)
121+
122+
123+
if match.command("case_all_notes"):
124+
last_command = message
125+
if await check_format(bot, match, message, room, 1):
126+
res = request_get_flowintel(f"{match.args()[0]}/all_notes")
127+
await send_markdown(bot, message, room, res)
128+
129+
if match.command("case_all_users"):
130+
last_command = message
131+
if await check_format(bot, match, message, room, 1):
132+
res = request_get_flowintel(f"{match.args()[0]}/get_all_users")
133+
await send_markdown(bot, message, room, res)
134+
135+
if match.command("list_status"):
136+
last_command = message
137+
if await check_format(bot, match, message, room, 1):
138+
res = request_get_flowintel(f"list_status")
139+
await send_markdown(bot, message, room, res)
140+
141+
if match.command("case_status"):
142+
last_command = message
143+
if await check_format(bot, match, message, room, 1):
144+
res = request_get_flowintel(match.args()[0])
145+
if "message" in res.json():
146+
loc_message = res.json()["message"]
147+
else:
148+
res_status = request_get_flowintel(f"list_status")
149+
loc_message = "Status not found"
150+
for status in res_status.json():
151+
if status["id"] == res.json()["status_id"]:
152+
loc_message = f"Case is `{status['name']}`"
153+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
154+
155+
if match.command("case_modify_status"):
156+
last_command = message
157+
if await check_format(bot, match, message, room, 2):
158+
case_id = match.args()[0]
159+
status_id = match.args()[1]
160+
data = {"status_id": status_id}
84161

162+
res = request_post_flowintel(f"{case_id}/change_status", data, matrix_id=message.sender)
163+
await send_markdown(bot, message, room, res)
85164

86165

87166
if last_command:
88-
return last_command
167+
return last_command
168+

bin/help.py

+57-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,74 @@
11
def help():
22
help = "```\n"
33

4+
help += "help: Show this output.\n"
5+
help += "help_case: Show command for case part.\n"
6+
help += "help_task: Show command for task part.\n"
7+
48
## Basic command
59
help += "last_command: Give the last command used.\n"
610
help += "url: Give the url for flowintel-cm instance.\n"
711

812
## Case command
13+
help += help_case(False)
14+
15+
## Task command
16+
help += help_task(False)
17+
18+
help = "```\n"
19+
return help
20+
21+
22+
def help_case(standalone=True):
23+
## Case command
24+
help = ""
25+
if standalone:
26+
help = "```\n"
27+
928
help += "create_case: Create a new case.\n\t!create_case New case name\n"
1029
help += "case_title: Search a case by title.\n\t!case_title title to search\n"
11-
help += "case_id: Get a case by its id.\n\t!case_id 1\n"
12-
help += "modify_title: Modify a title of a case:\n\t!modify_title case_id new title\n"
30+
help += "case: Get a case by its id.\n\t!case_id case_id\n"
31+
help += "modify_title: Modify a title of a case.\n\t!modify_title case_id new title\n"
1332
help += "modify_description: Modify a description of a case:\n\t!modify_description case_id new description\n"
1433
help += "complete_case: Mark a case as Finished.\n\t!complete_case case_id\n"
34+
help += "case_tasks: List all tasks for a case.\n\t!case_tasks case_id\n"
35+
help += "case_note: Get note of a case.\n\t!case_note case_id\n"
36+
help += "case_modify_note: Modify note of a case.\n\t!case_modify_note case_id new note\n"
37+
help += "case_all_notes: Get notes from all tasks in a case.\n\t!case_all_notes case_id\n"
38+
help += "case_all_users: Get list of user that can be assign.\n\t!case_all_users case_id\n"
39+
help += "list_status: Get list of status apllicable to a case.\n\t!list_status\n"
40+
help += "case_status: Get current status of the case.\n\t!case_status case_id\n"
41+
help += "case_modify_status: Modify status of the case.\n\t!case_modify_status case_id status_id\n"
42+
43+
if standalone:
44+
help = "```\n"
45+
46+
return help
1547

48+
49+
def help_task(standalone=True):
1650
## Task command
51+
help = ""
52+
if standalone:
53+
help = "```\n"
54+
1755
help += "create_task: Create a new task in a case.\n\t!create_task case_id task title\n"
56+
help += "task: Get a task by its id.\n\t!task_id task_id\n"
1857
help += "my_assignment: Give task assign to user.\n\t!my_assignment page\n"
58+
help += "modify_title: Modify a title of a task.\n\t!modify_title task_id new title\n"
59+
help += "modify_description: Modify a description of a task.\n\t!modify_description task_id new description\n"
60+
help += "complete_task: Mark a task as Finished.\n\t!complete_task task_id\n"
61+
help += "create_note: Create a note for a task.\n\t!create_note task_id new note\n"
62+
help += "task_get_all_notes: Get all notes for a task.\n\t!task_get_all_notes task_id\n"
63+
help += "task_get_note: Get a note for a task.\n\t!task_get_note task_id\n"
64+
help += "task_modify_note: Modify a note for a task.\n\t!task_modify_note task_id new note\n"
65+
help += "delete_note: Delete a note for a task.\n\t!delete_note task_id\n"
66+
help += "take_task: Assign current user to the task.\n\t!take_task task_id\n"
67+
help += "remove_assignment: Remove assignment of current user on the task.\n\t!remove_assignment task_id\n"
68+
help += "task_status: Get current status of the task.\n\t!task_status task_id\n"
69+
help += "task_modify_status: Modify status of the task.\n\t!task_modify_status task_id status_id\n"
1970

20-
help += "```"
21-
return help
71+
if standalone:
72+
help = "```\n"
73+
74+
return help

bin/neo.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
d = dirname(dirname(abspath(__file__)))
1313
sys.path.append(os.path.join(d, "config"))
1414
import config as Config
15-
from help import help
15+
from help import help, help_task, help_case
1616

1717
from case_function import case_check
1818
from task_function import task_check
@@ -40,6 +40,16 @@ async def echo(room, message):
4040
last_command = message
4141
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
4242

43+
if match.command("help_case"):
44+
loc_message = help_case()
45+
last_command = message
46+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
47+
48+
if match.command("help_task"):
49+
loc_message = help_task()
50+
last_command = message
51+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
52+
4353
if match.command("last_command"):
4454
if last_command:
4555
loc_message = f"```\n{last_command.body}```"

0 commit comments

Comments
 (0)