Skip to content

Commit ebf5a4e

Browse files
committed
new: [bot] basic command
1 parent 499e736 commit ebf5a4e

7 files changed

+261
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
config/config.py
3+
bin/store/*
4+
bin/session.txt
5+
*.pyc

bin/case_function.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import json
2+
import requests
3+
import sys
4+
import os
5+
from os.path import dirname, abspath
6+
d = dirname(dirname(abspath(__file__)))
7+
sys.path.append(os.path.join(d, "config"))
8+
import config as Config
9+
10+
headers={'X-API-KEY': Config.FLOWINTEL_API_KEY}
11+
12+
def request_post_flowintel(url, data, content_type = 'application/json'):
13+
headers["Content-Type"] = content_type
14+
res = requests.post(f"{Config.FLOWINTEL_URL}/api/case/{url}", json=data, headers=headers)
15+
return res
16+
17+
def request_get_flowintel(url):
18+
res = requests.get(f"{Config.FLOWINTEL_URL}/api/case/{url}", headers=headers)
19+
return res
20+
21+
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]
26+
27+
data = {type: " ".join(arg for arg in args_list)}
28+
res = request_post_flowintel(f"{case_id}/edit", data)
29+
if "message" in res.json():
30+
loc_message = res.json()["message"]
31+
else:
32+
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
33+
last_command = message
34+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
35+
return last_command
36+
37+
38+
async def case_check(bot, match, message, room):
39+
"""All ckeck for case part"""
40+
last_command = ""
41+
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```"
48+
last_command = message
49+
await bot.api.send_text_message(room.room_id, loc_message, reply_to=message.event_id)
50+
51+
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```"
58+
last_command = message
59+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
60+
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```"
67+
last_command = message
68+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
69+
70+
if match.command("modify_title"):
71+
last_command = modify_case(bot, match, message, room, "title")
72+
73+
if match.command("modify_description"):
74+
last_command = modify_case(bot, match, message, room, "description")
75+
76+
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```"
82+
last_command = message
83+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
84+
85+
86+
87+
if last_command:
88+
return last_command

bin/help.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def help():
2+
help = "```\n"
3+
4+
## Basic command
5+
help += "last_command: Give the last command used.\n"
6+
help += "url: Give the url for flowintel-cm instance.\n"
7+
8+
## Case command
9+
help += "create_case: Create a new case.\n\t!create_case New case name\n"
10+
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"
13+
help += "modify_description: Modify a description of a case:\n\t!modify_description case_id new description\n"
14+
help += "complete_case: Mark a case as Finished.\n\t!complete_case case_id\n"
15+
16+
## Task command
17+
help += "create_task: Create a new task in a case.\n\t!create_task case_id task title\n"
18+
help += "my_assignment: Give task assign to user.\n\t!my_assignment page\n"
19+
20+
help += "```"
21+
return help

bin/neo.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
Example Usage:
3+
4+
random_user
5+
!case something
6+
"""
7+
8+
import simplematrixbotlib as botlib
9+
import sys
10+
import os
11+
from os.path import dirname, abspath
12+
d = dirname(dirname(abspath(__file__)))
13+
sys.path.append(os.path.join(d, "config"))
14+
import config as Config
15+
from help import help
16+
17+
from case_function import case_check
18+
from task_function import task_check
19+
20+
config = botlib.Config()
21+
config.encryption_enabled = True # Automatically enabled by installing encryption support
22+
config.ignore_unverified_devices = True
23+
24+
creds = botlib.Creds(Config.MATRIX_SERVER, Config.MATRIX_USER, Config.MATRIX_PASSWORD, device_name=Config.MATRIX_DEVICE_NAME)
25+
bot = botlib.Bot(creds, config)
26+
PREFIX = '!'
27+
28+
29+
last_command = ""
30+
31+
32+
@bot.listener.on_message_event
33+
async def echo(room, message):
34+
global last_command
35+
match = botlib.MessageMatch(room, message, bot, PREFIX)
36+
37+
if match.is_not_from_this_bot() and match.prefix():
38+
if match.command("help"):
39+
loc_message = help()
40+
last_command = message
41+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
42+
43+
if match.command("last_command"):
44+
if last_command:
45+
loc_message = f"```\n{last_command.body}```"
46+
else:
47+
loc_message = "```\nNo last command```"
48+
last_command = message
49+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
50+
51+
if match.command("url"):
52+
loc_message = Config.FLOWINTEL_URL
53+
last_command = message
54+
await bot.api.send_text_message(room.room_id, loc_message, reply_to=message.event_id)
55+
56+
loc = await case_check(bot, match, message, room)
57+
if loc:
58+
last_command = loc
59+
60+
loc = await task_check(bot, match, message, room)
61+
if loc:
62+
last_command = loc
63+
64+
bot.run()

bin/task_function.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import json
2+
import requests
3+
import sys
4+
import os
5+
from os.path import dirname, abspath
6+
d = dirname(dirname(abspath(__file__)))
7+
sys.path.append(os.path.join(d, "config"))
8+
import config as Config
9+
10+
headers={'X-API-KEY': Config.FLOWINTEL_API_KEY}
11+
12+
def request_post_flowintel(url, data, content_type = 'application/json'):
13+
headers["Content-Type"] = content_type
14+
res = requests.post(f"{Config.FLOWINTEL_URL}/api/case/{url}", json=data, headers=headers)
15+
return res
16+
17+
def request_get_flowintel(url):
18+
res = requests.get(f"{Config.FLOWINTEL_URL}/api/case/{url}", headers=headers)
19+
return res
20+
21+
22+
23+
async def modify_case(bot, match, message, room, type):
24+
"""Modify title or description of a case"""
25+
args_list = match.args()[1:]
26+
case_id = match.args()[0]
27+
28+
data = {type: " ".join(arg for arg in args_list)}
29+
res = request_post_flowintel(f"{case_id}/edit", data)
30+
if "message" in res.json():
31+
loc_message = res.json()["message"]
32+
else:
33+
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
34+
last_command = message
35+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
36+
return last_command
37+
38+
39+
async def task_check(bot, match, message, room):
40+
"""All ckeck for task part"""
41+
last_command = ""
42+
if match.command("create_task"):
43+
args_list = match.args()[1:]
44+
case_id = match.args()[0]
45+
data = {"title": " ".join(arg for arg in args_list)}
46+
res = request_post_flowintel(f"{case_id}/create_task", data)
47+
if "message" in res.json():
48+
loc_message = res.json()["message"]
49+
else:
50+
loc_message = f"```JSON\n{json.dumps(res.json(), indent=2)}\n```"
51+
last_command = message
52+
await bot.api.send_text_message(room.room_id, loc_message, reply_to=message.event_id)
53+
54+
if match.command("my_assignment"):
55+
res = requests.get(f"{Config.FLOWINTEL_URL}/api/admin/user_matrix_id?matrix_id={message.sender}", headers=headers)
56+
if "message" in res.json():
57+
loc_message = res.json()["message"]
58+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
59+
else:
60+
loc_res = res.json()
61+
62+
res = requests.get(f"{Config.FLOWINTEL_URL}/api/my_assignment/user?user_id={loc_res['id']}", headers=headers)
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```"
67+
await bot.api.send_markdown_message(room.room_id, loc_message, reply_to=message.event_id)
68+
69+
last_command = message
70+
71+
if last_command:
72+
return last_command

config/config.sample.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FLOWINTEL_URL = 'http://localhost:7006' # Used for connector, to know what's the origin of the data
2+
FLOWINTEL_API_KEY = ''
3+
4+
# Matrix bot
5+
MATRIX_SERVER = "https://matrix.org"
6+
MATRIX_USER = ""
7+
MATRIX_PASSWORD = ""
8+
MATRIX_DEVICE_NAME = "Flowintel-cm-neo"

requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests
2+
matrix-nio[e2e]
3+
git+https://codeberg.org/imbev/simplematrixbotlib

0 commit comments

Comments
 (0)