[Bps-public-commit] rt-extension-notifyslack branch, valid-json-payload, created. ce5c30ec1e2dac5d8627d7eb558f6351c40af49b

? sunnavy sunnavy at bestpractical.com
Thu Sep 3 07:07:07 EDT 2020


The branch, valid-json-payload has been created
        at  ce5c30ec1e2dac5d8627d7eb558f6351c40af49b (commit)

- Log -----------------------------------------------------------------
commit ce5c30ec1e2dac5d8627d7eb558f6351c40af49b
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Sep 3 19:00:35 2020 +0800

    Generate json string from perl structure to make it 100% valid
    
    Previously we didn't escape ticket subject or transaction content, which
    could cause json to be invalid(e.g. if it contains double quotes).
    
    Besides that, previous code also used single quotes and trailing commas,
    which are not valid either(it worked though probably because slack uses
    a tolerant parser).

diff --git a/etc/initialdata b/etc/initialdata
index 4dbe9af..6ae95b1 100644
--- a/etc/initialdata
+++ b/etc/initialdata
@@ -24,28 +24,28 @@ our @Templates;
     Name => 'Slack Create Message',
     Description => 'Send a message to slack channel on ticket create.',
     Content =>q[
-\{
-    "attachments": [
-        \{
-            "color": "#36a64f",
-            "title": "#{$Ticket->Id} {$Ticket->Subject} Created:",
-            "title_link": "{RT->Config->Get( 'WebURL' )}Ticket/Display.html?id={$Ticket->Id}",
-            "text": "{$Transaction->Content}",
-            "fields": [
-                \{
-                    "title" : 'Queue',
-                    "value" : "{$Ticket->QueueObj->Name}",
-                    "short" : false,
-                \},
-                \{
-                    "title" : 'Owner',
-                    "value" : "{$Ticket->OwnerObj->Name}",
-                    "short" : false,
-                \}
-            ],
-        \}
-    ]
-\}]
+{
+    my $payload = {
+        attachments => [
+            {   "color"      => "#36a64f",
+                "title"      => '#' . $Ticket->Id . ' ' . $Ticket->Subject . ' Created:',
+                "title_link" => RT->Config->Get('WebURL') . 'Ticket/Display.html?id=' . $Ticket->Id,
+                "text"       => $Transaction->Content,
+                "fields"     => [
+                    {   "title" => "Queue",
+                        "value" => $Ticket->QueueObj->Name,
+                        "short" => 0,
+                    },
+                    {   "title" => "Owner",
+                        "value" => $Ticket->OwnerObj->Name,
+                        "short" => 0,
+                    },
+                ],
+            },
+        ],
+    };
+    JSON::to_json($payload);
+}
 });
 
 ########################################################################

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list