From 8eafc402ace3d42c68ad866617e9649b131b62a5 Mon Sep 17 00:00:00 2001 From: Luke Tainton Date: Thu, 20 Apr 2023 19:37:01 +0100 Subject: [PATCH] Add 'My Tasks' button (#23) --- app/commands/submit_task.py | 30 +++++++++++++++++++++++++++--- app/utils/n8n.py | 13 ++++++++++++- test.sh | 3 +++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100755 test.sh diff --git a/app/commands/submit_task.py b/app/commands/submit_task.py index 85c2fa0..ea35204 100644 --- a/app/commands/submit_task.py +++ b/app/commands/submit_task.py @@ -17,7 +17,7 @@ from webexteamssdk.models.cards import ( from webexteamssdk.models.cards.actions import Submit from app.utils.config import config -from app.utils.n8n import submit_task +from app.utils.n8n import submit_task, get_tasks log: logging.Logger = logging.getLogger(__name__) @@ -27,7 +27,7 @@ class SubmitTaskCommand(Command): super().__init__( command_keyword="submit_feedback_dstgmyn", help_message="Submit Task", - chained_commands=[SubmitTaskCallback()], + chained_commands=[SubmitTaskCallback(), MyTasksCallback()], delete_previous_message=True, ) self.sender: str = "" @@ -36,7 +36,6 @@ class SubmitTaskCommand(Command): self.sender = activity.get("actor").get("id") def execute(self, message, attachment_actions, activity) -> Response: - card_body: list = [ ColumnSet( columns=[ @@ -104,6 +103,13 @@ class SubmitTaskCommand(Command): "sender": self.sender, }, ), + Submit( + title="My Tasks", + data={ + "callback_keyword": "my_tasks_callback_rbamzfyx", + "sender": self.sender, + }, + ), Submit(title="Cancel", data={"command_keyword": "exit"}), ], ) @@ -142,3 +148,21 @@ class SubmitTaskCallback(Command): def execute(self, message, attachment_actions, activity) -> str: return self.msg + + +class MyTasksCallback(Command): + def __init__(self) -> None: + super().__init__( + card_callback_keyword="my_tasks_callback_rbamzfyx", delete_previous_message=True + ) + self.msg: str = "" + + def pre_execute(self, message, attachment_actions, activity) -> str: + return "Getting your tasks..." + + def execute(self, message, attachment_actions, activity) -> str | None: + sender: str = attachment_actions.inputs.get("sender") + result: bool = get_tasks(requestor=sender) + if not result: + return "Failed to get tasks. Please try again." + return diff --git a/app/utils/n8n.py b/app/utils/n8n.py index eb5dc06..e1d8e31 100644 --- a/app/utils/n8n.py +++ b/app/utils/n8n.py @@ -16,7 +16,6 @@ def __n8n_post(data: dict) -> bool: def submit_task(summary, description, completion_date, requestor) -> bool: - print(f"submit_task: {completion_date=}") data: dict = { "requestor": requestor, "title": summary, @@ -24,3 +23,15 @@ def submit_task(summary, description, completion_date, requestor) -> bool: "completiondate": completion_date, } return __n8n_post(data=data) + + +def get_tasks(requestor) -> bool: + headers: dict = {"Content-Type": "application/json"} + resp: requests.Response = requests.get( + url=config.n8n_webhook_url, + headers=headers, + timeout=10, + verify=False, + params={"requestor": requestor}, + ) + return bool(resp.status_code == 200) diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..b0f95fc --- /dev/null +++ b/test.sh @@ -0,0 +1,3 @@ +export $(cat .env | xargs) +python -B -m app.main +unset BOT_NAME WEBEX_API_KEY ADMIN_FIRST_NAME ADMIN_EMAIL N8N_WEBHOOK_URL \ No newline at end of file